blob: db287d79bdcfb96c8a9f4d432f5c11477eada8a5 [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, Desaif1c35e62010-03-09 16:31:43 +0530116#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
117
Eric Moore635374e2009-03-09 01:21:12 -0600118/**
119 * struct fw_event_work - firmware event struct
120 * @list: link list framework
121 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530122 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600123 * @ioc: per adapter object
124 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530125 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600126 * @ignore: flag meaning this event has been marked to ignore
127 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
128 * @event_data: reply event data payload follows
129 *
130 * This object stored on ioc->fw_event_list.
131 */
132struct fw_event_work {
133 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530134 u8 cancel_pending_work;
135 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600136 struct MPT2SAS_ADAPTER *ioc;
137 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530138 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600139 u8 ignore;
140 u16 event;
141 void *event_data;
142};
143
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530144/* raid transport support */
145static struct raid_template *mpt2sas_raid_template;
146
Eric Moore635374e2009-03-09 01:21:12 -0600147/**
148 * struct _scsi_io_transfer - scsi io transfer
149 * @handle: sas device handle (assigned by firmware)
150 * @is_raid: flag set for hidden raid components
151 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
152 * @data_length: data transfer length
153 * @data_dma: dma pointer to data
154 * @sense: sense data
155 * @lun: lun number
156 * @cdb_length: cdb length
157 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600158 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530159 * @VF_ID: virtual function id
160 * @VP_ID: virtual port id
161 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600162 * @sense_length: sense length
163 * @ioc_status: ioc status
164 * @scsi_state: scsi state
165 * @scsi_status: scsi staus
166 * @log_info: log information
167 * @transfer_length: data length transfer when there is a reply message
168 *
169 * Used for sending internal scsi commands to devices within this module.
170 * Refer to _scsi_send_scsi_io().
171 */
172struct _scsi_io_transfer {
173 u16 handle;
174 u8 is_raid;
175 enum dma_data_direction dir;
176 u32 data_length;
177 dma_addr_t data_dma;
178 u8 sense[SCSI_SENSE_BUFFERSIZE];
179 u32 lun;
180 u8 cdb_length;
181 u8 cdb[32];
182 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530183 u8 VF_ID;
184 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600185 u8 valid_reply;
186 /* the following bits are only valid when 'valid_reply = 1' */
187 u32 sense_length;
188 u16 ioc_status;
189 u8 scsi_state;
190 u8 scsi_status;
191 u32 log_info;
192 u32 transfer_length;
193};
194
195/*
196 * The pci device ids are defined in mpi/mpi2_cnfg.h.
197 */
198static struct pci_device_id scsih_pci_table[] = {
199 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
200 PCI_ANY_ID, PCI_ANY_ID },
201 /* Falcon ~ 2008*/
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
203 PCI_ANY_ID, PCI_ANY_ID },
204 /* Liberator ~ 2108 */
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
206 PCI_ANY_ID, PCI_ANY_ID },
207 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
208 PCI_ANY_ID, PCI_ANY_ID },
209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
210 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530211 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600212 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
213 PCI_ANY_ID, PCI_ANY_ID },
214 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
215 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530216 /* Thunderbolt ~ 2208 */
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
220 PCI_ANY_ID, PCI_ANY_ID },
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
222 PCI_ANY_ID, PCI_ANY_ID },
223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
224 PCI_ANY_ID, PCI_ANY_ID },
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
226 PCI_ANY_ID, PCI_ANY_ID },
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
228 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530229 /* Mustang ~ 2308 */
230 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530231 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530232 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
233 PCI_ANY_ID, PCI_ANY_ID },
234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530235 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600236 {0} /* Terminating entry */
237};
238MODULE_DEVICE_TABLE(pci, scsih_pci_table);
239
240/**
Eric Moored5d135b2009-05-18 13:02:08 -0600241 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600242 *
243 * Note: The logging levels are defined in mpt2sas_debug.h.
244 */
245static int
Eric Moored5d135b2009-05-18 13:02:08 -0600246_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600247{
248 int ret = param_set_int(val, kp);
249 struct MPT2SAS_ADAPTER *ioc;
250
251 if (ret)
252 return ret;
253
254 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600255 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600256 ioc->logging_level = logging_level;
257 return 0;
258}
Eric Moored5d135b2009-05-18 13:02:08 -0600259module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600260 &logging_level, 0644);
261
262/**
263 * _scsih_srch_boot_sas_address - search based on sas_address
264 * @sas_address: sas address
265 * @boot_device: boot device object from bios page 2
266 *
267 * Returns 1 when there's a match, 0 means no match.
268 */
269static inline int
270_scsih_srch_boot_sas_address(u64 sas_address,
271 Mpi2BootDeviceSasWwid_t *boot_device)
272{
273 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
274}
275
276/**
277 * _scsih_srch_boot_device_name - search based on device name
278 * @device_name: device name specified in INDENTIFY fram
279 * @boot_device: boot device object from bios page 2
280 *
281 * Returns 1 when there's a match, 0 means no match.
282 */
283static inline int
284_scsih_srch_boot_device_name(u64 device_name,
285 Mpi2BootDeviceDeviceName_t *boot_device)
286{
287 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
288}
289
290/**
291 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
292 * @enclosure_logical_id: enclosure logical id
293 * @slot_number: slot number
294 * @boot_device: boot device object from bios page 2
295 *
296 * Returns 1 when there's a match, 0 means no match.
297 */
298static inline int
299_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
300 Mpi2BootDeviceEnclosureSlot_t *boot_device)
301{
302 return (enclosure_logical_id == le64_to_cpu(boot_device->
303 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
304 SlotNumber)) ? 1 : 0;
305}
306
307/**
308 * _scsih_is_boot_device - search for matching boot device.
309 * @sas_address: sas address
310 * @device_name: device name specified in INDENTIFY fram
311 * @enclosure_logical_id: enclosure logical id
312 * @slot_number: slot number
313 * @form: specifies boot device form
314 * @boot_device: boot device object from bios page 2
315 *
316 * Returns 1 when there's a match, 0 means no match.
317 */
318static int
319_scsih_is_boot_device(u64 sas_address, u64 device_name,
320 u64 enclosure_logical_id, u16 slot, u8 form,
321 Mpi2BiosPage2BootDevice_t *boot_device)
322{
323 int rc = 0;
324
325 switch (form) {
326 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
327 if (!sas_address)
328 break;
329 rc = _scsih_srch_boot_sas_address(
330 sas_address, &boot_device->SasWwid);
331 break;
332 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
333 if (!enclosure_logical_id)
334 break;
335 rc = _scsih_srch_boot_encl_slot(
336 enclosure_logical_id,
337 slot, &boot_device->EnclosureSlot);
338 break;
339 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
340 if (!device_name)
341 break;
342 rc = _scsih_srch_boot_device_name(
343 device_name, &boot_device->DeviceName);
344 break;
345 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
346 break;
347 }
348
349 return rc;
350}
351
352/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530353 * _scsih_get_sas_address - set the sas_address for given device handle
354 * @handle: device handle
355 * @sas_address: sas address
356 *
357 * Returns 0 success, non-zero when failure
358 */
359static int
360_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
361 u64 *sas_address)
362{
363 Mpi2SasDevicePage0_t sas_device_pg0;
364 Mpi2ConfigReply_t mpi_reply;
365 u32 ioc_status;
366
367 if (handle <= ioc->sas_hba.num_phys) {
368 *sas_address = ioc->sas_hba.sas_address;
369 return 0;
370 } else
371 *sas_address = 0;
372
373 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
374 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
375 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
376 ioc->name, __FILE__, __LINE__, __func__);
377 return -ENXIO;
378 }
379
380 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
381 MPI2_IOCSTATUS_MASK;
382 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
383 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
384 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
385 __FILE__, __LINE__, __func__);
386 return -EIO;
387 }
388
389 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
390 return 0;
391}
392
393/**
Eric Moore635374e2009-03-09 01:21:12 -0600394 * _scsih_determine_boot_device - determine boot device.
395 * @ioc: per adapter object
396 * @device: either sas_device or raid_device object
397 * @is_raid: [flag] 1 = raid object, 0 = sas object
398 *
399 * Determines whether this device should be first reported device to
400 * to scsi-ml or sas transport, this purpose is for persistant boot device.
401 * There are primary, alternate, and current entries in bios page 2. The order
402 * priority is primary, alternate, then current. This routine saves
403 * the corresponding device object and is_raid flag in the ioc object.
404 * The saved data to be used later in _scsih_probe_boot_devices().
405 */
406static void
407_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
408 void *device, u8 is_raid)
409{
410 struct _sas_device *sas_device;
411 struct _raid_device *raid_device;
412 u64 sas_address;
413 u64 device_name;
414 u64 enclosure_logical_id;
415 u16 slot;
416
417 /* only process this function when driver loads */
418 if (!ioc->wait_for_port_enable_to_complete)
419 return;
420
421 if (!is_raid) {
422 sas_device = device;
423 sas_address = sas_device->sas_address;
424 device_name = sas_device->device_name;
425 enclosure_logical_id = sas_device->enclosure_logical_id;
426 slot = sas_device->slot;
427 } else {
428 raid_device = device;
429 sas_address = raid_device->wwid;
430 device_name = 0;
431 enclosure_logical_id = 0;
432 slot = 0;
433 }
434
435 if (!ioc->req_boot_device.device) {
436 if (_scsih_is_boot_device(sas_address, device_name,
437 enclosure_logical_id, slot,
438 (ioc->bios_pg2.ReqBootDeviceForm &
439 MPI2_BIOSPAGE2_FORM_MASK),
440 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530441 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600442 "%s: req_boot_device(0x%016llx)\n",
443 ioc->name, __func__,
444 (unsigned long long)sas_address));
445 ioc->req_boot_device.device = device;
446 ioc->req_boot_device.is_raid = is_raid;
447 }
448 }
449
450 if (!ioc->req_alt_boot_device.device) {
451 if (_scsih_is_boot_device(sas_address, device_name,
452 enclosure_logical_id, slot,
453 (ioc->bios_pg2.ReqAltBootDeviceForm &
454 MPI2_BIOSPAGE2_FORM_MASK),
455 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530456 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600457 "%s: req_alt_boot_device(0x%016llx)\n",
458 ioc->name, __func__,
459 (unsigned long long)sas_address));
460 ioc->req_alt_boot_device.device = device;
461 ioc->req_alt_boot_device.is_raid = is_raid;
462 }
463 }
464
465 if (!ioc->current_boot_device.device) {
466 if (_scsih_is_boot_device(sas_address, device_name,
467 enclosure_logical_id, slot,
468 (ioc->bios_pg2.CurrentBootDeviceForm &
469 MPI2_BIOSPAGE2_FORM_MASK),
470 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530471 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600472 "%s: current_boot_device(0x%016llx)\n",
473 ioc->name, __func__,
474 (unsigned long long)sas_address));
475 ioc->current_boot_device.device = device;
476 ioc->current_boot_device.is_raid = is_raid;
477 }
478 }
479}
480
481/**
482 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
483 * @ioc: per adapter object
484 * @sas_address: sas address
485 * Context: Calling function should acquire ioc->sas_device_lock
486 *
487 * This searches for sas_device based on sas_address, then return sas_device
488 * object.
489 */
490struct _sas_device *
491mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
492 u64 sas_address)
493{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530494 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600495
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530496 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
497 if (sas_device->sas_address == sas_address)
498 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600499
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530500 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
501 if (sas_device->sas_address == sas_address)
502 return sas_device;
503
504 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600505}
506
507/**
508 * _scsih_sas_device_find_by_handle - sas device search
509 * @ioc: per adapter object
510 * @handle: sas device handle (assigned by firmware)
511 * Context: Calling function should acquire ioc->sas_device_lock
512 *
513 * This searches for sas_device based on sas_address, then return sas_device
514 * object.
515 */
516static struct _sas_device *
517_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
518{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530519 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600520
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530521 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
522 if (sas_device->handle == handle)
523 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600524
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530525 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
526 if (sas_device->handle == handle)
527 return sas_device;
528
529 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600530}
531
532/**
533 * _scsih_sas_device_remove - remove sas_device from list.
534 * @ioc: per adapter object
535 * @sas_device: the sas_device object
536 * Context: This function will acquire ioc->sas_device_lock.
537 *
538 * Removing object and freeing associated memory from the ioc->sas_device_list.
539 */
540static void
541_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
542 struct _sas_device *sas_device)
543{
544 unsigned long flags;
545
Kashyap, Desai980ead32010-04-08 17:55:22 +0530546 if (!sas_device)
547 return;
548
Eric Moore635374e2009-03-09 01:21:12 -0600549 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530550 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
551 sas_device->sas_address)) {
552 list_del(&sas_device->list);
553 kfree(sas_device);
554 }
Eric Moore635374e2009-03-09 01:21:12 -0600555 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
556}
557
558/**
559 * _scsih_sas_device_add - insert sas_device to the list.
560 * @ioc: per adapter object
561 * @sas_device: the sas_device object
562 * Context: This function will acquire ioc->sas_device_lock.
563 *
564 * Adding new object to the ioc->sas_device_list.
565 */
566static void
567_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
568 struct _sas_device *sas_device)
569{
570 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600571
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530572 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600573 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
574 sas_device->handle, (unsigned long long)sas_device->sas_address));
575
576 spin_lock_irqsave(&ioc->sas_device_lock, flags);
577 list_add_tail(&sas_device->list, &ioc->sas_device_list);
578 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
579
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530580 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
581 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600582 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600583}
584
585/**
586 * _scsih_sas_device_init_add - insert sas_device to the list.
587 * @ioc: per adapter object
588 * @sas_device: the sas_device object
589 * Context: This function will acquire ioc->sas_device_lock.
590 *
591 * Adding new object at driver load time to the ioc->sas_device_init_list.
592 */
593static void
594_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
595 struct _sas_device *sas_device)
596{
597 unsigned long flags;
598
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530599 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600600 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
601 sas_device->handle, (unsigned long long)sas_device->sas_address));
602
603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
604 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
605 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
606 _scsih_determine_boot_device(ioc, sas_device, 0);
607}
608
609/**
Eric Moore635374e2009-03-09 01:21:12 -0600610 * _scsih_raid_device_find_by_id - raid device search
611 * @ioc: per adapter object
612 * @id: sas device target id
613 * @channel: sas device channel
614 * Context: Calling function should acquire ioc->raid_device_lock
615 *
616 * This searches for raid_device based on target id, then return raid_device
617 * object.
618 */
619static struct _raid_device *
620_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
621{
622 struct _raid_device *raid_device, *r;
623
624 r = NULL;
625 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
626 if (raid_device->id == id && raid_device->channel == channel) {
627 r = raid_device;
628 goto out;
629 }
630 }
631
632 out:
633 return r;
634}
635
636/**
637 * _scsih_raid_device_find_by_handle - raid device search
638 * @ioc: per adapter object
639 * @handle: sas device handle (assigned by firmware)
640 * Context: Calling function should acquire ioc->raid_device_lock
641 *
642 * This searches for raid_device based on handle, then return raid_device
643 * object.
644 */
645static struct _raid_device *
646_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
647{
648 struct _raid_device *raid_device, *r;
649
650 r = NULL;
651 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
652 if (raid_device->handle != handle)
653 continue;
654 r = raid_device;
655 goto out;
656 }
657
658 out:
659 return r;
660}
661
662/**
663 * _scsih_raid_device_find_by_wwid - raid device search
664 * @ioc: per adapter object
665 * @handle: sas device handle (assigned by firmware)
666 * Context: Calling function should acquire ioc->raid_device_lock
667 *
668 * This searches for raid_device based on wwid, then return raid_device
669 * object.
670 */
671static struct _raid_device *
672_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
673{
674 struct _raid_device *raid_device, *r;
675
676 r = NULL;
677 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
678 if (raid_device->wwid != wwid)
679 continue;
680 r = raid_device;
681 goto out;
682 }
683
684 out:
685 return r;
686}
687
688/**
689 * _scsih_raid_device_add - add raid_device object
690 * @ioc: per adapter object
691 * @raid_device: raid_device object
692 *
693 * This is added to the raid_device_list link list.
694 */
695static void
696_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
697 struct _raid_device *raid_device)
698{
699 unsigned long flags;
700
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530701 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600702 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
703 raid_device->handle, (unsigned long long)raid_device->wwid));
704
705 spin_lock_irqsave(&ioc->raid_device_lock, flags);
706 list_add_tail(&raid_device->list, &ioc->raid_device_list);
707 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
708}
709
710/**
711 * _scsih_raid_device_remove - delete raid_device object
712 * @ioc: per adapter object
713 * @raid_device: raid_device object
714 *
715 * This is removed from the raid_device_list link list.
716 */
717static void
718_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
719 struct _raid_device *raid_device)
720{
721 unsigned long flags;
722
723 spin_lock_irqsave(&ioc->raid_device_lock, flags);
724 list_del(&raid_device->list);
725 memset(raid_device, 0, sizeof(struct _raid_device));
726 kfree(raid_device);
727 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
728}
729
730/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530731 * mpt2sas_scsih_expander_find_by_handle - expander device search
732 * @ioc: per adapter object
733 * @handle: expander handle (assigned by firmware)
734 * Context: Calling function should acquire ioc->sas_device_lock
735 *
736 * This searches for expander device based on handle, then returns the
737 * sas_node object.
738 */
739struct _sas_node *
740mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
741{
742 struct _sas_node *sas_expander, *r;
743
744 r = NULL;
745 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
746 if (sas_expander->handle != handle)
747 continue;
748 r = sas_expander;
749 goto out;
750 }
751 out:
752 return r;
753}
754
755/**
Eric Moore635374e2009-03-09 01:21:12 -0600756 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
757 * @ioc: per adapter object
758 * @sas_address: sas address
759 * Context: Calling function should acquire ioc->sas_node_lock.
760 *
761 * This searches for expander device based on sas_address, then returns the
762 * sas_node object.
763 */
764struct _sas_node *
765mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
766 u64 sas_address)
767{
768 struct _sas_node *sas_expander, *r;
769
770 r = NULL;
771 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
772 if (sas_expander->sas_address != sas_address)
773 continue;
774 r = sas_expander;
775 goto out;
776 }
777 out:
778 return r;
779}
780
781/**
782 * _scsih_expander_node_add - insert expander device to the list.
783 * @ioc: per adapter object
784 * @sas_expander: the sas_device object
785 * Context: This function will acquire ioc->sas_node_lock.
786 *
787 * Adding new object to the ioc->sas_expander_list.
788 *
789 * Return nothing.
790 */
791static void
792_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
793 struct _sas_node *sas_expander)
794{
795 unsigned long flags;
796
797 spin_lock_irqsave(&ioc->sas_node_lock, flags);
798 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
800}
801
802/**
803 * _scsih_is_end_device - determines if device is an end device
804 * @device_info: bitfield providing information about the device.
805 * Context: none
806 *
807 * Returns 1 if end device.
808 */
809static int
810_scsih_is_end_device(u32 device_info)
811{
812 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
813 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
814 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
815 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
816 return 1;
817 else
818 return 0;
819}
820
821/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530822 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600823 * @ioc: per adapter object
824 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600825 *
826 * Returns the smid stored scmd pointer.
827 */
828static struct scsi_cmnd *
829_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
830{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530831 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600832}
833
834/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530835 * _scsih_scsi_lookup_get_clear - returns scmd entry
836 * @ioc: per adapter object
837 * @smid: system request message index
838 *
839 * Returns the smid stored scmd pointer.
840 * Then will derefrence the stored scmd pointer.
841 */
842static inline struct scsi_cmnd *
843_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
844{
845 unsigned long flags;
846 struct scsi_cmnd *scmd;
847
848 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
849 scmd = ioc->scsi_lookup[smid - 1].scmd;
850 ioc->scsi_lookup[smid - 1].scmd = NULL;
851 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
852
853 return scmd;
854}
855
856/**
Eric Moore635374e2009-03-09 01:21:12 -0600857 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
858 * @ioc: per adapter object
859 * @smid: system request message index
860 * @scmd: pointer to scsi command object
861 * Context: This function will acquire ioc->scsi_lookup_lock.
862 *
863 * This will search for a scmd pointer in the scsi_lookup array,
864 * returning the revelent smid. A returned value of zero means invalid.
865 */
866static u16
867_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
868 *scmd)
869{
870 u16 smid;
871 unsigned long flags;
872 int i;
873
874 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
875 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530876 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600877 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530878 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600879 goto out;
880 }
881 }
882 out:
883 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
884 return smid;
885}
886
887/**
888 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
889 * @ioc: per adapter object
890 * @id: target id
891 * @channel: channel
892 * Context: This function will acquire ioc->scsi_lookup_lock.
893 *
894 * This will search for a matching channel:id in the scsi_lookup array,
895 * returning 1 if found.
896 */
897static u8
898_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
899 int channel)
900{
901 u8 found;
902 unsigned long flags;
903 int i;
904
905 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
906 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530907 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600908 if (ioc->scsi_lookup[i].scmd &&
909 (ioc->scsi_lookup[i].scmd->device->id == id &&
910 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
911 found = 1;
912 goto out;
913 }
914 }
915 out:
916 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
917 return found;
918}
919
920/**
Eric Moore993e0da2009-05-18 13:00:45 -0600921 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
922 * @ioc: per adapter object
923 * @id: target id
924 * @lun: lun number
925 * @channel: channel
926 * Context: This function will acquire ioc->scsi_lookup_lock.
927 *
928 * This will search for a matching channel:id:lun in the scsi_lookup array,
929 * returning 1 if found.
930 */
931static u8
932_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
933 unsigned int lun, int channel)
934{
935 u8 found;
936 unsigned long flags;
937 int i;
938
939 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
940 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530941 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600942 if (ioc->scsi_lookup[i].scmd &&
943 (ioc->scsi_lookup[i].scmd->device->id == id &&
944 ioc->scsi_lookup[i].scmd->device->channel == channel &&
945 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
946 found = 1;
947 goto out;
948 }
949 }
950 out:
951 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
952 return found;
953}
954
955/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530956 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -0600957 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530958 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -0600959 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530960 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -0600961 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530962static struct chain_tracker *
963_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -0600964{
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530965 struct chain_tracker *chain_req;
966 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600967
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530968 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
969 if (list_empty(&ioc->free_chain_list)) {
970 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
971 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
972 ioc->name);
973 return NULL;
974 }
975 chain_req = list_entry(ioc->free_chain_list.next,
976 struct chain_tracker, tracker_list);
977 list_del_init(&chain_req->tracker_list);
978 list_add_tail(&chain_req->tracker_list,
979 &ioc->scsi_lookup[smid - 1].chain_list);
980 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
981 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -0600982}
983
984/**
985 * _scsih_build_scatter_gather - main sg creation routine
986 * @ioc: per adapter object
987 * @scmd: scsi command
988 * @smid: system request message index
989 * Context: none.
990 *
991 * The main routine that builds scatter gather table from a given
992 * scsi request sent via the .queuecommand main handler.
993 *
994 * Returns 0 success, anything else error
995 */
996static int
997_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
998 struct scsi_cmnd *scmd, u16 smid)
999{
1000 Mpi2SCSIIORequest_t *mpi_request;
1001 dma_addr_t chain_dma;
1002 struct scatterlist *sg_scmd;
1003 void *sg_local, *chain;
1004 u32 chain_offset;
1005 u32 chain_length;
1006 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001007 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001008 u32 sges_in_segment;
1009 u32 sgl_flags;
1010 u32 sgl_flags_last_element;
1011 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301012 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001013
1014 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1015
1016 /* init scatter gather flags */
1017 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1018 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1019 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1020 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1021 << MPI2_SGE_FLAGS_SHIFT;
1022 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1023 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1024 << MPI2_SGE_FLAGS_SHIFT;
1025 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1026
1027 sg_scmd = scsi_sglist(scmd);
1028 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001029 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001030 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1031 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1032 return -ENOMEM;
1033 }
1034
1035 sg_local = &mpi_request->SGL;
1036 sges_in_segment = ioc->max_sges_in_main_message;
1037 if (sges_left <= sges_in_segment)
1038 goto fill_in_last_segment;
1039
1040 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1041 (sges_in_segment * ioc->sge_size))/4;
1042
1043 /* fill in main message segment when there is a chain following */
1044 while (sges_in_segment) {
1045 if (sges_in_segment == 1)
1046 ioc->base_add_sg_single(sg_local,
1047 sgl_flags_last_element | sg_dma_len(sg_scmd),
1048 sg_dma_address(sg_scmd));
1049 else
1050 ioc->base_add_sg_single(sg_local, sgl_flags |
1051 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1052 sg_scmd = sg_next(sg_scmd);
1053 sg_local += ioc->sge_size;
1054 sges_left--;
1055 sges_in_segment--;
1056 }
1057
1058 /* initializing the chain flags and pointers */
1059 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301060 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1061 if (!chain_req)
1062 return -1;
1063 chain = chain_req->chain_buffer;
1064 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001065 do {
1066 sges_in_segment = (sges_left <=
1067 ioc->max_sges_in_chain_message) ? sges_left :
1068 ioc->max_sges_in_chain_message;
1069 chain_offset = (sges_left == sges_in_segment) ?
1070 0 : (sges_in_segment * ioc->sge_size)/4;
1071 chain_length = sges_in_segment * ioc->sge_size;
1072 if (chain_offset) {
1073 chain_offset = chain_offset <<
1074 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1075 chain_length += ioc->sge_size;
1076 }
1077 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1078 chain_length, chain_dma);
1079 sg_local = chain;
1080 if (!chain_offset)
1081 goto fill_in_last_segment;
1082
1083 /* fill in chain segments */
1084 while (sges_in_segment) {
1085 if (sges_in_segment == 1)
1086 ioc->base_add_sg_single(sg_local,
1087 sgl_flags_last_element |
1088 sg_dma_len(sg_scmd),
1089 sg_dma_address(sg_scmd));
1090 else
1091 ioc->base_add_sg_single(sg_local, sgl_flags |
1092 sg_dma_len(sg_scmd),
1093 sg_dma_address(sg_scmd));
1094 sg_scmd = sg_next(sg_scmd);
1095 sg_local += ioc->sge_size;
1096 sges_left--;
1097 sges_in_segment--;
1098 }
1099
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301100 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1101 if (!chain_req)
1102 return -1;
1103 chain = chain_req->chain_buffer;
1104 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001105 } while (1);
1106
1107
1108 fill_in_last_segment:
1109
1110 /* fill the last segment */
1111 while (sges_left) {
1112 if (sges_left == 1)
1113 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1114 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1115 else
1116 ioc->base_add_sg_single(sg_local, sgl_flags |
1117 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1118 sg_scmd = sg_next(sg_scmd);
1119 sg_local += ioc->sge_size;
1120 sges_left--;
1121 }
1122
1123 return 0;
1124}
1125
1126/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301127 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001128 * @sdev: scsi device struct
1129 * @qdepth: requested queue depth
1130 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301131 *
1132 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001133 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301134static void
1135_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001136{
1137 struct Scsi_Host *shost = sdev->host;
1138 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301139 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1140 struct MPT2SAS_DEVICE *sas_device_priv_data;
1141 struct MPT2SAS_TARGET *sas_target_priv_data;
1142 struct _sas_device *sas_device;
1143 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001144
1145 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301146
1147 /* limit max device queue for SATA to 32 */
1148 sas_device_priv_data = sdev->hostdata;
1149 if (!sas_device_priv_data)
1150 goto not_sata;
1151 sas_target_priv_data = sas_device_priv_data->sas_target;
1152 if (!sas_target_priv_data)
1153 goto not_sata;
1154 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1155 goto not_sata;
1156 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1157 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1158 sas_device_priv_data->sas_target->sas_address);
1159 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1160 if (sas_device && sas_device->device_info &
1161 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1162 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1163
1164 not_sata:
1165
Eric Moore635374e2009-03-09 01:21:12 -06001166 if (!sdev->tagged_supported)
1167 max_depth = 1;
1168 if (qdepth > max_depth)
1169 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301170 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1171}
1172
1173/**
1174 * _scsih_change_queue_depth - setting device queue depth
1175 * @sdev: scsi device struct
1176 * @qdepth: requested queue depth
1177 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1178 * (see include/scsi/scsi_host.h for definition)
1179 *
1180 * Returns queue depth.
1181 */
1182static int
1183_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1184{
1185 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1186 _scsih_adjust_queue_depth(sdev, qdepth);
1187 else if (reason == SCSI_QDEPTH_QFULL)
1188 scsi_track_queue_full(sdev, qdepth);
1189 else
1190 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001191
1192 if (sdev->inquiry_len > 7)
1193 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1194 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1195 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1196 sdev->ordered_tags, sdev->scsi_level,
1197 (sdev->inquiry[7] & 2) >> 1);
1198
1199 return sdev->queue_depth;
1200}
1201
1202/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301203 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001204 * @sdev: scsi device struct
1205 * @tag_type: requested tag type
1206 *
1207 * Returns queue tag type.
1208 */
1209static int
Eric Moored5d135b2009-05-18 13:02:08 -06001210_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001211{
1212 if (sdev->tagged_supported) {
1213 scsi_set_tag_type(sdev, tag_type);
1214 if (tag_type)
1215 scsi_activate_tcq(sdev, sdev->queue_depth);
1216 else
1217 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1218 } else
1219 tag_type = 0;
1220
1221 return tag_type;
1222}
1223
1224/**
Eric Moored5d135b2009-05-18 13:02:08 -06001225 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001226 * @starget: scsi target struct
1227 *
1228 * Returns 0 if ok. Any other return is assumed to be an error and
1229 * the device is ignored.
1230 */
1231static int
Eric Moored5d135b2009-05-18 13:02:08 -06001232_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001233{
1234 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1235 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1236 struct MPT2SAS_TARGET *sas_target_priv_data;
1237 struct _sas_device *sas_device;
1238 struct _raid_device *raid_device;
1239 unsigned long flags;
1240 struct sas_rphy *rphy;
1241
1242 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1243 if (!sas_target_priv_data)
1244 return -ENOMEM;
1245
1246 starget->hostdata = sas_target_priv_data;
1247 sas_target_priv_data->starget = starget;
1248 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1249
1250 /* RAID volumes */
1251 if (starget->channel == RAID_CHANNEL) {
1252 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1253 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1254 starget->channel);
1255 if (raid_device) {
1256 sas_target_priv_data->handle = raid_device->handle;
1257 sas_target_priv_data->sas_address = raid_device->wwid;
1258 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1259 raid_device->starget = starget;
1260 }
1261 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1262 return 0;
1263 }
1264
1265 /* sas/sata devices */
1266 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1267 rphy = dev_to_rphy(starget->dev.parent);
1268 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1269 rphy->identify.sas_address);
1270
1271 if (sas_device) {
1272 sas_target_priv_data->handle = sas_device->handle;
1273 sas_target_priv_data->sas_address = sas_device->sas_address;
1274 sas_device->starget = starget;
1275 sas_device->id = starget->id;
1276 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301277 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001278 sas_target_priv_data->flags |=
1279 MPT_TARGET_FLAGS_RAID_COMPONENT;
1280 }
1281 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1282
1283 return 0;
1284}
1285
1286/**
Eric Moored5d135b2009-05-18 13:02:08 -06001287 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001288 * @starget: scsi target struct
1289 *
1290 * Returns nothing.
1291 */
1292static void
Eric Moored5d135b2009-05-18 13:02:08 -06001293_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001294{
1295 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1296 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1297 struct MPT2SAS_TARGET *sas_target_priv_data;
1298 struct _sas_device *sas_device;
1299 struct _raid_device *raid_device;
1300 unsigned long flags;
1301 struct sas_rphy *rphy;
1302
1303 sas_target_priv_data = starget->hostdata;
1304 if (!sas_target_priv_data)
1305 return;
1306
1307 if (starget->channel == RAID_CHANNEL) {
1308 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1309 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1310 starget->channel);
1311 if (raid_device) {
1312 raid_device->starget = NULL;
1313 raid_device->sdev = NULL;
1314 }
1315 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1316 goto out;
1317 }
1318
1319 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1320 rphy = dev_to_rphy(starget->dev.parent);
1321 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1322 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001323 if (sas_device && (sas_device->starget == starget) &&
1324 (sas_device->id == starget->id) &&
1325 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001326 sas_device->starget = NULL;
1327
1328 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1329
1330 out:
1331 kfree(sas_target_priv_data);
1332 starget->hostdata = NULL;
1333}
1334
1335/**
Eric Moored5d135b2009-05-18 13:02:08 -06001336 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001337 * @sdev: scsi device struct
1338 *
1339 * Returns 0 if ok. Any other return is assumed to be an error and
1340 * the device is ignored.
1341 */
1342static int
Eric Moored5d135b2009-05-18 13:02:08 -06001343_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001344{
1345 struct Scsi_Host *shost;
1346 struct MPT2SAS_ADAPTER *ioc;
1347 struct MPT2SAS_TARGET *sas_target_priv_data;
1348 struct MPT2SAS_DEVICE *sas_device_priv_data;
1349 struct scsi_target *starget;
1350 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001351 unsigned long flags;
1352
1353 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1354 if (!sas_device_priv_data)
1355 return -ENOMEM;
1356
1357 sas_device_priv_data->lun = sdev->lun;
1358 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1359
1360 starget = scsi_target(sdev);
1361 sas_target_priv_data = starget->hostdata;
1362 sas_target_priv_data->num_luns++;
1363 sas_device_priv_data->sas_target = sas_target_priv_data;
1364 sdev->hostdata = sas_device_priv_data;
1365 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1366 sdev->no_uld_attach = 1;
1367
1368 shost = dev_to_shost(&starget->dev);
1369 ioc = shost_priv(shost);
1370 if (starget->channel == RAID_CHANNEL) {
1371 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1372 raid_device = _scsih_raid_device_find_by_id(ioc,
1373 starget->id, starget->channel);
1374 if (raid_device)
1375 raid_device->sdev = sdev; /* raid is single lun */
1376 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001377 }
1378
Eric Moore635374e2009-03-09 01:21:12 -06001379 return 0;
1380}
1381
1382/**
Eric Moored5d135b2009-05-18 13:02:08 -06001383 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001384 * @sdev: scsi device struct
1385 *
1386 * Returns nothing.
1387 */
1388static void
Eric Moored5d135b2009-05-18 13:02:08 -06001389_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001390{
1391 struct MPT2SAS_TARGET *sas_target_priv_data;
1392 struct scsi_target *starget;
1393
1394 if (!sdev->hostdata)
1395 return;
1396
1397 starget = scsi_target(sdev);
1398 sas_target_priv_data = starget->hostdata;
1399 sas_target_priv_data->num_luns--;
1400 kfree(sdev->hostdata);
1401 sdev->hostdata = NULL;
1402}
1403
1404/**
Eric Moored5d135b2009-05-18 13:02:08 -06001405 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001406 * @ioc: per adapter object
1407 * @sas_device: the sas_device object
1408 * @sdev: scsi device struct
1409 */
1410static void
Eric Moored5d135b2009-05-18 13:02:08 -06001411_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001412 struct _sas_device *sas_device, struct scsi_device *sdev)
1413{
1414 Mpi2ConfigReply_t mpi_reply;
1415 Mpi2SasDevicePage0_t sas_device_pg0;
1416 u32 ioc_status;
1417 u16 flags;
1418 u32 device_info;
1419
1420 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1421 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1422 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1423 ioc->name, __FILE__, __LINE__, __func__);
1424 return;
1425 }
1426
1427 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1428 MPI2_IOCSTATUS_MASK;
1429 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1430 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1431 ioc->name, __FILE__, __LINE__, __func__);
1432 return;
1433 }
1434
1435 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301436 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001437
1438 sdev_printk(KERN_INFO, sdev,
1439 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1440 "sw_preserve(%s)\n",
1441 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1442 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1443 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1444 "n",
1445 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1446 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1447 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1448}
1449
1450/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301451 * _scsih_is_raid - return boolean indicating device is raid volume
1452 * @dev the device struct object
1453 */
1454static int
1455_scsih_is_raid(struct device *dev)
1456{
1457 struct scsi_device *sdev = to_scsi_device(dev);
1458
1459 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1460}
1461
1462/**
1463 * _scsih_get_resync - get raid volume resync percent complete
1464 * @dev the device struct object
1465 */
1466static void
1467_scsih_get_resync(struct device *dev)
1468{
1469 struct scsi_device *sdev = to_scsi_device(dev);
1470 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1471 static struct _raid_device *raid_device;
1472 unsigned long flags;
1473 Mpi2RaidVolPage0_t vol_pg0;
1474 Mpi2ConfigReply_t mpi_reply;
1475 u32 volume_status_flags;
1476 u8 percent_complete = 0;
1477
1478 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1479 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1480 sdev->channel);
1481 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1482
1483 if (!raid_device)
1484 goto out;
1485
1486 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1487 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1488 sizeof(Mpi2RaidVolPage0_t))) {
1489 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1490 ioc->name, __FILE__, __LINE__, __func__);
1491 goto out;
1492 }
1493
1494 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1495 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1496 percent_complete = raid_device->percent_complete;
1497 out:
1498 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1499}
1500
1501/**
1502 * _scsih_get_state - get raid volume level
1503 * @dev the device struct object
1504 */
1505static void
1506_scsih_get_state(struct device *dev)
1507{
1508 struct scsi_device *sdev = to_scsi_device(dev);
1509 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1510 static struct _raid_device *raid_device;
1511 unsigned long flags;
1512 Mpi2RaidVolPage0_t vol_pg0;
1513 Mpi2ConfigReply_t mpi_reply;
1514 u32 volstate;
1515 enum raid_state state = RAID_STATE_UNKNOWN;
1516
1517 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1518 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1519 sdev->channel);
1520 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1521
1522 if (!raid_device)
1523 goto out;
1524
1525 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1526 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1527 sizeof(Mpi2RaidVolPage0_t))) {
1528 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1529 ioc->name, __FILE__, __LINE__, __func__);
1530 goto out;
1531 }
1532
1533 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1534 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1535 state = RAID_STATE_RESYNCING;
1536 goto out;
1537 }
1538
1539 switch (vol_pg0.VolumeState) {
1540 case MPI2_RAID_VOL_STATE_OPTIMAL:
1541 case MPI2_RAID_VOL_STATE_ONLINE:
1542 state = RAID_STATE_ACTIVE;
1543 break;
1544 case MPI2_RAID_VOL_STATE_DEGRADED:
1545 state = RAID_STATE_DEGRADED;
1546 break;
1547 case MPI2_RAID_VOL_STATE_FAILED:
1548 case MPI2_RAID_VOL_STATE_MISSING:
1549 state = RAID_STATE_OFFLINE;
1550 break;
1551 }
1552 out:
1553 raid_set_state(mpt2sas_raid_template, dev, state);
1554}
1555
1556/**
1557 * _scsih_set_level - set raid level
1558 * @sdev: scsi device struct
1559 * @raid_device: raid_device object
1560 */
1561static void
1562_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1563{
1564 enum raid_level level = RAID_LEVEL_UNKNOWN;
1565
1566 switch (raid_device->volume_type) {
1567 case MPI2_RAID_VOL_TYPE_RAID0:
1568 level = RAID_LEVEL_0;
1569 break;
1570 case MPI2_RAID_VOL_TYPE_RAID10:
1571 level = RAID_LEVEL_10;
1572 break;
1573 case MPI2_RAID_VOL_TYPE_RAID1E:
1574 level = RAID_LEVEL_1E;
1575 break;
1576 case MPI2_RAID_VOL_TYPE_RAID1:
1577 level = RAID_LEVEL_1;
1578 break;
1579 }
1580
1581 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1582}
1583
1584/**
Eric Moore635374e2009-03-09 01:21:12 -06001585 * _scsih_get_volume_capabilities - volume capabilities
1586 * @ioc: per adapter object
1587 * @sas_device: the raid_device object
1588 */
1589static void
1590_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1591 struct _raid_device *raid_device)
1592{
1593 Mpi2RaidVolPage0_t *vol_pg0;
1594 Mpi2RaidPhysDiskPage0_t pd_pg0;
1595 Mpi2SasDevicePage0_t sas_device_pg0;
1596 Mpi2ConfigReply_t mpi_reply;
1597 u16 sz;
1598 u8 num_pds;
1599
1600 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1601 &num_pds)) || !num_pds) {
1602 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1603 ioc->name, __FILE__, __LINE__, __func__);
1604 return;
1605 }
1606
1607 raid_device->num_pds = num_pds;
1608 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1609 sizeof(Mpi2RaidVol0PhysDisk_t));
1610 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1611 if (!vol_pg0) {
1612 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1613 ioc->name, __FILE__, __LINE__, __func__);
1614 return;
1615 }
1616
1617 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1618 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1619 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1620 ioc->name, __FILE__, __LINE__, __func__);
1621 kfree(vol_pg0);
1622 return;
1623 }
1624
1625 raid_device->volume_type = vol_pg0->VolumeType;
1626
1627 /* figure out what the underlying devices are by
1628 * obtaining the device_info bits for the 1st device
1629 */
1630 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1631 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1632 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1633 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1634 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1635 le16_to_cpu(pd_pg0.DevHandle)))) {
1636 raid_device->device_info =
1637 le32_to_cpu(sas_device_pg0.DeviceInfo);
1638 }
1639 }
1640
1641 kfree(vol_pg0);
1642}
1643
1644/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301645 * _scsih_enable_tlr - setting TLR flags
1646 * @ioc: per adapter object
1647 * @sdev: scsi device struct
1648 *
1649 * Enabling Transaction Layer Retries for tape devices when
1650 * vpd page 0x90 is present
1651 *
1652 */
1653static void
1654_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1655{
1656 /* only for TAPE */
1657 if (sdev->type != TYPE_TAPE)
1658 return;
1659
1660 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1661 return;
1662
1663 sas_enable_tlr(sdev);
1664 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1665 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1666 return;
1667
1668}
1669
1670/**
Eric Moored5d135b2009-05-18 13:02:08 -06001671 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001672 * @sdev: scsi device struct
1673 *
1674 * Returns 0 if ok. Any other return is assumed to be an error and
1675 * the device is ignored.
1676 */
1677static int
Eric Moored5d135b2009-05-18 13:02:08 -06001678_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001679{
1680 struct Scsi_Host *shost = sdev->host;
1681 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1682 struct MPT2SAS_DEVICE *sas_device_priv_data;
1683 struct MPT2SAS_TARGET *sas_target_priv_data;
1684 struct _sas_device *sas_device;
1685 struct _raid_device *raid_device;
1686 unsigned long flags;
1687 int qdepth;
1688 u8 ssp_target = 0;
1689 char *ds = "";
1690 char *r_level = "";
1691
1692 qdepth = 1;
1693 sas_device_priv_data = sdev->hostdata;
1694 sas_device_priv_data->configured_lun = 1;
1695 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1696 sas_target_priv_data = sas_device_priv_data->sas_target;
1697
1698 /* raid volume handling */
1699 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1700
1701 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1702 raid_device = _scsih_raid_device_find_by_handle(ioc,
1703 sas_target_priv_data->handle);
1704 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1705 if (!raid_device) {
1706 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1707 ioc->name, __FILE__, __LINE__, __func__);
1708 return 0;
1709 }
1710
1711 _scsih_get_volume_capabilities(ioc, raid_device);
1712
1713 /* RAID Queue Depth Support
1714 * IS volume = underlying qdepth of drive type, either
1715 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1716 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1717 */
1718 if (raid_device->device_info &
1719 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1720 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1721 ds = "SSP";
1722 } else {
1723 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1724 if (raid_device->device_info &
1725 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1726 ds = "SATA";
1727 else
1728 ds = "STP";
1729 }
1730
1731 switch (raid_device->volume_type) {
1732 case MPI2_RAID_VOL_TYPE_RAID0:
1733 r_level = "RAID0";
1734 break;
1735 case MPI2_RAID_VOL_TYPE_RAID1E:
1736 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301737 if (ioc->manu_pg10.OEMIdentifier &&
1738 (ioc->manu_pg10.GenericFlags0 &
1739 MFG10_GF0_R10_DISPLAY) &&
1740 !(raid_device->num_pds % 2))
1741 r_level = "RAID10";
1742 else
1743 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001744 break;
1745 case MPI2_RAID_VOL_TYPE_RAID1:
1746 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1747 r_level = "RAID1";
1748 break;
1749 case MPI2_RAID_VOL_TYPE_RAID10:
1750 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1751 r_level = "RAID10";
1752 break;
1753 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1754 default:
1755 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1756 r_level = "RAIDX";
1757 break;
1758 }
1759
1760 sdev_printk(KERN_INFO, sdev, "%s: "
1761 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1762 r_level, raid_device->handle,
1763 (unsigned long long)raid_device->wwid,
1764 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001765 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301766 /* raid transport support */
1767 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001768 return 0;
1769 }
1770
1771 /* non-raid handling */
1772 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1773 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1774 sas_device_priv_data->sas_target->sas_address);
1775 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1776 if (sas_device) {
1777 if (sas_target_priv_data->flags &
1778 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1779 mpt2sas_config_get_volume_handle(ioc,
1780 sas_device->handle, &sas_device->volume_handle);
1781 mpt2sas_config_get_volume_wwid(ioc,
1782 sas_device->volume_handle,
1783 &sas_device->volume_wwid);
1784 }
1785 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1786 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1787 ssp_target = 1;
1788 ds = "SSP";
1789 } else {
1790 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1791 if (sas_device->device_info &
1792 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1793 ds = "STP";
1794 else if (sas_device->device_info &
1795 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1796 ds = "SATA";
1797 }
1798
1799 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05301800 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06001801 ds, sas_device->handle,
1802 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05301803 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06001804 (unsigned long long)sas_device->device_name);
1805 sdev_printk(KERN_INFO, sdev, "%s: "
1806 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1807 (unsigned long long) sas_device->enclosure_logical_id,
1808 sas_device->slot);
1809
1810 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001811 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001812 }
1813
Mike Christiee881a172009-10-15 17:46:39 -07001814 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06001815
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301816 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06001817 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301818 _scsih_enable_tlr(ioc, sdev);
1819 }
Eric Moore635374e2009-03-09 01:21:12 -06001820 return 0;
1821}
1822
1823/**
Eric Moored5d135b2009-05-18 13:02:08 -06001824 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001825 * @sdev: scsi device struct
1826 * @bdev: pointer to block device context
1827 * @capacity: device size (in 512 byte sectors)
1828 * @params: three element array to place output:
1829 * params[0] number of heads (max 255)
1830 * params[1] number of sectors (max 63)
1831 * params[2] number of cylinders
1832 *
1833 * Return nothing.
1834 */
1835static int
Eric Moored5d135b2009-05-18 13:02:08 -06001836_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001837 sector_t capacity, int params[])
1838{
1839 int heads;
1840 int sectors;
1841 sector_t cylinders;
1842 ulong dummy;
1843
1844 heads = 64;
1845 sectors = 32;
1846
1847 dummy = heads * sectors;
1848 cylinders = capacity;
1849 sector_div(cylinders, dummy);
1850
1851 /*
1852 * Handle extended translation size for logical drives
1853 * > 1Gb
1854 */
1855 if ((ulong)capacity >= 0x200000) {
1856 heads = 255;
1857 sectors = 63;
1858 dummy = heads * sectors;
1859 cylinders = capacity;
1860 sector_div(cylinders, dummy);
1861 }
1862
1863 /* return result */
1864 params[0] = heads;
1865 params[1] = sectors;
1866 params[2] = cylinders;
1867
1868 return 0;
1869}
1870
1871/**
1872 * _scsih_response_code - translation of device response code
1873 * @ioc: per adapter object
1874 * @response_code: response code returned by the device
1875 *
1876 * Return nothing.
1877 */
1878static void
1879_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1880{
1881 char *desc;
1882
1883 switch (response_code) {
1884 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1885 desc = "task management request completed";
1886 break;
1887 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1888 desc = "invalid frame";
1889 break;
1890 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1891 desc = "task management request not supported";
1892 break;
1893 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1894 desc = "task management request failed";
1895 break;
1896 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1897 desc = "task management request succeeded";
1898 break;
1899 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1900 desc = "invalid lun";
1901 break;
1902 case 0xA:
1903 desc = "overlapped tag attempted";
1904 break;
1905 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1906 desc = "task queued, however not sent to target";
1907 break;
1908 default:
1909 desc = "unknown";
1910 break;
1911 }
1912 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1913 ioc->name, response_code, desc);
1914}
1915
1916/**
Eric Moored5d135b2009-05-18 13:02:08 -06001917 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001918 * @ioc: per adapter object
1919 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301920 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001921 * @reply: reply message frame(lower 32bit addr)
1922 * Context: none.
1923 *
1924 * The callback handler when using scsih_issue_tm.
1925 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301926 * Return 1 meaning mf should be freed from _base_interrupt
1927 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001928 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301929static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301930_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001931{
1932 MPI2DefaultReply_t *mpi_reply;
1933
1934 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301935 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001936 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301937 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001938 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1939 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1940 if (mpi_reply) {
1941 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1942 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1943 }
1944 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1945 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301946 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001947}
1948
1949/**
1950 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1951 * @ioc: per adapter object
1952 * @handle: device handle
1953 *
1954 * During taskmangement request, we need to freeze the device queue.
1955 */
1956void
1957mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1958{
1959 struct MPT2SAS_DEVICE *sas_device_priv_data;
1960 struct scsi_device *sdev;
1961 u8 skip = 0;
1962
1963 shost_for_each_device(sdev, ioc->shost) {
1964 if (skip)
1965 continue;
1966 sas_device_priv_data = sdev->hostdata;
1967 if (!sas_device_priv_data)
1968 continue;
1969 if (sas_device_priv_data->sas_target->handle == handle) {
1970 sas_device_priv_data->sas_target->tm_busy = 1;
1971 skip = 1;
1972 ioc->ignore_loginfos = 1;
1973 }
1974 }
1975}
1976
1977/**
1978 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1979 * @ioc: per adapter object
1980 * @handle: device handle
1981 *
1982 * During taskmangement request, we need to freeze the device queue.
1983 */
1984void
1985mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1986{
1987 struct MPT2SAS_DEVICE *sas_device_priv_data;
1988 struct scsi_device *sdev;
1989 u8 skip = 0;
1990
1991 shost_for_each_device(sdev, ioc->shost) {
1992 if (skip)
1993 continue;
1994 sas_device_priv_data = sdev->hostdata;
1995 if (!sas_device_priv_data)
1996 continue;
1997 if (sas_device_priv_data->sas_target->handle == handle) {
1998 sas_device_priv_data->sas_target->tm_busy = 0;
1999 skip = 1;
2000 ioc->ignore_loginfos = 0;
2001 }
2002 }
2003}
2004
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302005
Eric Moore635374e2009-03-09 01:21:12 -06002006/**
2007 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2008 * @ioc: per adapter struct
2009 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302010 * @channel: the channel assigned by the OS
2011 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002012 * @lun: lun number
2013 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2014 * @smid_task: smid assigned to the task
2015 * @timeout: timeout in seconds
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302016 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002017 *
2018 * A generic API for sending task management requests to firmware.
2019 *
Eric Moore635374e2009-03-09 01:21:12 -06002020 * The callback index is set inside `ioc->tm_cb_idx`.
2021 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302022 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002023 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302024int
2025mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2026 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2027 struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002028{
2029 Mpi2SCSITaskManagementRequest_t *mpi_request;
2030 Mpi2SCSITaskManagementReply_t *mpi_reply;
2031 u16 smid = 0;
2032 u32 ioc_state;
2033 unsigned long timeleft;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302034 struct scsi_cmnd *scmd_lookup;
2035 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002036
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302037 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302038 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2039 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2040 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302041 rc = FAILED;
2042 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302043 }
2044
Eric Moore3cb54692010-07-08 14:44:34 -06002045 if (ioc->shost_recovery || ioc->remove_host ||
2046 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002047 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2048 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302049 rc = FAILED;
2050 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002051 }
Eric Moore635374e2009-03-09 01:21:12 -06002052
2053 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2054 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302055 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002056 "active!\n", ioc->name));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302057 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2058 FORCE_BIG_HAMMER);
2059 rc = SUCCESS;
2060 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002061 }
2062
2063 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2064 mpt2sas_base_fault_info(ioc, ioc_state &
2065 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302066 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2067 FORCE_BIG_HAMMER);
2068 rc = SUCCESS;
2069 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002070 }
2071
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302072 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002073 if (!smid) {
2074 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2075 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302076 rc = FAILED;
2077 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002078 }
2079
2080 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302081 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2082 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002083 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2084 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2085 ioc->tm_cmds.smid = smid;
2086 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2087 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2088 mpi_request->DevHandle = cpu_to_le16(handle);
2089 mpi_request->TaskType = type;
2090 mpi_request->TaskMID = cpu_to_le16(smid_task);
2091 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2092 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302093 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302094 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002095 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002096 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2097 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2098 ioc->name, __func__);
2099 _debug_dump_mf(mpi_request,
2100 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302101 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2102 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2103 FORCE_BIG_HAMMER);
2104 rc = SUCCESS;
2105 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2106 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2107 goto err_out;
2108 }
Eric Moore635374e2009-03-09 01:21:12 -06002109 }
2110
2111 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2112 mpi_reply = ioc->tm_cmds.reply;
2113 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2114 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2115 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2116 le32_to_cpu(mpi_reply->IOCLogInfo),
2117 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302118 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002119 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302120 if (mpi_reply->IOCStatus)
2121 _debug_dump_mf(mpi_request,
2122 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2123 }
Eric Moore635374e2009-03-09 01:21:12 -06002124 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302125
2126 /* sanity check:
2127 * Check to see the commands were terminated.
2128 * This is only needed for eh callbacks, hence the scmd check.
2129 */
2130 rc = FAILED;
2131 if (scmd == NULL)
2132 goto bypass_sanity_checks;
2133 switch (type) {
2134 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2135 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2136 if (scmd_lookup && (scmd_lookup->serial_number ==
2137 scmd->serial_number))
2138 rc = FAILED;
2139 else
2140 rc = SUCCESS;
2141 break;
2142
2143 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2144 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2145 rc = FAILED;
2146 else
2147 rc = SUCCESS;
2148 break;
2149
2150 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2151 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2152 rc = FAILED;
2153 else
2154 rc = SUCCESS;
2155 break;
2156 }
2157
2158 bypass_sanity_checks:
2159
2160 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2161 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2162 mutex_unlock(&ioc->tm_cmds.mutex);
2163
2164 return rc;
2165
2166 err_out:
2167 mutex_unlock(&ioc->tm_cmds.mutex);
2168 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002169}
2170
2171/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302172 * _scsih_tm_display_info - displays info about the device
2173 * @ioc: per adapter struct
2174 * @scmd: pointer to scsi command object
2175 *
2176 * Called by task management callback handlers.
2177 */
2178static void
2179_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2180{
2181 struct scsi_target *starget = scmd->device->sdev_target;
2182 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2183 struct _sas_device *sas_device = NULL;
2184 unsigned long flags;
2185
2186 if (!priv_target)
2187 return;
2188
2189 scsi_print_command(scmd);
2190 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2191 starget_printk(KERN_INFO, starget, "volume handle(0x%04x), "
2192 "volume wwid(0x%016llx)\n",
2193 priv_target->handle,
2194 (unsigned long long)priv_target->sas_address);
2195 } else {
2196 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2197 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2198 priv_target->sas_address);
2199 if (sas_device) {
2200 if (priv_target->flags &
2201 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2202 starget_printk(KERN_INFO, starget,
2203 "volume handle(0x%04x), "
2204 "volume wwid(0x%016llx)\n",
2205 sas_device->volume_handle,
2206 (unsigned long long)sas_device->volume_wwid);
2207 }
2208 starget_printk(KERN_INFO, starget,
2209 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2210 sas_device->handle,
2211 (unsigned long long)sas_device->sas_address,
2212 sas_device->phy);
2213 starget_printk(KERN_INFO, starget,
2214 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2215 (unsigned long long)sas_device->enclosure_logical_id,
2216 sas_device->slot);
2217 }
2218 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2219 }
2220}
2221
2222/**
Eric Moored5d135b2009-05-18 13:02:08 -06002223 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302224 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002225 *
2226 * Returns SUCCESS if command aborted else FAILED
2227 */
2228static int
Eric Moored5d135b2009-05-18 13:02:08 -06002229_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002230{
2231 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2232 struct MPT2SAS_DEVICE *sas_device_priv_data;
2233 u16 smid;
2234 u16 handle;
2235 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002236
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302237 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2238 "scmd(%p)\n", scmd);
2239 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002240
2241 sas_device_priv_data = scmd->device->hostdata;
2242 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302243 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2244 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002245 scmd->result = DID_NO_CONNECT << 16;
2246 scmd->scsi_done(scmd);
2247 r = SUCCESS;
2248 goto out;
2249 }
2250
2251 /* search for the command */
2252 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2253 if (!smid) {
2254 scmd->result = DID_RESET << 16;
2255 r = SUCCESS;
2256 goto out;
2257 }
2258
2259 /* for hidden raid components and volumes this is not supported */
2260 if (sas_device_priv_data->sas_target->flags &
2261 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2262 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2263 scmd->result = DID_RESET << 16;
2264 r = FAILED;
2265 goto out;
2266 }
2267
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302268 mpt2sas_halt_firmware(ioc);
2269
Eric Moore635374e2009-03-09 01:21:12 -06002270 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302271 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2272 scmd->device->id, scmd->device->lun,
2273 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002274
2275 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302276 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2277 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002278 return r;
2279}
2280
Eric Moore635374e2009-03-09 01:21:12 -06002281/**
Eric Moored5d135b2009-05-18 13:02:08 -06002282 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302283 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002284 *
2285 * Returns SUCCESS if command aborted else FAILED
2286 */
2287static int
Eric Moored5d135b2009-05-18 13:02:08 -06002288_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002289{
2290 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2291 struct MPT2SAS_DEVICE *sas_device_priv_data;
2292 struct _sas_device *sas_device;
2293 unsigned long flags;
2294 u16 handle;
2295 int r;
2296
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302297 struct scsi_target *starget = scmd->device->sdev_target;
2298
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302299 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302300 "scmd(%p)\n", scmd);
2301 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002302
2303 sas_device_priv_data = scmd->device->hostdata;
2304 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302305 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302306 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002307 scmd->result = DID_NO_CONNECT << 16;
2308 scmd->scsi_done(scmd);
2309 r = SUCCESS;
2310 goto out;
2311 }
2312
2313 /* for hidden raid components obtain the volume_handle */
2314 handle = 0;
2315 if (sas_device_priv_data->sas_target->flags &
2316 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2317 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2318 sas_device = _scsih_sas_device_find_by_handle(ioc,
2319 sas_device_priv_data->sas_target->handle);
2320 if (sas_device)
2321 handle = sas_device->volume_handle;
2322 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2323 } else
2324 handle = sas_device_priv_data->sas_target->handle;
2325
2326 if (!handle) {
2327 scmd->result = DID_RESET << 16;
2328 r = FAILED;
2329 goto out;
2330 }
2331
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302332 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2333 scmd->device->id, scmd->device->lun,
2334 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002335
2336 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302337 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2338 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002339 return r;
2340}
2341
2342/**
Eric Moored5d135b2009-05-18 13:02:08 -06002343 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302344 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002345 *
2346 * Returns SUCCESS if command aborted else FAILED
2347 */
2348static int
Eric Moored5d135b2009-05-18 13:02:08 -06002349_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002350{
2351 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2352 struct MPT2SAS_DEVICE *sas_device_priv_data;
2353 struct _sas_device *sas_device;
2354 unsigned long flags;
2355 u16 handle;
2356 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302357 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002358
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302359 starget_printk(KERN_INFO, starget, "attempting target reset! "
2360 "scmd(%p)\n", scmd);
2361 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002362
2363 sas_device_priv_data = scmd->device->hostdata;
2364 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302365 starget_printk(KERN_INFO, starget, "target been deleted! "
2366 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002367 scmd->result = DID_NO_CONNECT << 16;
2368 scmd->scsi_done(scmd);
2369 r = SUCCESS;
2370 goto out;
2371 }
2372
2373 /* for hidden raid components obtain the volume_handle */
2374 handle = 0;
2375 if (sas_device_priv_data->sas_target->flags &
2376 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2377 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2378 sas_device = _scsih_sas_device_find_by_handle(ioc,
2379 sas_device_priv_data->sas_target->handle);
2380 if (sas_device)
2381 handle = sas_device->volume_handle;
2382 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2383 } else
2384 handle = sas_device_priv_data->sas_target->handle;
2385
2386 if (!handle) {
2387 scmd->result = DID_RESET << 16;
2388 r = FAILED;
2389 goto out;
2390 }
2391
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302392 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2393 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2394 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002395
2396 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302397 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2398 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002399 return r;
2400}
2401
2402/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302403 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302404 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002405 *
2406 * Returns SUCCESS if command aborted else FAILED
2407 */
2408static int
Eric Moored5d135b2009-05-18 13:02:08 -06002409_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002410{
2411 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2412 int r, retval;
2413
2414 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2415 ioc->name, scmd);
2416 scsi_print_command(scmd);
2417
2418 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2419 FORCE_BIG_HAMMER);
2420 r = (retval < 0) ? FAILED : SUCCESS;
2421 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2422 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2423
2424 return r;
2425}
2426
2427/**
2428 * _scsih_fw_event_add - insert and queue up fw_event
2429 * @ioc: per adapter object
2430 * @fw_event: object describing the event
2431 * Context: This function will acquire ioc->fw_event_lock.
2432 *
2433 * This adds the firmware event object into link list, then queues it up to
2434 * be processed from user context.
2435 *
2436 * Return nothing.
2437 */
2438static void
2439_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2440{
2441 unsigned long flags;
2442
2443 if (ioc->firmware_event_thread == NULL)
2444 return;
2445
2446 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2447 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302448 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2449 queue_delayed_work(ioc->firmware_event_thread,
2450 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002451 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2452}
2453
2454/**
2455 * _scsih_fw_event_free - delete fw_event
2456 * @ioc: per adapter object
2457 * @fw_event: object describing the event
2458 * Context: This function will acquire ioc->fw_event_lock.
2459 *
2460 * This removes firmware event object from link list, frees associated memory.
2461 *
2462 * Return nothing.
2463 */
2464static void
2465_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2466 *fw_event)
2467{
2468 unsigned long flags;
2469
2470 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2471 list_del(&fw_event->list);
2472 kfree(fw_event->event_data);
2473 kfree(fw_event);
2474 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2475}
2476
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302477
Eric Moore635374e2009-03-09 01:21:12 -06002478/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302479 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002480 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002481 *
2482 * Return nothing.
2483 */
2484static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302485_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002486{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302487 struct fw_event_work *fw_event;
2488
2489 if (ioc->wait_for_port_enable_to_complete)
2490 return;
2491 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2492 if (!fw_event)
2493 return;
2494 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2495 fw_event->ioc = ioc;
2496 _scsih_fw_event_add(ioc, fw_event);
2497}
2498
2499/**
2500 * _scsih_fw_event_cleanup_queue - cleanup event queue
2501 * @ioc: per adapter object
2502 *
2503 * Walk the firmware event queue, either killing timers, or waiting
2504 * for outstanding events to complete
2505 *
2506 * Return nothing.
2507 */
2508static void
2509_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2510{
2511 struct fw_event_work *fw_event, *next;
2512
2513 if (list_empty(&ioc->fw_event_list) ||
2514 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002515 return;
2516
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302517 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2518 if (cancel_delayed_work(&fw_event->delayed_work)) {
2519 _scsih_fw_event_free(ioc, fw_event);
2520 continue;
2521 }
2522 fw_event->cancel_pending_work = 1;
2523 }
Eric Moore635374e2009-03-09 01:21:12 -06002524}
2525
Eric Moore635374e2009-03-09 01:21:12 -06002526/**
2527 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2528 * @ioc: per adapter object
2529 * @handle: device handle
2530 *
2531 * During device pull we need to appropiately set the sdev state.
2532 */
2533static void
2534_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2535{
2536 struct MPT2SAS_DEVICE *sas_device_priv_data;
2537 struct scsi_device *sdev;
2538
2539 shost_for_each_device(sdev, ioc->shost) {
2540 sas_device_priv_data = sdev->hostdata;
2541 if (!sas_device_priv_data)
2542 continue;
2543 if (!sas_device_priv_data->block)
2544 continue;
2545 if (sas_device_priv_data->sas_target->handle == handle) {
2546 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2547 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2548 "handle(0x%04x)\n", ioc->name, handle));
2549 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302550 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002551 }
2552 }
2553}
2554
2555/**
2556 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2557 * @ioc: per adapter object
2558 * @handle: device handle
2559 *
2560 * During device pull we need to appropiately set the sdev state.
2561 */
2562static void
2563_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2564{
2565 struct MPT2SAS_DEVICE *sas_device_priv_data;
2566 struct scsi_device *sdev;
2567
2568 shost_for_each_device(sdev, ioc->shost) {
2569 sas_device_priv_data = sdev->hostdata;
2570 if (!sas_device_priv_data)
2571 continue;
2572 if (sas_device_priv_data->block)
2573 continue;
2574 if (sas_device_priv_data->sas_target->handle == handle) {
2575 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2576 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2577 "handle(0x%04x)\n", ioc->name, handle));
2578 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302579 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002580 }
2581 }
2582}
2583
2584/**
2585 * _scsih_block_io_to_children_attached_to_ex
2586 * @ioc: per adapter object
2587 * @sas_expander: the sas_device object
2588 *
2589 * This routine set sdev state to SDEV_BLOCK for all devices
2590 * attached to this expander. This function called when expander is
2591 * pulled.
2592 */
2593static void
2594_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2595 struct _sas_node *sas_expander)
2596{
2597 struct _sas_port *mpt2sas_port;
2598 struct _sas_device *sas_device;
2599 struct _sas_node *expander_sibling;
2600 unsigned long flags;
2601
2602 if (!sas_expander)
2603 return;
2604
2605 list_for_each_entry(mpt2sas_port,
2606 &sas_expander->sas_port_list, port_list) {
2607 if (mpt2sas_port->remote_identify.device_type ==
2608 SAS_END_DEVICE) {
2609 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2610 sas_device =
2611 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2612 mpt2sas_port->remote_identify.sas_address);
2613 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2614 if (!sas_device)
2615 continue;
2616 _scsih_block_io_device(ioc, sas_device->handle);
2617 }
2618 }
2619
2620 list_for_each_entry(mpt2sas_port,
2621 &sas_expander->sas_port_list, port_list) {
2622
2623 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302624 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06002625 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302626 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06002627
2628 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2629 expander_sibling =
2630 mpt2sas_scsih_expander_find_by_sas_address(
2631 ioc, mpt2sas_port->remote_identify.sas_address);
2632 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2633 _scsih_block_io_to_children_attached_to_ex(ioc,
2634 expander_sibling);
2635 }
2636 }
2637}
2638
2639/**
2640 * _scsih_block_io_to_children_attached_directly
2641 * @ioc: per adapter object
2642 * @event_data: topology change event data
2643 *
2644 * This routine set sdev state to SDEV_BLOCK for all devices
2645 * direct attached during device pull.
2646 */
2647static void
2648_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2649 Mpi2EventDataSasTopologyChangeList_t *event_data)
2650{
2651 int i;
2652 u16 handle;
2653 u16 reason_code;
2654 u8 phy_number;
2655
2656 for (i = 0; i < event_data->NumEntries; i++) {
2657 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2658 if (!handle)
2659 continue;
2660 phy_number = event_data->StartPhyNum + i;
2661 reason_code = event_data->PHY[i].PhyStatus &
2662 MPI2_EVENT_SAS_TOPO_RC_MASK;
2663 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2664 _scsih_block_io_device(ioc, handle);
2665 }
2666}
2667
2668/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302669 * _scsih_tm_tr_send - send task management request
2670 * @ioc: per adapter object
2671 * @handle: device handle
2672 * Context: interrupt time.
2673 *
2674 * This code is to initiate the device removal handshake protocal
2675 * with controller firmware. This function will issue target reset
2676 * using high priority request queue. It will send a sas iounit
2677 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2678 *
2679 * This is designed to send muliple task management request at the same
2680 * time to the fifo. If the fifo is full, we will append the request,
2681 * and process it in a future completion.
2682 */
2683static void
2684_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2685{
2686 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302687 u16 smid;
2688 struct _sas_device *sas_device;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302689 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302690 unsigned long flags;
2691 struct _tr_list *delayed_tr;
2692
Eric Moore3cb54692010-07-08 14:44:34 -06002693 if (ioc->shost_recovery || ioc->remove_host ||
2694 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302695 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2696 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302697 return;
2698 }
2699
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302700 /* if PD, then return */
2701 if (test_bit(handle, ioc->pd_handles))
2702 return;
2703
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302704 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2705 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302706 if (sas_device && sas_device->starget &&
2707 sas_device->starget->hostdata) {
2708 sas_target_priv_data = sas_device->starget->hostdata;
2709 sas_target_priv_data->deleted = 1;
2710 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2711 "setting delete flag: handle(0x%04x), "
2712 "sas_addr(0x%016llx)\n", ioc->name, handle,
2713 (unsigned long long) sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05302714 }
2715 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302716
2717 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2718 if (!smid) {
2719 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2720 if (!delayed_tr)
2721 return;
2722 INIT_LIST_HEAD(&delayed_tr->list);
2723 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302724 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2725 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2726 "DELAYED:tr:handle(0x%04x), (open)\n",
2727 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302728 return;
2729 }
2730
Kashyap, Desai1278b112010-03-09 17:34:13 +05302731 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2732 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2733 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302734 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2735 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2736 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2737 mpi_request->DevHandle = cpu_to_le16(handle);
2738 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302739 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2740}
2741
2742
2743
2744/**
2745 * _scsih_sas_control_complete - completion routine
2746 * @ioc: per adapter object
2747 * @smid: system request message index
2748 * @msix_index: MSIX table index supplied by the OS
2749 * @reply: reply message frame(lower 32bit addr)
2750 * Context: interrupt time.
2751 *
2752 * This is the sas iounit controll completion routine.
2753 * This code is part of the code to initiate the device removal
2754 * handshake protocal with controller firmware.
2755 *
2756 * Return 1 meaning mf should be freed from _base_interrupt
2757 * 0 means the mf is freed from this function.
2758 */
2759static u8
2760_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2761 u8 msix_index, u32 reply)
2762{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302763#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302764 Mpi2SasIoUnitControlReply_t *mpi_reply =
2765 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302766#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05302767 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2768 "sc_complete:handle(0x%04x), (open) "
2769 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2770 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2771 le16_to_cpu(mpi_reply->IOCStatus),
2772 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302773 return 1;
2774}
2775
2776/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302777 * _scsih_tm_tr_volume_send - send target reset request for volumes
2778 * @ioc: per adapter object
2779 * @handle: device handle
2780 * Context: interrupt time.
2781 *
2782 * This is designed to send muliple task management request at the same
2783 * time to the fifo. If the fifo is full, we will append the request,
2784 * and process it in a future completion.
2785 */
2786static void
2787_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2788{
2789 Mpi2SCSITaskManagementRequest_t *mpi_request;
2790 u16 smid;
2791 struct _tr_list *delayed_tr;
2792
Eric Moore3cb54692010-07-08 14:44:34 -06002793 if (ioc->shost_recovery || ioc->remove_host ||
2794 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302795 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2796 "progress!\n", __func__, ioc->name));
2797 return;
2798 }
2799
2800 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2801 if (!smid) {
2802 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2803 if (!delayed_tr)
2804 return;
2805 INIT_LIST_HEAD(&delayed_tr->list);
2806 delayed_tr->handle = handle;
2807 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2808 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2809 "DELAYED:tr:handle(0x%04x), (open)\n",
2810 ioc->name, handle));
2811 return;
2812 }
2813
2814 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2815 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2816 ioc->tm_tr_volume_cb_idx));
2817 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2818 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2819 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2820 mpi_request->DevHandle = cpu_to_le16(handle);
2821 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2822 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2823}
2824
2825/**
2826 * _scsih_tm_volume_tr_complete - target reset completion
2827 * @ioc: per adapter object
2828 * @smid: system request message index
2829 * @msix_index: MSIX table index supplied by the OS
2830 * @reply: reply message frame(lower 32bit addr)
2831 * Context: interrupt time.
2832 *
2833 * Return 1 meaning mf should be freed from _base_interrupt
2834 * 0 means the mf is freed from this function.
2835 */
2836static u8
2837_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2838 u8 msix_index, u32 reply)
2839{
2840 u16 handle;
2841 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2842 Mpi2SCSITaskManagementReply_t *mpi_reply =
2843 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2844
Eric Moore3cb54692010-07-08 14:44:34 -06002845 if (ioc->shost_recovery || ioc->remove_host ||
2846 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302847 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2848 "progress!\n", __func__, ioc->name));
2849 return 1;
2850 }
2851
2852 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2853 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2854 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2855 dewtprintk(ioc, printk("spurious interrupt: "
2856 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2857 le16_to_cpu(mpi_reply->DevHandle), smid));
2858 return 0;
2859 }
2860
2861 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2862 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2863 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2864 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2865 le32_to_cpu(mpi_reply->IOCLogInfo),
2866 le32_to_cpu(mpi_reply->TerminationCount)));
2867
2868 return _scsih_check_for_pending_tm(ioc, smid);
2869}
2870
2871/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302872 * _scsih_tm_tr_complete -
2873 * @ioc: per adapter object
2874 * @smid: system request message index
2875 * @msix_index: MSIX table index supplied by the OS
2876 * @reply: reply message frame(lower 32bit addr)
2877 * Context: interrupt time.
2878 *
2879 * This is the target reset completion routine.
2880 * This code is part of the code to initiate the device removal
2881 * handshake protocal with controller firmware.
2882 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2883 *
2884 * Return 1 meaning mf should be freed from _base_interrupt
2885 * 0 means the mf is freed from this function.
2886 */
2887static u8
2888_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2889 u32 reply)
2890{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302891 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302892 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302893 Mpi2SCSITaskManagementReply_t *mpi_reply =
2894 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2895 Mpi2SasIoUnitControlRequest_t *mpi_request;
2896 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302897
Eric Moore3cb54692010-07-08 14:44:34 -06002898 if (ioc->shost_recovery || ioc->remove_host ||
2899 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302900 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2901 "progress!\n", __func__, ioc->name));
2902 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302903 }
2904
Kashyap, Desai1278b112010-03-09 17:34:13 +05302905 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2906 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2907 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2908 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2909 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2910 le16_to_cpu(mpi_reply->DevHandle), smid));
2911 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302912 }
2913
Kashyap, Desai1278b112010-03-09 17:34:13 +05302914 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2915 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2916 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2917 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2918 le32_to_cpu(mpi_reply->IOCLogInfo),
2919 le32_to_cpu(mpi_reply->TerminationCount)));
2920
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302921 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2922 if (!smid_sas_ctrl) {
2923 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2924 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302925 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302926 }
2927
Kashyap, Desai1278b112010-03-09 17:34:13 +05302928 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2929 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2930 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302931 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2932 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2933 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2934 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302935 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302936 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302937
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302938 return _scsih_check_for_pending_tm(ioc, smid);
2939}
2940
2941/**
2942 * _scsih_check_for_pending_tm - check for pending task management
2943 * @ioc: per adapter object
2944 * @smid: system request message index
2945 *
2946 * This will check delayed target reset list, and feed the
2947 * next reqeust.
2948 *
2949 * Return 1 meaning mf should be freed from _base_interrupt
2950 * 0 means the mf is freed from this function.
2951 */
2952static u8
2953_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2954{
2955 struct _tr_list *delayed_tr;
2956
2957 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2958 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2959 struct _tr_list, list);
2960 mpt2sas_base_free_smid(ioc, smid);
2961 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2962 list_del(&delayed_tr->list);
2963 kfree(delayed_tr);
2964 return 0;
2965 }
2966
Kashyap, Desai1278b112010-03-09 17:34:13 +05302967 if (!list_empty(&ioc->delayed_tr_list)) {
2968 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2969 struct _tr_list, list);
2970 mpt2sas_base_free_smid(ioc, smid);
2971 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2972 list_del(&delayed_tr->list);
2973 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302974 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302975 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302976
Kashyap, Desai1278b112010-03-09 17:34:13 +05302977 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302978}
2979
2980/**
Eric Moore635374e2009-03-09 01:21:12 -06002981 * _scsih_check_topo_delete_events - sanity check on topo events
2982 * @ioc: per adapter object
2983 * @event_data: the event data payload
2984 *
2985 * This routine added to better handle cable breaker.
2986 *
2987 * This handles the case where driver recieves multiple expander
2988 * add and delete events in a single shot. When there is a delete event
2989 * the routine will void any pending add events waiting in the event queue.
2990 *
2991 * Return nothing.
2992 */
2993static void
2994_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2995 Mpi2EventDataSasTopologyChangeList_t *event_data)
2996{
2997 struct fw_event_work *fw_event;
2998 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2999 u16 expander_handle;
3000 struct _sas_node *sas_expander;
3001 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303002 int i, reason_code;
3003 u16 handle;
3004
3005 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303006 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3007 if (!handle)
3008 continue;
3009 reason_code = event_data->PHY[i].PhyStatus &
3010 MPI2_EVENT_SAS_TOPO_RC_MASK;
3011 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3012 _scsih_tm_tr_send(ioc, handle);
3013 }
Eric Moore635374e2009-03-09 01:21:12 -06003014
3015 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3016 if (expander_handle < ioc->sas_hba.num_phys) {
3017 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3018 return;
3019 }
3020
3021 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3022 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3023 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3024 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3025 expander_handle);
3026 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3027 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3028 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3029 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3030
3031 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3032 return;
3033
3034 /* mark ignore flag for pending events */
3035 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3036 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3037 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3038 fw_event->ignore)
3039 continue;
3040 local_event_data = fw_event->event_data;
3041 if (local_event_data->ExpStatus ==
3042 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3043 local_event_data->ExpStatus ==
3044 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3045 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3046 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303047 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003048 "setting ignoring flag\n", ioc->name));
3049 fw_event->ignore = 1;
3050 }
3051 }
3052 }
3053 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3054}
3055
3056/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303057 * _scsih_set_volume_delete_flag - setting volume delete flag
3058 * @ioc: per adapter object
3059 * @handle: device handle
3060 *
3061 * This
3062 * Return nothing.
3063 */
3064static void
3065_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3066{
3067 struct _raid_device *raid_device;
3068 struct MPT2SAS_TARGET *sas_target_priv_data;
3069 unsigned long flags;
3070
3071 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3072 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3073 if (raid_device && raid_device->starget &&
3074 raid_device->starget->hostdata) {
3075 sas_target_priv_data =
3076 raid_device->starget->hostdata;
3077 sas_target_priv_data->deleted = 1;
3078 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3079 "setting delete flag: handle(0x%04x), "
3080 "wwid(0x%016llx)\n", ioc->name, handle,
3081 (unsigned long long) raid_device->wwid));
3082 }
3083 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3084}
3085
3086/**
3087 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3088 * @handle: input handle
3089 * @a: handle for volume a
3090 * @b: handle for volume b
3091 *
3092 * IR firmware only supports two raid volumes. The purpose of this
3093 * routine is to set the volume handle in either a or b. When the given
3094 * input handle is non-zero, or when a and b have not been set before.
3095 */
3096static void
3097_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3098{
3099 if (!handle || handle == *a || handle == *b)
3100 return;
3101 if (!*a)
3102 *a = handle;
3103 else if (!*b)
3104 *b = handle;
3105}
3106
3107/**
3108 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3109 * @ioc: per adapter object
3110 * @event_data: the event data payload
3111 * Context: interrupt time.
3112 *
3113 * This routine will send target reset to volume, followed by target
3114 * resets to the PDs. This is called when a PD has been removed, or
3115 * volume has been deleted or removed. When the target reset is sent
3116 * to volume, the PD target resets need to be queued to start upon
3117 * completion of the volume target reset.
3118 *
3119 * Return nothing.
3120 */
3121static void
3122_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3123 Mpi2EventDataIrConfigChangeList_t *event_data)
3124{
3125 Mpi2EventIrConfigElement_t *element;
3126 int i;
3127 u16 handle, volume_handle, a, b;
3128 struct _tr_list *delayed_tr;
3129
3130 a = 0;
3131 b = 0;
3132
3133 /* Volume Resets for Deleted or Removed */
3134 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3135 for (i = 0; i < event_data->NumElements; i++, element++) {
3136 if (element->ReasonCode ==
3137 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3138 element->ReasonCode ==
3139 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3140 volume_handle = le16_to_cpu(element->VolDevHandle);
3141 _scsih_set_volume_delete_flag(ioc, volume_handle);
3142 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3143 }
3144 }
3145
3146 /* Volume Resets for UNHIDE events */
3147 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3148 for (i = 0; i < event_data->NumElements; i++, element++) {
3149 if (le32_to_cpu(event_data->Flags) &
3150 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3151 continue;
3152 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3153 volume_handle = le16_to_cpu(element->VolDevHandle);
3154 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3155 }
3156 }
3157
3158 if (a)
3159 _scsih_tm_tr_volume_send(ioc, a);
3160 if (b)
3161 _scsih_tm_tr_volume_send(ioc, b);
3162
3163 /* PD target resets */
3164 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3165 for (i = 0; i < event_data->NumElements; i++, element++) {
3166 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3167 continue;
3168 handle = le16_to_cpu(element->PhysDiskDevHandle);
3169 volume_handle = le16_to_cpu(element->VolDevHandle);
3170 clear_bit(handle, ioc->pd_handles);
3171 if (!volume_handle)
3172 _scsih_tm_tr_send(ioc, handle);
3173 else if (volume_handle == a || volume_handle == b) {
3174 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3175 BUG_ON(!delayed_tr);
3176 INIT_LIST_HEAD(&delayed_tr->list);
3177 delayed_tr->handle = handle;
3178 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3179 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3180 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3181 handle));
3182 } else
3183 _scsih_tm_tr_send(ioc, handle);
3184 }
3185}
3186
3187
3188/**
3189 * _scsih_check_volume_delete_events - set delete flag for volumes
3190 * @ioc: per adapter object
3191 * @event_data: the event data payload
3192 * Context: interrupt time.
3193 *
3194 * This will handle the case when the cable connected to entire volume is
3195 * pulled. We will take care of setting the deleted flag so normal IO will
3196 * not be sent.
3197 *
3198 * Return nothing.
3199 */
3200static void
3201_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3202 Mpi2EventDataIrVolume_t *event_data)
3203{
3204 u32 state;
3205
3206 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3207 return;
3208 state = le32_to_cpu(event_data->NewValue);
3209 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3210 MPI2_RAID_VOL_STATE_FAILED)
3211 _scsih_set_volume_delete_flag(ioc,
3212 le16_to_cpu(event_data->VolDevHandle));
3213}
3214
3215/**
Eric Moore635374e2009-03-09 01:21:12 -06003216 * _scsih_flush_running_cmds - completing outstanding commands.
3217 * @ioc: per adapter object
3218 *
3219 * The flushing out of all pending scmd commands following host reset,
3220 * where all IO is dropped to the floor.
3221 *
3222 * Return nothing.
3223 */
3224static void
3225_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3226{
3227 struct scsi_cmnd *scmd;
3228 u16 smid;
3229 u16 count = 0;
3230
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303231 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303232 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003233 if (!scmd)
3234 continue;
3235 count++;
3236 mpt2sas_base_free_smid(ioc, smid);
3237 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003238 if (ioc->pci_error_recovery)
3239 scmd->result = DID_NO_CONNECT << 16;
3240 else
3241 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003242 scmd->scsi_done(scmd);
3243 }
3244 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3245 ioc->name, count));
3246}
3247
3248/**
Eric Moore3c621b32009-05-18 12:59:41 -06003249 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3250 * @scmd: pointer to scsi command object
3251 * @mpi_request: pointer to the SCSI_IO reqest message frame
3252 *
3253 * Supporting protection 1 and 3.
3254 *
3255 * Returns nothing
3256 */
3257static void
3258_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3259{
3260 u16 eedp_flags;
3261 unsigned char prot_op = scsi_get_prot_op(scmd);
3262 unsigned char prot_type = scsi_get_prot_type(scmd);
3263
Eric Moored334aa72010-04-22 10:47:40 -06003264 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003265 return;
3266
3267 if (prot_op == SCSI_PROT_READ_STRIP)
3268 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3269 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3270 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3271 else
3272 return;
3273
Eric Moore3c621b32009-05-18 12:59:41 -06003274 switch (prot_type) {
3275 case SCSI_PROT_DIF_TYPE1:
3276
3277 /*
3278 * enable ref/guard checking
3279 * auto increment ref tag
3280 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303281 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003282 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3283 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3284 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3285 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003286 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003287
Eric Moored334aa72010-04-22 10:47:40 -06003288 case SCSI_PROT_DIF_TYPE2:
3289
3290 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3291 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3292 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003293 break;
3294
3295 case SCSI_PROT_DIF_TYPE3:
3296
3297 /*
3298 * enable guard checking
3299 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303300 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003301 break;
3302 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303303 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3304 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003305}
3306
3307/**
3308 * _scsih_eedp_error_handling - return sense code for EEDP errors
3309 * @scmd: pointer to scsi command object
3310 * @ioc_status: ioc status
3311 *
3312 * Returns nothing
3313 */
3314static void
3315_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3316{
3317 u8 ascq;
3318 u8 sk;
3319 u8 host_byte;
3320
3321 switch (ioc_status) {
3322 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3323 ascq = 0x01;
3324 break;
3325 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3326 ascq = 0x02;
3327 break;
3328 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3329 ascq = 0x03;
3330 break;
3331 default:
3332 ascq = 0x00;
3333 break;
3334 }
3335
3336 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3337 sk = ILLEGAL_REQUEST;
3338 host_byte = DID_ABORT;
3339 } else {
3340 sk = ABORTED_COMMAND;
3341 host_byte = DID_OK;
3342 }
3343
3344 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3345 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3346 SAM_STAT_CHECK_CONDITION;
3347}
3348
3349/**
Eric Moored5d135b2009-05-18 13:02:08 -06003350 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003351 * @scmd: pointer to scsi command object
3352 * @done: function pointer to be invoked on completion
3353 *
3354 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3355 *
3356 * Returns 0 on success. If there's a failure, return either:
3357 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3358 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3359 */
3360static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003361_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003362{
3363 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3364 struct MPT2SAS_DEVICE *sas_device_priv_data;
3365 struct MPT2SAS_TARGET *sas_target_priv_data;
3366 Mpi2SCSIIORequest_t *mpi_request;
3367 u32 mpi_control;
3368 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003369
3370 scmd->scsi_done = done;
3371 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303372 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003373 scmd->result = DID_NO_CONNECT << 16;
3374 scmd->scsi_done(scmd);
3375 return 0;
3376 }
3377
Eric Moore3cb54692010-07-08 14:44:34 -06003378 if (ioc->pci_error_recovery) {
3379 scmd->result = DID_NO_CONNECT << 16;
3380 scmd->scsi_done(scmd);
3381 return 0;
3382 }
3383
Eric Moore635374e2009-03-09 01:21:12 -06003384 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303385 /* invalid device handle */
3386 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003387 scmd->result = DID_NO_CONNECT << 16;
3388 scmd->scsi_done(scmd);
3389 return 0;
3390 }
3391
Kashyap, Desai130b9582010-04-08 17:54:32 +05303392 /* host recovery or link resets sent via IOCTLs */
3393 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003394 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003395 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303396 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3397 return SCSI_MLQUEUE_DEVICE_BUSY;
3398 /* device has been deleted */
3399 else if (sas_target_priv_data->deleted) {
3400 scmd->result = DID_NO_CONNECT << 16;
3401 scmd->scsi_done(scmd);
3402 return 0;
3403 }
Eric Moore635374e2009-03-09 01:21:12 -06003404
3405 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3406 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3407 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3408 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3409 else
3410 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3411
3412 /* set tags */
3413 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3414 if (scmd->device->tagged_supported) {
3415 if (scmd->device->ordered_tags)
3416 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3417 else
3418 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3419 } else
3420/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3421/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3422 */
3423 mpi_control |= (0x500);
3424
3425 } else
3426 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303427 /* Make sure Device is not raid volume */
3428 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003429 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003430 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3431
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303432 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003433 if (!smid) {
3434 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3435 ioc->name, __func__);
3436 goto out;
3437 }
3438 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3439 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003440 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003441 if (scmd->cmd_len == 32)
3442 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003443 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3444 if (sas_device_priv_data->sas_target->flags &
3445 MPT_TARGET_FLAGS_RAID_COMPONENT)
3446 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3447 else
3448 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3449 mpi_request->DevHandle =
3450 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3451 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3452 mpi_request->Control = cpu_to_le32(mpi_control);
3453 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3454 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3455 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3456 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303457 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003458 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3459 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3460 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303461 mpi_request->VF_ID = 0; /* TODO */
3462 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003463 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3464 mpi_request->LUN);
3465 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3466
3467 if (!mpi_request->DataLength) {
3468 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3469 } else {
3470 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3471 mpt2sas_base_free_smid(ioc, smid);
3472 goto out;
3473 }
3474 }
3475
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303476 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3477 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3478 sas_device_priv_data->sas_target->handle);
3479 else
3480 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003481 return 0;
3482
3483 out:
3484 return SCSI_MLQUEUE_HOST_BUSY;
3485}
3486
Jeff Garzikf2812332010-11-16 02:10:29 -05003487static DEF_SCSI_QCMD(_scsih_qcmd)
3488
Eric Moore635374e2009-03-09 01:21:12 -06003489/**
3490 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3491 * @sense_buffer: sense data returned by target
3492 * @data: normalized skey/asc/ascq
3493 *
3494 * Return nothing.
3495 */
3496static void
3497_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3498{
3499 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3500 /* descriptor format */
3501 data->skey = sense_buffer[1] & 0x0F;
3502 data->asc = sense_buffer[2];
3503 data->ascq = sense_buffer[3];
3504 } else {
3505 /* fixed format */
3506 data->skey = sense_buffer[2] & 0x0F;
3507 data->asc = sense_buffer[12];
3508 data->ascq = sense_buffer[13];
3509 }
3510}
3511
3512#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3513/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003514 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003515 * @ioc: per adapter object
3516 * @scmd: pointer to scsi command object
3517 * @mpi_reply: reply mf payload returned from firmware
3518 *
3519 * scsi_status - SCSI Status code returned from target device
3520 * scsi_state - state info associated with SCSI_IO determined by ioc
3521 * ioc_status - ioc supplied status info
3522 *
3523 * Return nothing.
3524 */
3525static void
3526_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3527 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3528{
3529 u32 response_info;
3530 u8 *response_bytes;
3531 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3532 MPI2_IOCSTATUS_MASK;
3533 u8 scsi_state = mpi_reply->SCSIState;
3534 u8 scsi_status = mpi_reply->SCSIStatus;
3535 char *desc_ioc_state = NULL;
3536 char *desc_scsi_status = NULL;
3537 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303538 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303539 struct _sas_device *sas_device = NULL;
3540 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303541 struct scsi_target *starget = scmd->device->sdev_target;
3542 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3543
3544 if (!priv_target)
3545 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303546
3547 if (log_info == 0x31170000)
3548 return;
Eric Moore635374e2009-03-09 01:21:12 -06003549
3550 switch (ioc_status) {
3551 case MPI2_IOCSTATUS_SUCCESS:
3552 desc_ioc_state = "success";
3553 break;
3554 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3555 desc_ioc_state = "invalid function";
3556 break;
3557 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3558 desc_ioc_state = "scsi recovered error";
3559 break;
3560 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3561 desc_ioc_state = "scsi invalid dev handle";
3562 break;
3563 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3564 desc_ioc_state = "scsi device not there";
3565 break;
3566 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3567 desc_ioc_state = "scsi data overrun";
3568 break;
3569 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3570 desc_ioc_state = "scsi data underrun";
3571 break;
3572 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3573 desc_ioc_state = "scsi io data error";
3574 break;
3575 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3576 desc_ioc_state = "scsi protocol error";
3577 break;
3578 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3579 desc_ioc_state = "scsi task terminated";
3580 break;
3581 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3582 desc_ioc_state = "scsi residual mismatch";
3583 break;
3584 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3585 desc_ioc_state = "scsi task mgmt failed";
3586 break;
3587 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3588 desc_ioc_state = "scsi ioc terminated";
3589 break;
3590 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3591 desc_ioc_state = "scsi ext terminated";
3592 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003593 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3594 desc_ioc_state = "eedp guard error";
3595 break;
3596 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3597 desc_ioc_state = "eedp ref tag error";
3598 break;
3599 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3600 desc_ioc_state = "eedp app tag error";
3601 break;
Eric Moore635374e2009-03-09 01:21:12 -06003602 default:
3603 desc_ioc_state = "unknown";
3604 break;
3605 }
3606
3607 switch (scsi_status) {
3608 case MPI2_SCSI_STATUS_GOOD:
3609 desc_scsi_status = "good";
3610 break;
3611 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3612 desc_scsi_status = "check condition";
3613 break;
3614 case MPI2_SCSI_STATUS_CONDITION_MET:
3615 desc_scsi_status = "condition met";
3616 break;
3617 case MPI2_SCSI_STATUS_BUSY:
3618 desc_scsi_status = "busy";
3619 break;
3620 case MPI2_SCSI_STATUS_INTERMEDIATE:
3621 desc_scsi_status = "intermediate";
3622 break;
3623 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3624 desc_scsi_status = "intermediate condmet";
3625 break;
3626 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3627 desc_scsi_status = "reservation conflict";
3628 break;
3629 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3630 desc_scsi_status = "command terminated";
3631 break;
3632 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3633 desc_scsi_status = "task set full";
3634 break;
3635 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3636 desc_scsi_status = "aca active";
3637 break;
3638 case MPI2_SCSI_STATUS_TASK_ABORTED:
3639 desc_scsi_status = "task aborted";
3640 break;
3641 default:
3642 desc_scsi_status = "unknown";
3643 break;
3644 }
3645
3646 desc_scsi_state[0] = '\0';
3647 if (!scsi_state)
3648 desc_scsi_state = " ";
3649 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3650 strcat(desc_scsi_state, "response info ");
3651 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3652 strcat(desc_scsi_state, "state terminated ");
3653 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3654 strcat(desc_scsi_state, "no status ");
3655 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3656 strcat(desc_scsi_state, "autosense failed ");
3657 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3658 strcat(desc_scsi_state, "autosense valid ");
3659
3660 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303661
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303662 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3663 printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
3664 (unsigned long long)priv_target->sas_address);
3665 } else {
3666 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3667 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3668 priv_target->sas_address);
3669 if (sas_device) {
3670 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
3671 "phy(%d)\n", ioc->name, sas_device->sas_address,
3672 sas_device->phy);
3673 printk(MPT2SAS_WARN_FMT
3674 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
3675 ioc->name, sas_device->enclosure_logical_id,
3676 sas_device->slot);
3677 }
3678 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303679 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303680
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303681 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
3682 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3683 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003684 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3685 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3686 scsi_get_resid(scmd));
3687 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3688 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3689 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3690 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3691 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3692 scsi_status, desc_scsi_state, scsi_state);
3693
3694 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3695 struct sense_info data;
3696 _scsih_normalize_sense(scmd->sense_buffer, &data);
3697 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303698 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3699 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06003700 }
3701
3702 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3703 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3704 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303705 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003706 }
3707}
3708#endif
3709
3710/**
3711 * _scsih_smart_predicted_fault - illuminate Fault LED
3712 * @ioc: per adapter object
3713 * @handle: device handle
3714 *
3715 * Return nothing.
3716 */
3717static void
3718_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3719{
3720 Mpi2SepReply_t mpi_reply;
3721 Mpi2SepRequest_t mpi_request;
3722 struct scsi_target *starget;
3723 struct MPT2SAS_TARGET *sas_target_priv_data;
3724 Mpi2EventNotificationReply_t *event_reply;
3725 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3726 struct _sas_device *sas_device;
3727 ssize_t sz;
3728 unsigned long flags;
3729
3730 /* only handle non-raid devices */
3731 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3732 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3733 if (!sas_device) {
3734 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3735 return;
3736 }
3737 starget = sas_device->starget;
3738 sas_target_priv_data = starget->hostdata;
3739
3740 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3741 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3742 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3743 return;
3744 }
3745 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3746 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3747
3748 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3749 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3750 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3751 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3752 mpi_request.SlotStatus =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303753 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
Eric Moore635374e2009-03-09 01:21:12 -06003754 mpi_request.DevHandle = cpu_to_le16(handle);
3755 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3756 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3757 &mpi_request)) != 0) {
3758 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3759 ioc->name, __FILE__, __LINE__, __func__);
3760 return;
3761 }
3762
3763 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3764 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3765 "enclosure_processor: ioc_status (0x%04x), "
3766 "loginfo(0x%08x)\n", ioc->name,
3767 le16_to_cpu(mpi_reply.IOCStatus),
3768 le32_to_cpu(mpi_reply.IOCLogInfo)));
3769 return;
3770 }
3771 }
3772
3773 /* insert into event log */
3774 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3775 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3776 event_reply = kzalloc(sz, GFP_KERNEL);
3777 if (!event_reply) {
3778 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3779 ioc->name, __FILE__, __LINE__, __func__);
3780 return;
3781 }
3782
3783 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3784 event_reply->Event =
3785 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3786 event_reply->MsgLength = sz/4;
3787 event_reply->EventDataLength =
3788 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3789 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3790 event_reply->EventData;
3791 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3792 event_data->ASC = 0x5D;
3793 event_data->DevHandle = cpu_to_le16(handle);
3794 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3795 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3796 kfree(event_reply);
3797}
3798
3799/**
Eric Moored5d135b2009-05-18 13:02:08 -06003800 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003801 * @ioc: per adapter object
3802 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303803 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003804 * @reply: reply message frame(lower 32bit addr)
3805 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303806 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003807 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303808 * Return 1 meaning mf should be freed from _base_interrupt
3809 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003810 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303811static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303812_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003813{
3814 Mpi2SCSIIORequest_t *mpi_request;
3815 Mpi2SCSIIOReply_t *mpi_reply;
3816 struct scsi_cmnd *scmd;
3817 u16 ioc_status;
3818 u32 xfer_cnt;
3819 u8 scsi_state;
3820 u8 scsi_status;
3821 u32 log_info;
3822 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303823 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003824
3825 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303826 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003827 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303828 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003829
3830 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3831
3832 if (mpi_reply == NULL) {
3833 scmd->result = DID_OK << 16;
3834 goto out;
3835 }
3836
3837 sas_device_priv_data = scmd->device->hostdata;
3838 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3839 sas_device_priv_data->sas_target->deleted) {
3840 scmd->result = DID_NO_CONNECT << 16;
3841 goto out;
3842 }
3843
3844 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303845 scsi_state = mpi_reply->SCSIState;
3846 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3847 response_code =
3848 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003849 if (!sas_device_priv_data->tlr_snoop_check) {
3850 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303851 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3852 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303853 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3854 sas_disable_tlr(scmd->device);
3855 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3856 }
Eric Moore635374e2009-03-09 01:21:12 -06003857 }
3858
3859 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3860 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3861 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3862 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3863 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3864 else
3865 log_info = 0;
3866 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003867 scsi_status = mpi_reply->SCSIStatus;
3868
3869 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3870 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3871 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3872 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3873 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3874 }
3875
3876 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3877 struct sense_info data;
3878 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3879 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003880 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003881 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003882 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003883 _scsih_normalize_sense(scmd->sense_buffer, &data);
3884 /* failure prediction threshold exceeded */
3885 if (data.asc == 0x5D)
3886 _scsih_smart_predicted_fault(ioc,
3887 le16_to_cpu(mpi_reply->DevHandle));
3888 }
3889
3890 switch (ioc_status) {
3891 case MPI2_IOCSTATUS_BUSY:
3892 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3893 scmd->result = SAM_STAT_BUSY;
3894 break;
3895
3896 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3897 scmd->result = DID_NO_CONNECT << 16;
3898 break;
3899
3900 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3901 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303902 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3903 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003904 }
Eric Moore635374e2009-03-09 01:21:12 -06003905 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3906 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3907 scmd->result = DID_RESET << 16;
3908 break;
3909
3910 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3911 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3912 scmd->result = DID_SOFT_ERROR << 16;
3913 else
3914 scmd->result = (DID_OK << 16) | scsi_status;
3915 break;
3916
3917 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3918 scmd->result = (DID_OK << 16) | scsi_status;
3919
3920 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3921 break;
3922
3923 if (xfer_cnt < scmd->underflow) {
3924 if (scsi_status == SAM_STAT_BUSY)
3925 scmd->result = SAM_STAT_BUSY;
3926 else
3927 scmd->result = DID_SOFT_ERROR << 16;
3928 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3929 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3930 scmd->result = DID_SOFT_ERROR << 16;
3931 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3932 scmd->result = DID_RESET << 16;
3933 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3934 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3935 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3936 scmd->result = (DRIVER_SENSE << 24) |
3937 SAM_STAT_CHECK_CONDITION;
3938 scmd->sense_buffer[0] = 0x70;
3939 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3940 scmd->sense_buffer[12] = 0x20;
3941 scmd->sense_buffer[13] = 0;
3942 }
3943 break;
3944
3945 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3946 scsi_set_resid(scmd, 0);
3947 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3948 case MPI2_IOCSTATUS_SUCCESS:
3949 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303950 if (response_code ==
3951 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3952 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3953 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003954 scmd->result = DID_SOFT_ERROR << 16;
3955 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3956 scmd->result = DID_RESET << 16;
3957 break;
3958
Eric Moore3c621b32009-05-18 12:59:41 -06003959 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3960 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3961 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3962 _scsih_eedp_error_handling(scmd, ioc_status);
3963 break;
Eric Moore635374e2009-03-09 01:21:12 -06003964 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3965 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3966 case MPI2_IOCSTATUS_INVALID_SGL:
3967 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3968 case MPI2_IOCSTATUS_INVALID_FIELD:
3969 case MPI2_IOCSTATUS_INVALID_STATE:
3970 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3971 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3972 default:
3973 scmd->result = DID_SOFT_ERROR << 16;
3974 break;
3975
3976 }
3977
3978#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3979 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3980 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3981#endif
3982
3983 out:
3984 scsi_dma_unmap(scmd);
3985 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303986 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003987}
3988
3989/**
Eric Moore635374e2009-03-09 01:21:12 -06003990 * _scsih_sas_host_refresh - refreshing sas host object contents
3991 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003992 * Context: user
3993 *
3994 * During port enable, fw will send topology events for every device. Its
3995 * possible that the handles may change from the previous setting, so this
3996 * code keeping handles updating if changed.
3997 *
3998 * Return nothing.
3999 */
4000static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304001_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004002{
4003 u16 sz;
4004 u16 ioc_status;
4005 int i;
4006 Mpi2ConfigReply_t mpi_reply;
4007 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304008 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304009 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004010
4011 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4012 "updating handles for sas_host(0x%016llx)\n",
4013 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4014
4015 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4016 * sizeof(Mpi2SasIOUnit0PhyData_t));
4017 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4018 if (!sas_iounit_pg0) {
4019 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4020 ioc->name, __FILE__, __LINE__, __func__);
4021 return;
4022 }
Eric Moore635374e2009-03-09 01:21:12 -06004023
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304024 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4025 sas_iounit_pg0, sz)) != 0)
4026 goto out;
4027 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4028 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4029 goto out;
4030 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304031 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304032 if (i == 0)
4033 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4034 PhyData[0].ControllerDevHandle);
4035 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4036 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4037 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304038 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4039 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304040 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304041 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304042 }
Eric Moore635374e2009-03-09 01:21:12 -06004043 out:
4044 kfree(sas_iounit_pg0);
4045}
4046
4047/**
4048 * _scsih_sas_host_add - create sas host object
4049 * @ioc: per adapter object
4050 *
4051 * Creating host side data object, stored in ioc->sas_hba
4052 *
4053 * Return nothing.
4054 */
4055static void
4056_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4057{
4058 int i;
4059 Mpi2ConfigReply_t mpi_reply;
4060 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4061 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4062 Mpi2SasPhyPage0_t phy_pg0;
4063 Mpi2SasDevicePage0_t sas_device_pg0;
4064 Mpi2SasEnclosurePage0_t enclosure_pg0;
4065 u16 ioc_status;
4066 u16 sz;
4067 u16 device_missing_delay;
4068
4069 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4070 if (!ioc->sas_hba.num_phys) {
4071 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4072 ioc->name, __FILE__, __LINE__, __func__);
4073 return;
4074 }
4075
4076 /* sas_iounit page 0 */
4077 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4078 sizeof(Mpi2SasIOUnit0PhyData_t));
4079 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4080 if (!sas_iounit_pg0) {
4081 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4082 ioc->name, __FILE__, __LINE__, __func__);
4083 return;
4084 }
4085 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4086 sas_iounit_pg0, sz))) {
4087 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4088 ioc->name, __FILE__, __LINE__, __func__);
4089 goto out;
4090 }
4091 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4092 MPI2_IOCSTATUS_MASK;
4093 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4094 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4095 ioc->name, __FILE__, __LINE__, __func__);
4096 goto out;
4097 }
4098
4099 /* sas_iounit page 1 */
4100 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4101 sizeof(Mpi2SasIOUnit1PhyData_t));
4102 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4103 if (!sas_iounit_pg1) {
4104 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4105 ioc->name, __FILE__, __LINE__, __func__);
4106 goto out;
4107 }
4108 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4109 sas_iounit_pg1, sz))) {
4110 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4111 ioc->name, __FILE__, __LINE__, __func__);
4112 goto out;
4113 }
4114 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4115 MPI2_IOCSTATUS_MASK;
4116 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4117 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4118 ioc->name, __FILE__, __LINE__, __func__);
4119 goto out;
4120 }
4121
4122 ioc->io_missing_delay =
4123 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4124 device_missing_delay =
4125 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4126 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4127 ioc->device_missing_delay = (device_missing_delay &
4128 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4129 else
4130 ioc->device_missing_delay = device_missing_delay &
4131 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4132
4133 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4134 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4135 sizeof(struct _sas_phy), GFP_KERNEL);
4136 if (!ioc->sas_hba.phy) {
4137 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4138 ioc->name, __FILE__, __LINE__, __func__);
4139 goto out;
4140 }
4141 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4142 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4143 i))) {
4144 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4145 ioc->name, __FILE__, __LINE__, __func__);
4146 goto out;
4147 }
4148 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4149 MPI2_IOCSTATUS_MASK;
4150 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4152 ioc->name, __FILE__, __LINE__, __func__);
4153 goto out;
4154 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304155
4156 if (i == 0)
4157 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4158 PhyData[0].ControllerDevHandle);
4159 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004160 ioc->sas_hba.phy[i].phy_id = i;
4161 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4162 phy_pg0, ioc->sas_hba.parent_dev);
4163 }
4164 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304165 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004166 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4167 ioc->name, __FILE__, __LINE__, __func__);
4168 goto out;
4169 }
Eric Moore635374e2009-03-09 01:21:12 -06004170 ioc->sas_hba.enclosure_handle =
4171 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4172 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4173 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4174 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4175 (unsigned long long) ioc->sas_hba.sas_address,
4176 ioc->sas_hba.num_phys) ;
4177
4178 if (ioc->sas_hba.enclosure_handle) {
4179 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4180 &enclosure_pg0,
4181 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4182 ioc->sas_hba.enclosure_handle))) {
4183 ioc->sas_hba.enclosure_logical_id =
4184 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4185 }
4186 }
4187
4188 out:
4189 kfree(sas_iounit_pg1);
4190 kfree(sas_iounit_pg0);
4191}
4192
4193/**
4194 * _scsih_expander_add - creating expander object
4195 * @ioc: per adapter object
4196 * @handle: expander handle
4197 *
4198 * Creating expander object, stored in ioc->sas_expander_list.
4199 *
4200 * Return 0 for success, else error.
4201 */
4202static int
4203_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4204{
4205 struct _sas_node *sas_expander;
4206 Mpi2ConfigReply_t mpi_reply;
4207 Mpi2ExpanderPage0_t expander_pg0;
4208 Mpi2ExpanderPage1_t expander_pg1;
4209 Mpi2SasEnclosurePage0_t enclosure_pg0;
4210 u32 ioc_status;
4211 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304212 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004213 int i;
4214 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304215 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004216 int rc = 0;
4217
4218 if (!handle)
4219 return -1;
4220
Eric Moore3cb54692010-07-08 14:44:34 -06004221 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304222 return -1;
4223
Eric Moore635374e2009-03-09 01:21:12 -06004224 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4225 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4226 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4227 ioc->name, __FILE__, __LINE__, __func__);
4228 return -1;
4229 }
4230
4231 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4232 MPI2_IOCSTATUS_MASK;
4233 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4234 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4235 ioc->name, __FILE__, __LINE__, __func__);
4236 return -1;
4237 }
4238
4239 /* handle out of order topology events */
4240 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304241 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4242 != 0) {
4243 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4244 ioc->name, __FILE__, __LINE__, __func__);
4245 return -1;
4246 }
4247 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004248 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304249 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4250 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004251 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4252 if (!sas_expander) {
4253 rc = _scsih_expander_add(ioc, parent_handle);
4254 if (rc != 0)
4255 return rc;
4256 }
4257 }
4258
Eric Moore635374e2009-03-09 01:21:12 -06004259 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304260 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004261 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4262 sas_address);
4263 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4264
4265 if (sas_expander)
4266 return 0;
4267
4268 sas_expander = kzalloc(sizeof(struct _sas_node),
4269 GFP_KERNEL);
4270 if (!sas_expander) {
4271 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4272 ioc->name, __FILE__, __LINE__, __func__);
4273 return -1;
4274 }
4275
4276 sas_expander->handle = handle;
4277 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304278 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004279 sas_expander->sas_address = sas_address;
4280
4281 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4282 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304283 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004284 sas_expander->sas_address, sas_expander->num_phys);
4285
4286 if (!sas_expander->num_phys)
4287 goto out_fail;
4288 sas_expander->phy = kcalloc(sas_expander->num_phys,
4289 sizeof(struct _sas_phy), GFP_KERNEL);
4290 if (!sas_expander->phy) {
4291 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4292 ioc->name, __FILE__, __LINE__, __func__);
4293 rc = -1;
4294 goto out_fail;
4295 }
4296
4297 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4298 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304299 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004300 if (!mpt2sas_port) {
4301 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4302 ioc->name, __FILE__, __LINE__, __func__);
4303 rc = -1;
4304 goto out_fail;
4305 }
4306 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4307
4308 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4309 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4310 &expander_pg1, i, handle))) {
4311 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4312 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304313 rc = -1;
4314 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004315 }
4316 sas_expander->phy[i].handle = handle;
4317 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304318
4319 if ((mpt2sas_transport_add_expander_phy(ioc,
4320 &sas_expander->phy[i], expander_pg1,
4321 sas_expander->parent_dev))) {
4322 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4323 ioc->name, __FILE__, __LINE__, __func__);
4324 rc = -1;
4325 goto out_fail;
4326 }
Eric Moore635374e2009-03-09 01:21:12 -06004327 }
4328
4329 if (sas_expander->enclosure_handle) {
4330 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4331 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4332 sas_expander->enclosure_handle))) {
4333 sas_expander->enclosure_logical_id =
4334 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4335 }
4336 }
4337
4338 _scsih_expander_node_add(ioc, sas_expander);
4339 return 0;
4340
4341 out_fail:
4342
Kashyap, Desai20f58952009-08-07 19:34:26 +05304343 if (mpt2sas_port)
4344 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304345 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004346 kfree(sas_expander);
4347 return rc;
4348}
4349
4350/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304351 * _scsih_done - scsih callback handler.
4352 * @ioc: per adapter object
4353 * @smid: system request message index
4354 * @msix_index: MSIX table index supplied by the OS
4355 * @reply: reply message frame(lower 32bit addr)
4356 *
4357 * Callback handler when sending internal generated message frames.
4358 * The callback index passed is `ioc->scsih_cb_idx`
4359 *
4360 * Return 1 meaning mf should be freed from _base_interrupt
4361 * 0 means the mf is freed from this function.
4362 */
4363static u8
4364_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4365{
4366 MPI2DefaultReply_t *mpi_reply;
4367
4368 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4369 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4370 return 1;
4371 if (ioc->scsih_cmds.smid != smid)
4372 return 1;
4373 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4374 if (mpi_reply) {
4375 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4376 mpi_reply->MsgLength*4);
4377 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4378 }
4379 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4380 complete(&ioc->scsih_cmds.done);
4381 return 1;
4382}
4383
4384/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304385 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004386 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304387 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004388 *
4389 * Return nothing.
4390 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304391void
4392mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004393{
4394 struct _sas_node *sas_expander;
4395 unsigned long flags;
4396
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304397 if (ioc->shost_recovery)
4398 return;
4399
Eric Moore635374e2009-03-09 01:21:12 -06004400 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304401 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4402 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304403 if (!sas_expander) {
4404 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4405 return;
4406 }
4407 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004408 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4409 _scsih_expander_node_remove(ioc, sas_expander);
4410}
4411
4412/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304413 * _scsih_check_access_status - check access flags
4414 * @ioc: per adapter object
4415 * @sas_address: sas address
4416 * @handle: sas device handle
4417 * @access_flags: errors returned during discovery of the device
4418 *
4419 * Return 0 for success, else failure
4420 */
4421static u8
4422_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4423 u16 handle, u8 access_status)
4424{
4425 u8 rc = 1;
4426 char *desc = NULL;
4427
4428 switch (access_status) {
4429 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4430 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4431 rc = 0;
4432 break;
4433 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4434 desc = "sata capability failed";
4435 break;
4436 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4437 desc = "sata affiliation conflict";
4438 break;
4439 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4440 desc = "route not addressable";
4441 break;
4442 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4443 desc = "smp error not addressable";
4444 break;
4445 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4446 desc = "device blocked";
4447 break;
4448 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4449 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4450 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4451 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4452 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4453 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4454 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4455 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4456 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4457 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4458 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4459 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4460 desc = "sata initialization failed";
4461 break;
4462 default:
4463 desc = "unknown";
4464 break;
4465 }
4466
4467 if (!rc)
4468 return 0;
4469
4470 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4471 "handle(0x%04x)\n", ioc->name, desc,
4472 (unsigned long long)sas_address, handle);
4473 return rc;
4474}
4475
4476static void
4477_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4478{
4479 Mpi2ConfigReply_t mpi_reply;
4480 Mpi2SasDevicePage0_t sas_device_pg0;
4481 struct _sas_device *sas_device;
4482 u32 ioc_status;
4483 unsigned long flags;
4484 u64 sas_address;
4485 struct scsi_target *starget;
4486 struct MPT2SAS_TARGET *sas_target_priv_data;
4487 u32 device_info;
4488
4489 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4490 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4491 return;
4492
4493 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4494 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4495 return;
4496
4497 /* check if this is end device */
4498 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4499 if (!(_scsih_is_end_device(device_info)))
4500 return;
4501
4502 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4503 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4504 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4505 sas_address);
4506
4507 if (!sas_device) {
4508 printk(MPT2SAS_ERR_FMT "device is not present "
4509 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4510 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4511 return;
4512 }
4513
4514 if (unlikely(sas_device->handle != handle)) {
4515 starget = sas_device->starget;
4516 sas_target_priv_data = starget->hostdata;
4517 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4518 " to (0x%04x)!!!\n", sas_device->handle, handle);
4519 sas_target_priv_data->handle = handle;
4520 sas_device->handle = handle;
4521 }
4522 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4523
4524 /* check if device is present */
4525 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4526 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4527 printk(MPT2SAS_ERR_FMT "device is not present "
4528 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4529 return;
4530 }
4531
4532 /* check if there were any issues with discovery */
4533 if (_scsih_check_access_status(ioc, sas_address, handle,
4534 sas_device_pg0.AccessStatus))
4535 return;
4536 _scsih_ublock_io_device(ioc, handle);
4537
4538}
4539
4540/**
Eric Moore635374e2009-03-09 01:21:12 -06004541 * _scsih_add_device - creating sas device object
4542 * @ioc: per adapter object
4543 * @handle: sas device handle
4544 * @phy_num: phy number end device attached to
4545 * @is_pd: is this hidden raid component
4546 *
4547 * Creating end device object, stored in ioc->sas_device_list.
4548 *
4549 * Returns 0 for success, non-zero for failure.
4550 */
4551static int
4552_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4553{
4554 Mpi2ConfigReply_t mpi_reply;
4555 Mpi2SasDevicePage0_t sas_device_pg0;
4556 Mpi2SasEnclosurePage0_t enclosure_pg0;
4557 struct _sas_device *sas_device;
4558 u32 ioc_status;
4559 __le64 sas_address;
4560 u32 device_info;
4561 unsigned long flags;
4562
4563 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4564 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4565 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4566 ioc->name, __FILE__, __LINE__, __func__);
4567 return -1;
4568 }
4569
4570 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4571 MPI2_IOCSTATUS_MASK;
4572 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4573 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4574 ioc->name, __FILE__, __LINE__, __func__);
4575 return -1;
4576 }
4577
Kashyap, Desaib4344272010-03-17 16:24:14 +05304578 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4579
Eric Moore635374e2009-03-09 01:21:12 -06004580 /* check if device is present */
4581 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4582 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4583 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4584 ioc->name, __FILE__, __LINE__, __func__);
4585 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4586 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4587 return -1;
4588 }
4589
Kashyap, Desaib4344272010-03-17 16:24:14 +05304590 /* check if there were any issues with discovery */
4591 if (_scsih_check_access_status(ioc, sas_address, handle,
4592 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06004593 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06004594
4595 /* check if this is end device */
4596 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4597 if (!(_scsih_is_end_device(device_info))) {
4598 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4599 ioc->name, __FILE__, __LINE__, __func__);
4600 return -1;
4601 }
4602
Eric Moore635374e2009-03-09 01:21:12 -06004603
4604 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4605 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4606 sas_address);
4607 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4608
Kashyap, Desaib4344272010-03-17 16:24:14 +05304609 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004610 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06004611
4612 sas_device = kzalloc(sizeof(struct _sas_device),
4613 GFP_KERNEL);
4614 if (!sas_device) {
4615 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4616 ioc->name, __FILE__, __LINE__, __func__);
4617 return -1;
4618 }
4619
4620 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304621 if (_scsih_get_sas_address(ioc, le16_to_cpu
4622 (sas_device_pg0.ParentDevHandle),
4623 &sas_device->sas_address_parent) != 0)
4624 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4625 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004626 sas_device->enclosure_handle =
4627 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4628 sas_device->slot =
4629 le16_to_cpu(sas_device_pg0.Slot);
4630 sas_device->device_info = device_info;
4631 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304632 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06004633
4634 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304635 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4636 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4637 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004638 sas_device->enclosure_logical_id =
4639 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004640
4641 /* get device name */
4642 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4643
4644 if (ioc->wait_for_port_enable_to_complete)
4645 _scsih_sas_device_init_add(ioc, sas_device);
4646 else
4647 _scsih_sas_device_add(ioc, sas_device);
4648
4649 return 0;
4650}
4651
4652/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05304653 * _scsih_remove_device - removing sas device object
4654 * @ioc: per adapter object
4655 * @sas_device_delete: the sas_device object
4656 *
4657 * Return nothing.
4658 */
4659static void
4660_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4661 struct _sas_device *sas_device)
4662{
4663 struct _sas_device sas_device_backup;
4664 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304665
Kashyap, Desai1278b112010-03-09 17:34:13 +05304666 if (!sas_device)
4667 return;
Eric Moore635374e2009-03-09 01:21:12 -06004668
Kashyap, Desai1278b112010-03-09 17:34:13 +05304669 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06004670 _scsih_sas_device_remove(ioc, sas_device);
4671
Kashyap, Desai1278b112010-03-09 17:34:13 +05304672 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4673 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4674 sas_device_backup.handle, (unsigned long long)
4675 sas_device_backup.sas_address));
4676
4677 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4678 sas_target_priv_data = sas_device_backup.starget->hostdata;
4679 sas_target_priv_data->deleted = 1;
4680 }
4681
Kashyap, Desai1278b112010-03-09 17:34:13 +05304682 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4683
4684 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4685 sas_device_backup.sas_address_parent);
4686
4687 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4688 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4689 (unsigned long long) sas_device_backup.sas_address);
4690
4691 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4692 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4693 sas_device_backup.handle, (unsigned long long)
4694 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004695}
4696
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304697/**
4698 * mpt2sas_device_remove - removing device object
4699 * @ioc: per adapter object
4700 * @sas_address: expander sas_address
4701 *
4702 * Return nothing.
4703 */
4704void
4705mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4706{
4707 struct _sas_device *sas_device;
4708 unsigned long flags;
4709
4710 if (ioc->shost_recovery)
4711 return;
4712
4713 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4714 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4715 sas_address);
4716 if (!sas_device) {
4717 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4718 return;
4719 }
4720 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4721 _scsih_remove_device(ioc, sas_device);
4722}
4723
Eric Moore635374e2009-03-09 01:21:12 -06004724#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4725/**
4726 * _scsih_sas_topology_change_event_debug - debug for topology event
4727 * @ioc: per adapter object
4728 * @event_data: event data payload
4729 * Context: user.
4730 */
4731static void
4732_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4733 Mpi2EventDataSasTopologyChangeList_t *event_data)
4734{
4735 int i;
4736 u16 handle;
4737 u16 reason_code;
4738 u8 phy_number;
4739 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304740 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004741
4742 switch (event_data->ExpStatus) {
4743 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4744 status_str = "add";
4745 break;
4746 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4747 status_str = "remove";
4748 break;
4749 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304750 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004751 status_str = "responding";
4752 break;
4753 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4754 status_str = "remove delay";
4755 break;
4756 default:
4757 status_str = "unknown status";
4758 break;
4759 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304760 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06004761 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304762 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06004763 "start_phy(%02d), count(%d)\n",
4764 le16_to_cpu(event_data->ExpanderDevHandle),
4765 le16_to_cpu(event_data->EnclosureHandle),
4766 event_data->StartPhyNum, event_data->NumEntries);
4767 for (i = 0; i < event_data->NumEntries; i++) {
4768 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4769 if (!handle)
4770 continue;
4771 phy_number = event_data->StartPhyNum + i;
4772 reason_code = event_data->PHY[i].PhyStatus &
4773 MPI2_EVENT_SAS_TOPO_RC_MASK;
4774 switch (reason_code) {
4775 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304776 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004777 break;
4778 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304779 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004780 break;
4781 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304782 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004783 break;
4784 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304785 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004786 break;
4787 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304788 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004789 break;
4790 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304791 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004792 break;
4793 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304794 link_rate = event_data->PHY[i].LinkRate >> 4;
4795 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304796 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304797 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4798 handle, status_str, link_rate, prev_link_rate);
4799
Eric Moore635374e2009-03-09 01:21:12 -06004800 }
4801}
4802#endif
4803
4804/**
4805 * _scsih_sas_topology_change_event - handle topology changes
4806 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304807 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004808 * Context: user.
4809 *
4810 */
4811static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304812_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004813 struct fw_event_work *fw_event)
4814{
4815 int i;
4816 u16 parent_handle, handle;
4817 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304818 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06004819 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304820 struct _sas_device *sas_device;
4821 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004822 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304823 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304824 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004825
4826#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4827 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4828 _scsih_sas_topology_change_event_debug(ioc, event_data);
4829#endif
4830
Eric Moore3cb54692010-07-08 14:44:34 -06004831 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304832 return;
4833
Eric Moore635374e2009-03-09 01:21:12 -06004834 if (!ioc->sas_hba.num_phys)
4835 _scsih_sas_host_add(ioc);
4836 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304837 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004838
4839 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304840 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06004841 "event\n", ioc->name));
4842 return;
4843 }
4844
4845 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4846
4847 /* handle expander add */
4848 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4849 if (_scsih_expander_add(ioc, parent_handle) != 0)
4850 return;
4851
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304852 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4853 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4854 parent_handle);
4855 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304856 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304857 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304858 max_phys = sas_expander->num_phys;
4859 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304860 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304861 max_phys = ioc->sas_hba.num_phys;
4862 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304863 return;
4864
Eric Moore635374e2009-03-09 01:21:12 -06004865 /* handle siblings events */
4866 for (i = 0; i < event_data->NumEntries; i++) {
4867 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304868 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06004869 "expander event\n", ioc->name));
4870 return;
4871 }
Eric Moore3cb54692010-07-08 14:44:34 -06004872 if (ioc->shost_recovery || ioc->remove_host ||
4873 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304874 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304875 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304876 if (phy_number >= max_phys)
4877 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304878 reason_code = event_data->PHY[i].PhyStatus &
4879 MPI2_EVENT_SAS_TOPO_RC_MASK;
4880 if ((event_data->PHY[i].PhyStatus &
4881 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4882 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004883 continue;
4884 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4885 if (!handle)
4886 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304887 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304888 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004889 switch (reason_code) {
4890 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304891
4892 if (link_rate == prev_link_rate)
4893 break;
4894
4895 mpt2sas_transport_update_links(ioc, sas_address,
4896 handle, phy_number, link_rate);
4897
Kashyap, Desaib4344272010-03-17 16:24:14 +05304898 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4899 break;
4900
4901 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304902 break;
Eric Moore635374e2009-03-09 01:21:12 -06004903 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304904
4905 mpt2sas_transport_update_links(ioc, sas_address,
4906 handle, phy_number, link_rate);
4907
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304908 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004909 break;
4910 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304911
4912 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4913 sas_device = _scsih_sas_device_find_by_handle(ioc,
4914 handle);
4915 if (!sas_device) {
4916 spin_unlock_irqrestore(&ioc->sas_device_lock,
4917 flags);
4918 break;
4919 }
4920 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4921 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004922 break;
4923 }
4924 }
4925
4926 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304927 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4928 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304929 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004930
4931}
4932
4933#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4934/**
4935 * _scsih_sas_device_status_change_event_debug - debug for device event
4936 * @event_data: event data payload
4937 * Context: user.
4938 *
4939 * Return nothing.
4940 */
4941static void
4942_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4943 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4944{
4945 char *reason_str = NULL;
4946
4947 switch (event_data->ReasonCode) {
4948 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4949 reason_str = "smart data";
4950 break;
4951 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4952 reason_str = "unsupported device discovered";
4953 break;
4954 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4955 reason_str = "internal device reset";
4956 break;
4957 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4958 reason_str = "internal task abort";
4959 break;
4960 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4961 reason_str = "internal task abort set";
4962 break;
4963 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4964 reason_str = "internal clear task set";
4965 break;
4966 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4967 reason_str = "internal query task";
4968 break;
4969 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4970 reason_str = "sata init failure";
4971 break;
4972 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4973 reason_str = "internal device reset complete";
4974 break;
4975 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4976 reason_str = "internal task abort complete";
4977 break;
4978 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4979 reason_str = "internal async notification";
4980 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304981 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4982 reason_str = "expander reduced functionality";
4983 break;
4984 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4985 reason_str = "expander reduced functionality complete";
4986 break;
Eric Moore635374e2009-03-09 01:21:12 -06004987 default:
4988 reason_str = "unknown reason";
4989 break;
4990 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304991 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06004992 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4993 reason_str, le16_to_cpu(event_data->DevHandle),
4994 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4995 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304996 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004997 event_data->ASC, event_data->ASCQ);
4998 printk(KERN_INFO "\n");
4999}
5000#endif
5001
5002/**
5003 * _scsih_sas_device_status_change_event - handle device status change
5004 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305005 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005006 * Context: user.
5007 *
5008 * Return nothing.
5009 */
5010static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305011_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5012 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005013{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305014 struct MPT2SAS_TARGET *target_priv_data;
5015 struct _sas_device *sas_device;
5016 __le64 sas_address;
5017 unsigned long flags;
5018 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5019 fw_event->event_data;
5020
Eric Moore635374e2009-03-09 01:21:12 -06005021#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5022 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305023 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305024 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005025#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305026
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305027 /* In MPI Revision K (0xC), the internal device reset complete was
5028 * implemented, so avoid setting tm_busy flag for older firmware.
5029 */
5030 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5031 return;
5032
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305033 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305034 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305035 event_data->ReasonCode !=
5036 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305037 return;
5038
5039 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5040 sas_address = le64_to_cpu(event_data->SASAddress);
5041 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5042 sas_address);
5043 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5044
5045 if (!sas_device || !sas_device->starget)
5046 return;
5047
5048 target_priv_data = sas_device->starget->hostdata;
5049 if (!target_priv_data)
5050 return;
5051
5052 if (event_data->ReasonCode ==
5053 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5054 target_priv_data->tm_busy = 1;
5055 else
5056 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005057}
5058
5059#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5060/**
5061 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5062 * @ioc: per adapter object
5063 * @event_data: event data payload
5064 * Context: user.
5065 *
5066 * Return nothing.
5067 */
5068static void
5069_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5070 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5071{
5072 char *reason_str = NULL;
5073
5074 switch (event_data->ReasonCode) {
5075 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5076 reason_str = "enclosure add";
5077 break;
5078 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5079 reason_str = "enclosure remove";
5080 break;
5081 default:
5082 reason_str = "unknown reason";
5083 break;
5084 }
5085
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305086 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005087 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5088 " number slots(%d)\n", ioc->name, reason_str,
5089 le16_to_cpu(event_data->EnclosureHandle),
5090 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5091 le16_to_cpu(event_data->StartSlot));
5092}
5093#endif
5094
5095/**
5096 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5097 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305098 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005099 * Context: user.
5100 *
5101 * Return nothing.
5102 */
5103static void
5104_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305105 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005106{
5107#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5108 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5109 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305110 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005111#endif
5112}
5113
5114/**
5115 * _scsih_sas_broadcast_primative_event - handle broadcast events
5116 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305117 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005118 * Context: user.
5119 *
5120 * Return nothing.
5121 */
5122static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305123_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5124 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005125{
5126 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305127 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005128 u16 smid, handle;
5129 u32 lun;
5130 struct MPT2SAS_DEVICE *sas_device_priv_data;
5131 u32 termination_count;
5132 u32 query_count;
5133 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305134#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5135 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5136#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05305137 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305138 unsigned long flags;
5139 int r;
5140
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305141 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
Eric Moore635374e2009-03-09 01:21:12 -06005142 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5143 event_data->PortWidth));
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305144 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005145 __func__));
5146
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305147 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5148 ioc->broadcast_aen_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005149 termination_count = 0;
5150 query_count = 0;
5151 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305152 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06005153 scmd = _scsih_scsi_lookup_get(ioc, smid);
5154 if (!scmd)
5155 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305156 sdev = scmd->device;
5157 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005158 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5159 continue;
5160 /* skip hidden raid components */
5161 if (sas_device_priv_data->sas_target->flags &
5162 MPT_TARGET_FLAGS_RAID_COMPONENT)
5163 continue;
5164 /* skip volumes */
5165 if (sas_device_priv_data->sas_target->flags &
5166 MPT_TARGET_FLAGS_VOLUME)
5167 continue;
5168
5169 handle = sas_device_priv_data->sas_target->handle;
5170 lun = sas_device_priv_data->lun;
5171 query_count++;
5172
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305173 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305174 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5175 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
Eric Moore8901cbb2009-04-21 15:41:32 -06005176 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305177 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5178 & MPI2_IOCSTATUS_MASK;
5179 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06005180 (mpi_reply->ResponseCode ==
5181 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5182 mpi_reply->ResponseCode ==
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305183 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC)) {
5184 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005185 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305186 }
5187 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5188 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5189 scmd);
5190 if (r == FAILED)
5191 sdev_printk(KERN_WARNING, sdev, "task abort: FAILED "
5192 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06005193 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305194 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005195 }
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305196 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005197
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305198 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005199 "%s - exit, query_count = %d termination_count = %d\n",
5200 ioc->name, __func__, query_count, termination_count));
5201}
5202
5203/**
5204 * _scsih_sas_discovery_event - handle discovery events
5205 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305206 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005207 * Context: user.
5208 *
5209 * Return nothing.
5210 */
5211static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305212_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5213 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005214{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305215 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5216
Eric Moore635374e2009-03-09 01:21:12 -06005217#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5218 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305219 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005220 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5221 "start" : "stop");
5222 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305223 printk("discovery_status(0x%08x)",
5224 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005225 printk("\n");
5226 }
5227#endif
5228
5229 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5230 !ioc->sas_hba.num_phys)
5231 _scsih_sas_host_add(ioc);
5232}
5233
5234/**
5235 * _scsih_reprobe_lun - reprobing lun
5236 * @sdev: scsi device struct
5237 * @no_uld_attach: sdev->no_uld_attach flag setting
5238 *
5239 **/
5240static void
5241_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5242{
5243 int rc;
5244
5245 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5246 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5247 sdev->no_uld_attach ? "hidding" : "exposing");
5248 rc = scsi_device_reprobe(sdev);
5249}
5250
5251/**
5252 * _scsih_reprobe_target - reprobing target
5253 * @starget: scsi target struct
5254 * @no_uld_attach: sdev->no_uld_attach flag setting
5255 *
5256 * Note: no_uld_attach flag determines whether the disk device is attached
5257 * to block layer. A value of `1` means to not attach.
5258 **/
5259static void
5260_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5261{
5262 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5263
5264 if (no_uld_attach)
5265 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5266 else
5267 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5268
5269 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5270 _scsih_reprobe_lun);
5271}
5272/**
5273 * _scsih_sas_volume_add - add new volume
5274 * @ioc: per adapter object
5275 * @element: IR config element data
5276 * Context: user.
5277 *
5278 * Return nothing.
5279 */
5280static void
5281_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5282 Mpi2EventIrConfigElement_t *element)
5283{
5284 struct _raid_device *raid_device;
5285 unsigned long flags;
5286 u64 wwid;
5287 u16 handle = le16_to_cpu(element->VolDevHandle);
5288 int rc;
5289
Eric Moore635374e2009-03-09 01:21:12 -06005290 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5291 if (!wwid) {
5292 printk(MPT2SAS_ERR_FMT
5293 "failure at %s:%d/%s()!\n", ioc->name,
5294 __FILE__, __LINE__, __func__);
5295 return;
5296 }
5297
5298 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5299 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5300 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5301
5302 if (raid_device)
5303 return;
5304
5305 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5306 if (!raid_device) {
5307 printk(MPT2SAS_ERR_FMT
5308 "failure at %s:%d/%s()!\n", ioc->name,
5309 __FILE__, __LINE__, __func__);
5310 return;
5311 }
5312
5313 raid_device->id = ioc->sas_id++;
5314 raid_device->channel = RAID_CHANNEL;
5315 raid_device->handle = handle;
5316 raid_device->wwid = wwid;
5317 _scsih_raid_device_add(ioc, raid_device);
5318 if (!ioc->wait_for_port_enable_to_complete) {
5319 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5320 raid_device->id, 0);
5321 if (rc)
5322 _scsih_raid_device_remove(ioc, raid_device);
5323 } else
5324 _scsih_determine_boot_device(ioc, raid_device, 1);
5325}
5326
5327/**
5328 * _scsih_sas_volume_delete - delete volume
5329 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305330 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005331 * Context: user.
5332 *
5333 * Return nothing.
5334 */
5335static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305336_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005337{
5338 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06005339 unsigned long flags;
5340 struct MPT2SAS_TARGET *sas_target_priv_data;
5341
Eric Moore635374e2009-03-09 01:21:12 -06005342 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5343 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5344 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5345 if (!raid_device)
5346 return;
5347 if (raid_device->starget) {
5348 sas_target_priv_data = raid_device->starget->hostdata;
5349 sas_target_priv_data->deleted = 1;
5350 scsi_remove_target(&raid_device->starget->dev);
5351 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305352 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5353 "(0x%016llx)\n", ioc->name, raid_device->handle,
5354 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06005355 _scsih_raid_device_remove(ioc, raid_device);
5356}
5357
5358/**
5359 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5360 * @ioc: per adapter object
5361 * @element: IR config element data
5362 * Context: user.
5363 *
5364 * Return nothing.
5365 */
5366static void
5367_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5368 Mpi2EventIrConfigElement_t *element)
5369{
5370 struct _sas_device *sas_device;
5371 unsigned long flags;
5372 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5373
5374 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5375 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5376 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5377 if (!sas_device)
5378 return;
5379
5380 /* exposing raid component */
5381 sas_device->volume_handle = 0;
5382 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305383 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005384 _scsih_reprobe_target(sas_device->starget, 0);
5385}
5386
5387/**
5388 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5389 * @ioc: per adapter object
5390 * @element: IR config element data
5391 * Context: user.
5392 *
5393 * Return nothing.
5394 */
5395static void
5396_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5397 Mpi2EventIrConfigElement_t *element)
5398{
5399 struct _sas_device *sas_device;
5400 unsigned long flags;
5401 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5402
5403 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5404 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5405 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5406 if (!sas_device)
5407 return;
5408
5409 /* hiding raid component */
5410 mpt2sas_config_get_volume_handle(ioc, handle,
5411 &sas_device->volume_handle);
5412 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5413 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305414 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005415 _scsih_reprobe_target(sas_device->starget, 1);
5416}
5417
5418/**
5419 * _scsih_sas_pd_delete - delete pd component
5420 * @ioc: per adapter object
5421 * @element: IR config element data
5422 * Context: user.
5423 *
5424 * Return nothing.
5425 */
5426static void
5427_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5428 Mpi2EventIrConfigElement_t *element)
5429{
5430 struct _sas_device *sas_device;
5431 unsigned long flags;
5432 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5433
5434 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5435 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5436 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5437 if (!sas_device)
5438 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305439 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005440}
5441
5442/**
5443 * _scsih_sas_pd_add - remove pd component
5444 * @ioc: per adapter object
5445 * @element: IR config element data
5446 * Context: user.
5447 *
5448 * Return nothing.
5449 */
5450static void
5451_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5452 Mpi2EventIrConfigElement_t *element)
5453{
5454 struct _sas_device *sas_device;
5455 unsigned long flags;
5456 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305457 Mpi2ConfigReply_t mpi_reply;
5458 Mpi2SasDevicePage0_t sas_device_pg0;
5459 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305460 u64 sas_address;
5461 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005462
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305463 set_bit(handle, ioc->pd_handles);
5464
Eric Moore635374e2009-03-09 01:21:12 -06005465 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5466 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5467 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305468 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305469 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305470
5471 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5472 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5473 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5474 ioc->name, __FILE__, __LINE__, __func__);
5475 return;
5476 }
5477
5478 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5479 MPI2_IOCSTATUS_MASK;
5480 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5481 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5482 ioc->name, __FILE__, __LINE__, __func__);
5483 return;
5484 }
5485
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305486 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5487 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5488 mpt2sas_transport_update_links(ioc, sas_address, handle,
5489 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305490
5491 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06005492}
5493
5494#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5495/**
5496 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5497 * @ioc: per adapter object
5498 * @event_data: event data payload
5499 * Context: user.
5500 *
5501 * Return nothing.
5502 */
5503static void
5504_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5505 Mpi2EventDataIrConfigChangeList_t *event_data)
5506{
5507 Mpi2EventIrConfigElement_t *element;
5508 u8 element_type;
5509 int i;
5510 char *reason_str = NULL, *element_str = NULL;
5511
5512 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5513
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305514 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005515 ioc->name, (le32_to_cpu(event_data->Flags) &
5516 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5517 "foreign" : "native", event_data->NumElements);
5518 for (i = 0; i < event_data->NumElements; i++, element++) {
5519 switch (element->ReasonCode) {
5520 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5521 reason_str = "add";
5522 break;
5523 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5524 reason_str = "remove";
5525 break;
5526 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5527 reason_str = "no change";
5528 break;
5529 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5530 reason_str = "hide";
5531 break;
5532 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5533 reason_str = "unhide";
5534 break;
5535 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5536 reason_str = "volume_created";
5537 break;
5538 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5539 reason_str = "volume_deleted";
5540 break;
5541 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5542 reason_str = "pd_created";
5543 break;
5544 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5545 reason_str = "pd_deleted";
5546 break;
5547 default:
5548 reason_str = "unknown reason";
5549 break;
5550 }
5551 element_type = le16_to_cpu(element->ElementFlags) &
5552 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5553 switch (element_type) {
5554 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5555 element_str = "volume";
5556 break;
5557 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5558 element_str = "phys disk";
5559 break;
5560 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5561 element_str = "hot spare";
5562 break;
5563 default:
5564 element_str = "unknown element";
5565 break;
5566 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305567 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005568 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5569 reason_str, le16_to_cpu(element->VolDevHandle),
5570 le16_to_cpu(element->PhysDiskDevHandle),
5571 element->PhysDiskNum);
5572 }
5573}
5574#endif
5575
5576/**
5577 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5578 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305579 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005580 * Context: user.
5581 *
5582 * Return nothing.
5583 */
5584static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305585_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5586 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005587{
5588 Mpi2EventIrConfigElement_t *element;
5589 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305590 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305591 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005592
5593#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5594 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5595 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5596
5597#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305598 foreign_config = (le32_to_cpu(event_data->Flags) &
5599 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005600
5601 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5602 for (i = 0; i < event_data->NumElements; i++, element++) {
5603
5604 switch (element->ReasonCode) {
5605 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5606 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305607 if (!foreign_config)
5608 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005609 break;
5610 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5611 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305612 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305613 _scsih_sas_volume_delete(ioc,
5614 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06005615 break;
5616 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5617 _scsih_sas_pd_hide(ioc, element);
5618 break;
5619 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5620 _scsih_sas_pd_expose(ioc, element);
5621 break;
5622 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5623 _scsih_sas_pd_add(ioc, element);
5624 break;
5625 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5626 _scsih_sas_pd_delete(ioc, element);
5627 break;
5628 }
5629 }
5630}
5631
5632/**
5633 * _scsih_sas_ir_volume_event - IR volume event
5634 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305635 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005636 * Context: user.
5637 *
5638 * Return nothing.
5639 */
5640static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305641_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5642 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005643{
5644 u64 wwid;
5645 unsigned long flags;
5646 struct _raid_device *raid_device;
5647 u16 handle;
5648 u32 state;
5649 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305650 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005651
5652 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5653 return;
5654
5655 handle = le16_to_cpu(event_data->VolDevHandle);
5656 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305657 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005658 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5659 le32_to_cpu(event_data->PreviousValue), state));
5660
Eric Moore635374e2009-03-09 01:21:12 -06005661 switch (state) {
5662 case MPI2_RAID_VOL_STATE_MISSING:
5663 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305664 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005665 break;
5666
5667 case MPI2_RAID_VOL_STATE_ONLINE:
5668 case MPI2_RAID_VOL_STATE_DEGRADED:
5669 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305670
5671 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5672 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5673 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5674
Eric Moore635374e2009-03-09 01:21:12 -06005675 if (raid_device)
5676 break;
5677
5678 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5679 if (!wwid) {
5680 printk(MPT2SAS_ERR_FMT
5681 "failure at %s:%d/%s()!\n", ioc->name,
5682 __FILE__, __LINE__, __func__);
5683 break;
5684 }
5685
5686 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5687 if (!raid_device) {
5688 printk(MPT2SAS_ERR_FMT
5689 "failure at %s:%d/%s()!\n", ioc->name,
5690 __FILE__, __LINE__, __func__);
5691 break;
5692 }
5693
5694 raid_device->id = ioc->sas_id++;
5695 raid_device->channel = RAID_CHANNEL;
5696 raid_device->handle = handle;
5697 raid_device->wwid = wwid;
5698 _scsih_raid_device_add(ioc, raid_device);
5699 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5700 raid_device->id, 0);
5701 if (rc)
5702 _scsih_raid_device_remove(ioc, raid_device);
5703 break;
5704
5705 case MPI2_RAID_VOL_STATE_INITIALIZING:
5706 default:
5707 break;
5708 }
5709}
5710
5711/**
5712 * _scsih_sas_ir_physical_disk_event - PD event
5713 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305714 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005715 * Context: user.
5716 *
5717 * Return nothing.
5718 */
5719static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305720_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5721 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005722{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305723 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005724 u32 state;
5725 struct _sas_device *sas_device;
5726 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305727 Mpi2ConfigReply_t mpi_reply;
5728 Mpi2SasDevicePage0_t sas_device_pg0;
5729 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305730 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305731 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005732
5733 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5734 return;
5735
5736 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5737 state = le32_to_cpu(event_data->NewValue);
5738
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305739 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005740 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5741 le32_to_cpu(event_data->PreviousValue), state));
5742
Eric Moore635374e2009-03-09 01:21:12 -06005743 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005744 case MPI2_RAID_PD_STATE_ONLINE:
5745 case MPI2_RAID_PD_STATE_DEGRADED:
5746 case MPI2_RAID_PD_STATE_REBUILDING:
5747 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305748 case MPI2_RAID_PD_STATE_HOT_SPARE:
5749
5750 set_bit(handle, ioc->pd_handles);
5751
5752 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5753 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5754 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5755
5756 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305757 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305758
5759 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5760 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5761 handle))) {
5762 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5763 ioc->name, __FILE__, __LINE__, __func__);
5764 return;
5765 }
5766
5767 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5768 MPI2_IOCSTATUS_MASK;
5769 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5770 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5771 ioc->name, __FILE__, __LINE__, __func__);
5772 return;
5773 }
5774
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305775 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5776 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5777 mpt2sas_transport_update_links(ioc, sas_address, handle,
5778 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305779
5780 _scsih_add_device(ioc, handle, 0, 1);
5781
Eric Moore635374e2009-03-09 01:21:12 -06005782 break;
5783
Kashyap, Desai62727a72009-08-07 19:35:18 +05305784 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005785 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5786 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06005787 default:
5788 break;
5789 }
5790}
5791
5792#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5793/**
5794 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5795 * @ioc: per adapter object
5796 * @event_data: event data payload
5797 * Context: user.
5798 *
5799 * Return nothing.
5800 */
5801static void
5802_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5803 Mpi2EventDataIrOperationStatus_t *event_data)
5804{
5805 char *reason_str = NULL;
5806
5807 switch (event_data->RAIDOperation) {
5808 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5809 reason_str = "resync";
5810 break;
5811 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5812 reason_str = "online capacity expansion";
5813 break;
5814 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5815 reason_str = "consistency check";
5816 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305817 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5818 reason_str = "background init";
5819 break;
5820 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5821 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005822 break;
5823 }
5824
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305825 if (!reason_str)
5826 return;
5827
Eric Moore635374e2009-03-09 01:21:12 -06005828 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5829 "\thandle(0x%04x), percent complete(%d)\n",
5830 ioc->name, reason_str,
5831 le16_to_cpu(event_data->VolDevHandle),
5832 event_data->PercentComplete);
5833}
5834#endif
5835
5836/**
5837 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5838 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305839 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005840 * Context: user.
5841 *
5842 * Return nothing.
5843 */
5844static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305845_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5846 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005847{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305848 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5849 static struct _raid_device *raid_device;
5850 unsigned long flags;
5851 u16 handle;
5852
Eric Moore635374e2009-03-09 01:21:12 -06005853#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5854 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305855 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305856 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005857#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305858
5859 /* code added for raid transport support */
5860 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5861
5862 handle = le16_to_cpu(event_data->VolDevHandle);
5863
5864 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5865 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5866 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5867
5868 if (!raid_device)
5869 return;
5870
5871 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5872 raid_device->percent_complete =
5873 event_data->PercentComplete;
5874 }
Eric Moore635374e2009-03-09 01:21:12 -06005875}
5876
5877/**
Kashyap, Desai14695852010-03-30 10:52:44 +05305878 * _scsih_prep_device_scan - initialize parameters prior to device scan
5879 * @ioc: per adapter object
5880 *
5881 * Set the deleted flag prior to device scan. If the device is found during
5882 * the scan, then we clear the deleted flag.
5883 */
5884static void
5885_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5886{
5887 struct MPT2SAS_DEVICE *sas_device_priv_data;
5888 struct scsi_device *sdev;
5889
5890 shost_for_each_device(sdev, ioc->shost) {
5891 sas_device_priv_data = sdev->hostdata;
5892 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5893 sas_device_priv_data->sas_target->deleted = 1;
5894 }
5895}
5896
5897/**
Eric Moore635374e2009-03-09 01:21:12 -06005898 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5899 * @ioc: per adapter object
5900 * @sas_address: sas address
5901 * @slot: enclosure slot id
5902 * @handle: device handle
5903 *
5904 * After host reset, find out whether devices are still responding.
5905 * Used in _scsi_remove_unresponsive_sas_devices.
5906 *
5907 * Return nothing.
5908 */
5909static void
5910_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5911 u16 slot, u16 handle)
5912{
5913 struct MPT2SAS_TARGET *sas_target_priv_data;
5914 struct scsi_target *starget;
5915 struct _sas_device *sas_device;
5916 unsigned long flags;
5917
5918 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5919 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5920 if (sas_device->sas_address == sas_address &&
5921 sas_device->slot == slot && sas_device->starget) {
5922 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305923 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05305924 if (starget && starget->hostdata) {
5925 sas_target_priv_data = starget->hostdata;
5926 sas_target_priv_data->tm_busy = 0;
5927 sas_target_priv_data->deleted = 0;
5928 } else
5929 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005930 starget_printk(KERN_INFO, sas_device->starget,
5931 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5932 "logical id(0x%016llx), slot(%d)\n", handle,
5933 (unsigned long long)sas_device->sas_address,
5934 (unsigned long long)
5935 sas_device->enclosure_logical_id,
5936 sas_device->slot);
5937 if (sas_device->handle == handle)
5938 goto out;
5939 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5940 sas_device->handle);
5941 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305942 if (sas_target_priv_data)
5943 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005944 goto out;
5945 }
5946 }
5947 out:
5948 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5949}
5950
5951/**
5952 * _scsih_search_responding_sas_devices -
5953 * @ioc: per adapter object
5954 *
5955 * After host reset, find out whether devices are still responding.
5956 * If not remove.
5957 *
5958 * Return nothing.
5959 */
5960static void
5961_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5962{
5963 Mpi2SasDevicePage0_t sas_device_pg0;
5964 Mpi2ConfigReply_t mpi_reply;
5965 u16 ioc_status;
5966 __le64 sas_address;
5967 u16 handle;
5968 u32 device_info;
5969 u16 slot;
5970
5971 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5972
5973 if (list_empty(&ioc->sas_device_list))
5974 return;
5975
5976 handle = 0xFFFF;
5977 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5978 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5979 handle))) {
5980 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5981 MPI2_IOCSTATUS_MASK;
5982 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5983 break;
5984 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5985 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5986 if (!(_scsih_is_end_device(device_info)))
5987 continue;
5988 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5989 slot = le16_to_cpu(sas_device_pg0.Slot);
5990 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5991 handle);
5992 }
5993}
5994
5995/**
5996 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5997 * @ioc: per adapter object
5998 * @wwid: world wide identifier for raid volume
5999 * @handle: device handle
6000 *
6001 * After host reset, find out whether devices are still responding.
6002 * Used in _scsi_remove_unresponsive_raid_devices.
6003 *
6004 * Return nothing.
6005 */
6006static void
6007_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6008 u16 handle)
6009{
6010 struct MPT2SAS_TARGET *sas_target_priv_data;
6011 struct scsi_target *starget;
6012 struct _raid_device *raid_device;
6013 unsigned long flags;
6014
6015 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6016 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6017 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306018 starget = raid_device->starget;
6019 if (starget && starget->hostdata) {
6020 sas_target_priv_data = starget->hostdata;
6021 sas_target_priv_data->deleted = 0;
6022 } else
6023 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006024 raid_device->responding = 1;
6025 starget_printk(KERN_INFO, raid_device->starget,
6026 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6027 (unsigned long long)raid_device->wwid);
6028 if (raid_device->handle == handle)
6029 goto out;
6030 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6031 raid_device->handle);
6032 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306033 if (sas_target_priv_data)
6034 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006035 goto out;
6036 }
6037 }
6038 out:
6039 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6040}
6041
6042/**
6043 * _scsih_search_responding_raid_devices -
6044 * @ioc: per adapter object
6045 *
6046 * After host reset, find out whether devices are still responding.
6047 * If not remove.
6048 *
6049 * Return nothing.
6050 */
6051static void
6052_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6053{
6054 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306055 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306056 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006057 Mpi2ConfigReply_t mpi_reply;
6058 u16 ioc_status;
6059 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306060 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006061
6062 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6063
6064 if (list_empty(&ioc->raid_device_list))
6065 return;
6066
6067 handle = 0xFFFF;
6068 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6069 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6070 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6071 MPI2_IOCSTATUS_MASK;
6072 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6073 break;
6074 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306075
6076 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6077 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6078 sizeof(Mpi2RaidVolPage0_t)))
6079 continue;
6080
6081 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6082 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6083 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6084 _scsih_mark_responding_raid_device(ioc,
6085 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006086 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306087
6088 /* refresh the pd_handles */
6089 phys_disk_num = 0xFF;
6090 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6091 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6092 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6093 phys_disk_num))) {
6094 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6095 MPI2_IOCSTATUS_MASK;
6096 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6097 break;
6098 phys_disk_num = pd_pg0.PhysDiskNum;
6099 handle = le16_to_cpu(pd_pg0.DevHandle);
6100 set_bit(handle, ioc->pd_handles);
6101 }
Eric Moore635374e2009-03-09 01:21:12 -06006102}
6103
6104/**
6105 * _scsih_mark_responding_expander - mark a expander as responding
6106 * @ioc: per adapter object
6107 * @sas_address: sas address
6108 * @handle:
6109 *
6110 * After host reset, find out whether devices are still responding.
6111 * Used in _scsi_remove_unresponsive_expanders.
6112 *
6113 * Return nothing.
6114 */
6115static void
6116_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6117 u16 handle)
6118{
6119 struct _sas_node *sas_expander;
6120 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306121 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006122
6123 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6124 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306125 if (sas_expander->sas_address != sas_address)
6126 continue;
6127 sas_expander->responding = 1;
6128 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006129 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306130 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6131 " from(0x%04x) to (0x%04x)!!!\n",
6132 (unsigned long long)sas_expander->sas_address,
6133 sas_expander->handle, handle);
6134 sas_expander->handle = handle;
6135 for (i = 0 ; i < sas_expander->num_phys ; i++)
6136 sas_expander->phy[i].handle = handle;
6137 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006138 }
6139 out:
6140 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6141}
6142
6143/**
6144 * _scsih_search_responding_expanders -
6145 * @ioc: per adapter object
6146 *
6147 * After host reset, find out whether devices are still responding.
6148 * If not remove.
6149 *
6150 * Return nothing.
6151 */
6152static void
6153_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6154{
6155 Mpi2ExpanderPage0_t expander_pg0;
6156 Mpi2ConfigReply_t mpi_reply;
6157 u16 ioc_status;
6158 __le64 sas_address;
6159 u16 handle;
6160
6161 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6162
6163 if (list_empty(&ioc->sas_expander_list))
6164 return;
6165
6166 handle = 0xFFFF;
6167 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6168 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6169
6170 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6171 MPI2_IOCSTATUS_MASK;
6172 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6173 break;
6174
6175 handle = le16_to_cpu(expander_pg0.DevHandle);
6176 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6177 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6178 "sas_addr(0x%016llx)\n", handle,
6179 (unsigned long long)sas_address);
6180 _scsih_mark_responding_expander(ioc, sas_address, handle);
6181 }
6182
6183}
6184
6185/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306186 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006187 * @ioc: per adapter object
6188 *
6189 * Return nothing.
6190 */
6191static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306192_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006193{
6194 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306195 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006196 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006197
Eric Moore635374e2009-03-09 01:21:12 -06006198
6199 list_for_each_entry_safe(sas_device, sas_device_next,
6200 &ioc->sas_device_list, list) {
6201 if (sas_device->responding) {
6202 sas_device->responding = 0;
6203 continue;
6204 }
6205 if (sas_device->starget)
6206 starget_printk(KERN_INFO, sas_device->starget,
6207 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6208 "enclosure logical id(0x%016llx), slot(%d)\n",
6209 sas_device->handle,
6210 (unsigned long long)sas_device->sas_address,
6211 (unsigned long long)
6212 sas_device->enclosure_logical_id,
6213 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306214 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006215 }
6216
6217 list_for_each_entry_safe(raid_device, raid_device_next,
6218 &ioc->raid_device_list, list) {
6219 if (raid_device->responding) {
6220 raid_device->responding = 0;
6221 continue;
6222 }
6223 if (raid_device->starget) {
6224 starget_printk(KERN_INFO, raid_device->starget,
6225 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6226 raid_device->handle,
6227 (unsigned long long)raid_device->wwid);
6228 scsi_remove_target(&raid_device->starget->dev);
6229 }
6230 _scsih_raid_device_remove(ioc, raid_device);
6231 }
6232
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306233 retry_expander_search:
6234 sas_expander = NULL;
6235 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006236 if (sas_expander->responding) {
6237 sas_expander->responding = 0;
6238 continue;
6239 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306240 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306241 goto retry_expander_search;
6242 }
6243}
6244
6245/**
6246 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6247 * @ioc: per adapter object
6248 * @reset_phase: phase
6249 *
6250 * The handler for doing any required cleanup or initialization.
6251 *
6252 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6253 * MPT2_IOC_DONE_RESET
6254 *
6255 * Return nothing.
6256 */
6257void
6258mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6259{
6260 switch (reset_phase) {
6261 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306262 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306263 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306264 break;
6265 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306266 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306267 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306268 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6269 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6270 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6271 complete(&ioc->scsih_cmds.done);
6272 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306273 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6274 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6275 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6276 complete(&ioc->tm_cmds.done);
6277 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306278 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306279 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306280 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306281 break;
6282 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306283 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306284 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306285 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05306286 _scsih_prep_device_scan(ioc);
6287 _scsih_search_responding_sas_devices(ioc);
6288 _scsih_search_responding_raid_devices(ioc);
6289 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306290 break;
Eric Moore635374e2009-03-09 01:21:12 -06006291 }
6292}
6293
6294/**
6295 * _firmware_event_work - delayed task for processing firmware events
6296 * @ioc: per adapter object
6297 * @work: equal to the fw_event_work object
6298 * Context: user.
6299 *
6300 * Return nothing.
6301 */
6302static void
6303_firmware_event_work(struct work_struct *work)
6304{
6305 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306306 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06006307 unsigned long flags;
6308 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6309
Eric Moore635374e2009-03-09 01:21:12 -06006310 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06006311 if (ioc->remove_host || fw_event->cancel_pending_work ||
6312 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06006313 _scsih_fw_event_free(ioc, fw_event);
6314 return;
6315 }
Eric Moore635374e2009-03-09 01:21:12 -06006316
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306317 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6318 _scsih_fw_event_free(ioc, fw_event);
6319 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6320 if (ioc->shost_recovery) {
6321 init_completion(&ioc->shost_recovery_done);
6322 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6323 flags);
6324 wait_for_completion(&ioc->shost_recovery_done);
6325 } else
6326 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6327 flags);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306328 _scsih_remove_unresponding_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006329 return;
6330 }
Eric Moore635374e2009-03-09 01:21:12 -06006331
6332 switch (fw_event->event) {
6333 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306334 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006335 break;
6336 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306337 _scsih_sas_device_status_change_event(ioc,
6338 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006339 break;
6340 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306341 _scsih_sas_discovery_event(ioc,
6342 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006343 break;
6344 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306345 _scsih_sas_broadcast_primative_event(ioc,
6346 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006347 break;
6348 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6349 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306350 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006351 break;
6352 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306353 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006354 break;
6355 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306356 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006357 break;
6358 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306359 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006360 break;
6361 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306362 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006363 break;
Eric Moore635374e2009-03-09 01:21:12 -06006364 }
6365 _scsih_fw_event_free(ioc, fw_event);
6366}
6367
6368/**
6369 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6370 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306371 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06006372 * @reply: reply message frame(lower 32bit addr)
6373 * Context: interrupt.
6374 *
6375 * This function merely adds a new work task into ioc->firmware_event_thread.
6376 * The tasks are worked from _firmware_event_work in user context.
6377 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306378 * Return 1 meaning mf should be freed from _base_interrupt
6379 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006380 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306381u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306382mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6383 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006384{
6385 struct fw_event_work *fw_event;
6386 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006387 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306388 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006389
6390 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06006391 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306392 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006393
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306394 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006395 event = le16_to_cpu(mpi_reply->Event);
6396
6397 switch (event) {
6398 /* handle these */
6399 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6400 {
6401 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6402 (Mpi2EventDataSasBroadcastPrimitive_t *)
6403 mpi_reply->EventData;
6404
6405 if (baen_data->Primitive !=
6406 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6407 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306408 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006409 ioc->broadcast_aen_busy = 1;
6410 break;
6411 }
6412
6413 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6414 _scsih_check_topo_delete_events(ioc,
6415 (Mpi2EventDataSasTopologyChangeList_t *)
6416 mpi_reply->EventData);
6417 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306418 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6419 _scsih_check_ir_config_unhide_events(ioc,
6420 (Mpi2EventDataIrConfigChangeList_t *)
6421 mpi_reply->EventData);
6422 break;
6423 case MPI2_EVENT_IR_VOLUME:
6424 _scsih_check_volume_delete_events(ioc,
6425 (Mpi2EventDataIrVolume_t *)
6426 mpi_reply->EventData);
6427 break;
Eric Moore635374e2009-03-09 01:21:12 -06006428 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6429 case MPI2_EVENT_IR_OPERATION_STATUS:
6430 case MPI2_EVENT_SAS_DISCOVERY:
6431 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06006432 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06006433 break;
6434
6435 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306436 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006437 }
6438
6439 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6440 if (!fw_event) {
6441 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6442 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306443 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006444 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306445 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6446 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06006447 if (!fw_event->event_data) {
6448 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6449 ioc->name, __FILE__, __LINE__, __func__);
6450 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306451 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006452 }
6453
6454 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306455 sz);
Eric Moore635374e2009-03-09 01:21:12 -06006456 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306457 fw_event->VF_ID = mpi_reply->VF_ID;
6458 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06006459 fw_event->event = event;
6460 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306461 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006462}
6463
6464/* shost template */
6465static struct scsi_host_template scsih_driver_template = {
6466 .module = THIS_MODULE,
6467 .name = "Fusion MPT SAS Host",
6468 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06006469 .queuecommand = _scsih_qcmd,
6470 .target_alloc = _scsih_target_alloc,
6471 .slave_alloc = _scsih_slave_alloc,
6472 .slave_configure = _scsih_slave_configure,
6473 .target_destroy = _scsih_target_destroy,
6474 .slave_destroy = _scsih_slave_destroy,
6475 .change_queue_depth = _scsih_change_queue_depth,
6476 .change_queue_type = _scsih_change_queue_type,
6477 .eh_abort_handler = _scsih_abort,
6478 .eh_device_reset_handler = _scsih_dev_reset,
6479 .eh_target_reset_handler = _scsih_target_reset,
6480 .eh_host_reset_handler = _scsih_host_reset,
6481 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06006482 .can_queue = 1,
6483 .this_id = -1,
6484 .sg_tablesize = MPT2SAS_SG_DEPTH,
6485 .max_sectors = 8192,
6486 .cmd_per_lun = 7,
6487 .use_clustering = ENABLE_CLUSTERING,
6488 .shost_attrs = mpt2sas_host_attrs,
6489 .sdev_attrs = mpt2sas_dev_attrs,
6490};
6491
6492/**
6493 * _scsih_expander_node_remove - removing expander device from list.
6494 * @ioc: per adapter object
6495 * @sas_expander: the sas_device object
6496 * Context: Calling function should acquire ioc->sas_node_lock.
6497 *
6498 * Removing object and freeing associated memory from the
6499 * ioc->sas_expander_list.
6500 *
6501 * Return nothing.
6502 */
6503static void
6504_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6505 struct _sas_node *sas_expander)
6506{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306507 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06006508
6509 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306510 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06006511 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306512 if (ioc->shost_recovery)
6513 return;
Eric Moore635374e2009-03-09 01:21:12 -06006514 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306515 SAS_END_DEVICE)
6516 mpt2sas_device_remove(ioc,
6517 mpt2sas_port->remote_identify.sas_address);
6518 else if (mpt2sas_port->remote_identify.device_type ==
6519 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06006520 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306521 SAS_FANOUT_EXPANDER_DEVICE)
6522 mpt2sas_expander_remove(ioc,
6523 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006524 }
6525
6526 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306527 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006528
6529 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6530 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6531 sas_expander->handle, (unsigned long long)
6532 sas_expander->sas_address);
6533
Eric Moore635374e2009-03-09 01:21:12 -06006534 kfree(sas_expander->phy);
6535 kfree(sas_expander);
6536}
6537
6538/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05306539 * _scsih_ir_shutdown - IR shutdown notification
6540 * @ioc: per adapter object
6541 *
6542 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6543 * the host system is shutting down.
6544 *
6545 * Return nothing.
6546 */
6547static void
6548_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6549{
6550 Mpi2RaidActionRequest_t *mpi_request;
6551 Mpi2RaidActionReply_t *mpi_reply;
6552 u16 smid;
6553
6554 /* is IR firmware build loaded ? */
6555 if (!ioc->ir_firmware)
6556 return;
6557
6558 /* are there any volumes ? */
6559 if (list_empty(&ioc->raid_device_list))
6560 return;
6561
6562 mutex_lock(&ioc->scsih_cmds.mutex);
6563
6564 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6565 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6566 ioc->name, __func__);
6567 goto out;
6568 }
6569 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6570
6571 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6572 if (!smid) {
6573 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6574 ioc->name, __func__);
6575 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6576 goto out;
6577 }
6578
6579 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6580 ioc->scsih_cmds.smid = smid;
6581 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6582
6583 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6584 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6585
6586 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6587 init_completion(&ioc->scsih_cmds.done);
6588 mpt2sas_base_put_smid_default(ioc, smid);
6589 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6590
6591 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6592 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6593 ioc->name, __func__);
6594 goto out;
6595 }
6596
6597 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6598 mpi_reply = ioc->scsih_cmds.reply;
6599
6600 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6601 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6602 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6603 le32_to_cpu(mpi_reply->IOCLogInfo));
6604 }
6605
6606 out:
6607 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6608 mutex_unlock(&ioc->scsih_cmds.mutex);
6609}
6610
6611/**
6612 * _scsih_shutdown - routine call during system shutdown
6613 * @pdev: PCI device struct
6614 *
6615 * Return nothing.
6616 */
6617static void
6618_scsih_shutdown(struct pci_dev *pdev)
6619{
6620 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6621 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306622 struct workqueue_struct *wq;
6623 unsigned long flags;
6624
6625 ioc->remove_host = 1;
6626 _scsih_fw_event_cleanup_queue(ioc);
6627
6628 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6629 wq = ioc->firmware_event_thread;
6630 ioc->firmware_event_thread = NULL;
6631 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6632 if (wq)
6633 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306634
6635 _scsih_ir_shutdown(ioc);
6636 mpt2sas_base_detach(ioc);
6637}
6638
6639/**
Eric Moored5d135b2009-05-18 13:02:08 -06006640 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006641 * @pdev: PCI device struct
6642 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306643 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006644 * Return nothing.
6645 */
6646static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006647_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006648{
6649 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6650 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306651 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306652 struct _raid_device *raid_device, *next;
6653 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006654 struct workqueue_struct *wq;
6655 unsigned long flags;
6656
6657 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306658 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006659
6660 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6661 wq = ioc->firmware_event_thread;
6662 ioc->firmware_event_thread = NULL;
6663 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6664 if (wq)
6665 destroy_workqueue(wq);
6666
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306667 /* release all the volumes */
6668 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6669 list) {
6670 if (raid_device->starget) {
6671 sas_target_priv_data =
6672 raid_device->starget->hostdata;
6673 sas_target_priv_data->deleted = 1;
6674 scsi_remove_target(&raid_device->starget->dev);
6675 }
6676 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6677 "(0x%016llx)\n", ioc->name, raid_device->handle,
6678 (unsigned long long) raid_device->wwid);
6679 _scsih_raid_device_remove(ioc, raid_device);
6680 }
6681
Eric Moore635374e2009-03-09 01:21:12 -06006682 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306683 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06006684 &ioc->sas_hba.sas_port_list, port_list) {
6685 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306686 SAS_END_DEVICE)
6687 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06006688 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306689 else if (mpt2sas_port->remote_identify.device_type ==
6690 SAS_EDGE_EXPANDER_DEVICE ||
6691 mpt2sas_port->remote_identify.device_type ==
6692 SAS_FANOUT_EXPANDER_DEVICE)
6693 mpt2sas_expander_remove(ioc,
6694 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006695 }
6696
6697 /* free phys attached to the sas_host */
6698 if (ioc->sas_hba.num_phys) {
6699 kfree(ioc->sas_hba.phy);
6700 ioc->sas_hba.phy = NULL;
6701 ioc->sas_hba.num_phys = 0;
6702 }
6703
6704 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306705 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006706 list_del(&ioc->list);
6707 scsi_remove_host(shost);
6708 scsi_host_put(shost);
6709}
6710
6711/**
6712 * _scsih_probe_boot_devices - reports 1st device
6713 * @ioc: per adapter object
6714 *
6715 * If specified in bios page 2, this routine reports the 1st
6716 * device scsi-ml or sas transport for persistent boot device
6717 * purposes. Please refer to function _scsih_determine_boot_device()
6718 */
6719static void
6720_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6721{
6722 u8 is_raid;
6723 void *device;
6724 struct _sas_device *sas_device;
6725 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306726 u16 handle;
6727 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006728 u64 sas_address;
6729 unsigned long flags;
6730 int rc;
6731
6732 device = NULL;
6733 if (ioc->req_boot_device.device) {
6734 device = ioc->req_boot_device.device;
6735 is_raid = ioc->req_boot_device.is_raid;
6736 } else if (ioc->req_alt_boot_device.device) {
6737 device = ioc->req_alt_boot_device.device;
6738 is_raid = ioc->req_alt_boot_device.is_raid;
6739 } else if (ioc->current_boot_device.device) {
6740 device = ioc->current_boot_device.device;
6741 is_raid = ioc->current_boot_device.is_raid;
6742 }
6743
6744 if (!device)
6745 return;
6746
6747 if (is_raid) {
6748 raid_device = device;
6749 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6750 raid_device->id, 0);
6751 if (rc)
6752 _scsih_raid_device_remove(ioc, raid_device);
6753 } else {
6754 sas_device = device;
6755 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306756 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006757 sas_address = sas_device->sas_address;
6758 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6759 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6760 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6761 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306762 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006763 _scsih_sas_device_remove(ioc, sas_device);
6764 } else if (!sas_device->starget) {
6765 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306766 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006767 _scsih_sas_device_remove(ioc, sas_device);
6768 }
6769 }
6770}
6771
6772/**
6773 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6774 * @ioc: per adapter object
6775 *
6776 * Called during initial loading of the driver.
6777 */
6778static void
6779_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6780{
6781 struct _raid_device *raid_device, *raid_next;
6782 int rc;
6783
6784 list_for_each_entry_safe(raid_device, raid_next,
6785 &ioc->raid_device_list, list) {
6786 if (raid_device->starget)
6787 continue;
6788 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6789 raid_device->id, 0);
6790 if (rc)
6791 _scsih_raid_device_remove(ioc, raid_device);
6792 }
6793}
6794
6795/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306796 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006797 * @ioc: per adapter object
6798 *
6799 * Called during initial loading of the driver.
6800 */
6801static void
6802_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6803{
6804 struct _sas_device *sas_device, *next;
6805 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006806
6807 /* SAS Device List */
6808 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6809 list) {
6810 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6811 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6812 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6813
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306814 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6815 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006816 _scsih_sas_device_remove(ioc, sas_device);
6817 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306818 mpt2sas_transport_port_remove(ioc,
6819 sas_device->sas_address,
6820 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006821 _scsih_sas_device_remove(ioc, sas_device);
6822 }
6823 }
6824}
6825
6826/**
6827 * _scsih_probe_devices - probing for devices
6828 * @ioc: per adapter object
6829 *
6830 * Called during initial loading of the driver.
6831 */
6832static void
6833_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6834{
6835 u16 volume_mapping_flags =
6836 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6837 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6838
6839 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6840 return; /* return when IOC doesn't support initiator mode */
6841
6842 _scsih_probe_boot_devices(ioc);
6843
6844 if (ioc->ir_firmware) {
6845 if ((volume_mapping_flags &
6846 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6847 _scsih_probe_sas(ioc);
6848 _scsih_probe_raid(ioc);
6849 } else {
6850 _scsih_probe_raid(ioc);
6851 _scsih_probe_sas(ioc);
6852 }
6853 } else
6854 _scsih_probe_sas(ioc);
6855}
6856
6857/**
Eric Moored5d135b2009-05-18 13:02:08 -06006858 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006859 * @pdev: PCI device struct
6860 * @id: pci device id
6861 *
6862 * Returns 0 success, anything else error.
6863 */
6864static int
Eric Moored5d135b2009-05-18 13:02:08 -06006865_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006866{
6867 struct MPT2SAS_ADAPTER *ioc;
6868 struct Scsi_Host *shost;
6869
6870 shost = scsi_host_alloc(&scsih_driver_template,
6871 sizeof(struct MPT2SAS_ADAPTER));
6872 if (!shost)
6873 return -ENODEV;
6874
6875 /* init local params */
6876 ioc = shost_priv(shost);
6877 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6878 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006879 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006880 ioc->shost = shost;
6881 ioc->id = mpt_ids++;
6882 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6883 ioc->pdev = pdev;
6884 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6885 ioc->tm_cb_idx = tm_cb_idx;
6886 ioc->ctl_cb_idx = ctl_cb_idx;
6887 ioc->base_cb_idx = base_cb_idx;
6888 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306889 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006890 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306891 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306892 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306893 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006894 ioc->logging_level = logging_level;
6895 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05306896 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06006897 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6898 spin_lock_init(&ioc->scsi_lookup_lock);
6899 spin_lock_init(&ioc->sas_device_lock);
6900 spin_lock_init(&ioc->sas_node_lock);
6901 spin_lock_init(&ioc->fw_event_lock);
6902 spin_lock_init(&ioc->raid_device_lock);
6903
6904 INIT_LIST_HEAD(&ioc->sas_device_list);
6905 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6906 INIT_LIST_HEAD(&ioc->sas_expander_list);
6907 INIT_LIST_HEAD(&ioc->fw_event_list);
6908 INIT_LIST_HEAD(&ioc->raid_device_list);
6909 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306910 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306911 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06006912
6913 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06006914 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06006915 shost->max_lun = max_lun;
6916 shost->transportt = mpt2sas_transport_template;
6917 shost->unique_id = ioc->id;
6918
6919 if ((scsi_add_host(shost, &pdev->dev))) {
6920 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6921 ioc->name, __FILE__, __LINE__, __func__);
6922 list_del(&ioc->list);
6923 goto out_add_shost_fail;
6924 }
6925
Eric Moore3c621b32009-05-18 12:59:41 -06006926 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06006927 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306928 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006929
Eric Moore635374e2009-03-09 01:21:12 -06006930 /* event thread */
6931 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6932 "fw_event%d", ioc->id);
6933 ioc->firmware_event_thread = create_singlethread_workqueue(
6934 ioc->firmware_event_name);
6935 if (!ioc->firmware_event_thread) {
6936 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6937 ioc->name, __FILE__, __LINE__, __func__);
6938 goto out_thread_fail;
6939 }
6940
6941 ioc->wait_for_port_enable_to_complete = 1;
6942 if ((mpt2sas_base_attach(ioc))) {
6943 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6944 ioc->name, __FILE__, __LINE__, __func__);
6945 goto out_attach_fail;
6946 }
6947
6948 ioc->wait_for_port_enable_to_complete = 0;
6949 _scsih_probe_devices(ioc);
6950 return 0;
6951
6952 out_attach_fail:
6953 destroy_workqueue(ioc->firmware_event_thread);
6954 out_thread_fail:
6955 list_del(&ioc->list);
6956 scsi_remove_host(shost);
6957 out_add_shost_fail:
6958 return -ENODEV;
6959}
6960
6961#ifdef CONFIG_PM
6962/**
Eric Moored5d135b2009-05-18 13:02:08 -06006963 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006964 * @pdev: PCI device struct
6965 * @state: PM state change to (usually PCI_D3)
6966 *
6967 * Returns 0 success, anything else error.
6968 */
6969static int
Eric Moored5d135b2009-05-18 13:02:08 -06006970_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006971{
6972 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6973 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6974 u32 device_state;
6975
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306976 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006977 flush_scheduled_work();
6978 scsi_block_requests(shost);
6979 device_state = pci_choose_state(pdev, state);
6980 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6981 "operating state [D%d]\n", ioc->name, pdev,
6982 pci_name(pdev), device_state);
6983
6984 mpt2sas_base_free_resources(ioc);
6985 pci_save_state(pdev);
6986 pci_disable_device(pdev);
6987 pci_set_power_state(pdev, device_state);
6988 return 0;
6989}
6990
6991/**
Eric Moored5d135b2009-05-18 13:02:08 -06006992 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006993 * @pdev: PCI device struct
6994 *
6995 * Returns 0 success, anything else error.
6996 */
6997static int
Eric Moored5d135b2009-05-18 13:02:08 -06006998_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006999{
7000 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7001 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7002 u32 device_state = pdev->current_state;
7003 int r;
7004
7005 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7006 "operating state [D%d]\n", ioc->name, pdev,
7007 pci_name(pdev), device_state);
7008
7009 pci_set_power_state(pdev, PCI_D0);
7010 pci_enable_wake(pdev, PCI_D0, 0);
7011 pci_restore_state(pdev);
7012 ioc->pdev = pdev;
7013 r = mpt2sas_base_map_resources(ioc);
7014 if (r)
7015 return r;
7016
7017 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7018 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05307019 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007020 return 0;
7021}
7022#endif /* CONFIG_PM */
7023
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307024/**
7025 * _scsih_pci_error_detected - Called when a PCI error is detected.
7026 * @pdev: PCI device struct
7027 * @state: PCI channel state
7028 *
7029 * Description: Called when a PCI error is detected.
7030 *
7031 * Return value:
7032 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7033 */
7034static pci_ers_result_t
7035_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7036{
7037 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7038 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7039
7040 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7041 ioc->name, state);
7042
7043 switch (state) {
7044 case pci_channel_io_normal:
7045 return PCI_ERS_RESULT_CAN_RECOVER;
7046 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06007047 /* Fatal error, prepare for slot reset */
7048 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307049 scsi_block_requests(ioc->shost);
7050 mpt2sas_base_stop_watchdog(ioc);
7051 mpt2sas_base_free_resources(ioc);
7052 return PCI_ERS_RESULT_NEED_RESET;
7053 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06007054 /* Permanent error, prepare for device removal */
7055 ioc->pci_error_recovery = 1;
7056 mpt2sas_base_stop_watchdog(ioc);
7057 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307058 return PCI_ERS_RESULT_DISCONNECT;
7059 }
7060 return PCI_ERS_RESULT_NEED_RESET;
7061}
7062
7063/**
7064 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7065 * @pdev: PCI device struct
7066 *
7067 * Description: This routine is called by the pci error recovery
7068 * code after the PCI slot has been reset, just before we
7069 * should resume normal operations.
7070 */
7071static pci_ers_result_t
7072_scsih_pci_slot_reset(struct pci_dev *pdev)
7073{
7074 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7075 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7076 int rc;
7077
7078 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7079 ioc->name);
7080
Eric Moore3cb54692010-07-08 14:44:34 -06007081 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307082 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06007083 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307084 rc = mpt2sas_base_map_resources(ioc);
7085 if (rc)
7086 return PCI_ERS_RESULT_DISCONNECT;
7087
7088
7089 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7090 FORCE_BIG_HAMMER);
7091
7092 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7093 (rc == 0) ? "success" : "failed");
7094
7095 if (!rc)
7096 return PCI_ERS_RESULT_RECOVERED;
7097 else
7098 return PCI_ERS_RESULT_DISCONNECT;
7099}
7100
7101/**
7102 * _scsih_pci_resume() - resume normal ops after PCI reset
7103 * @pdev: pointer to PCI device
7104 *
7105 * Called when the error recovery driver tells us that its
7106 * OK to resume normal operation. Use completion to allow
7107 * halted scsi ops to resume.
7108 */
7109static void
7110_scsih_pci_resume(struct pci_dev *pdev)
7111{
7112 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7113 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7114
7115 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7116
7117 pci_cleanup_aer_uncorrect_error_status(pdev);
7118 mpt2sas_base_start_watchdog(ioc);
7119 scsi_unblock_requests(ioc->shost);
7120}
7121
7122/**
7123 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7124 * @pdev: pointer to PCI device
7125 */
7126static pci_ers_result_t
7127_scsih_pci_mmio_enabled(struct pci_dev *pdev)
7128{
7129 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7130 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7131
7132 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7133 ioc->name);
7134
7135 /* TODO - dump whatever for debugging purposes */
7136
7137 /* Request a slot reset. */
7138 return PCI_ERS_RESULT_NEED_RESET;
7139}
7140
7141static struct pci_error_handlers _scsih_err_handler = {
7142 .error_detected = _scsih_pci_error_detected,
7143 .mmio_enabled = _scsih_pci_mmio_enabled,
7144 .slot_reset = _scsih_pci_slot_reset,
7145 .resume = _scsih_pci_resume,
7146};
Eric Moore635374e2009-03-09 01:21:12 -06007147
7148static struct pci_driver scsih_driver = {
7149 .name = MPT2SAS_DRIVER_NAME,
7150 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06007151 .probe = _scsih_probe,
7152 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05307153 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307154 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06007155#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06007156 .suspend = _scsih_suspend,
7157 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06007158#endif
7159};
7160
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307161/* raid transport support */
7162static struct raid_function_template mpt2sas_raid_functions = {
7163 .cookie = &scsih_driver_template,
7164 .is_raid = _scsih_is_raid,
7165 .get_resync = _scsih_get_resync,
7166 .get_state = _scsih_get_state,
7167};
Eric Moore635374e2009-03-09 01:21:12 -06007168
7169/**
Eric Moored5d135b2009-05-18 13:02:08 -06007170 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06007171 *
7172 * Returns 0 success, anything else error.
7173 */
7174static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06007175_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06007176{
7177 int error;
7178
7179 mpt_ids = 0;
7180 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7181 MPT2SAS_DRIVER_VERSION);
7182
7183 mpt2sas_transport_template =
7184 sas_attach_transport(&mpt2sas_transport_functions);
7185 if (!mpt2sas_transport_template)
7186 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307187 /* raid transport support */
7188 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7189 if (!mpt2sas_raid_template) {
7190 sas_release_transport(mpt2sas_transport_template);
7191 return -ENODEV;
7192 }
Eric Moore635374e2009-03-09 01:21:12 -06007193
7194 mpt2sas_base_initialize_callback_handler();
7195
7196 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06007197 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06007198
Uwe Kleine-König65155b32010-06-11 12:17:01 +02007199 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06007200 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06007201
7202 /* base internal commands callback handler */
7203 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7204
7205 /* transport internal commands callback handler */
7206 transport_cb_idx = mpt2sas_base_register_callback_handler(
7207 mpt2sas_transport_done);
7208
Kashyap, Desai744090d2009-10-05 15:56:56 +05307209 /* scsih internal commands callback handler */
7210 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7211
Eric Moore635374e2009-03-09 01:21:12 -06007212 /* configuration page API internal commands callback handler */
7213 config_cb_idx = mpt2sas_base_register_callback_handler(
7214 mpt2sas_config_done);
7215
7216 /* ctl module callback handler */
7217 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7218
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307219 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7220 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307221
7222 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7223 _scsih_tm_volume_tr_complete);
7224
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307225 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7226 _scsih_sas_control_complete);
7227
Eric Moore635374e2009-03-09 01:21:12 -06007228 mpt2sas_ctl_init();
7229
7230 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307231 if (error) {
7232 /* raid transport support */
7233 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06007234 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307235 }
Eric Moore635374e2009-03-09 01:21:12 -06007236
7237 return error;
7238}
7239
7240/**
Eric Moored5d135b2009-05-18 13:02:08 -06007241 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06007242 *
7243 * Returns 0 success, anything else error.
7244 */
7245static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06007246_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06007247{
7248 printk(KERN_INFO "mpt2sas version %s unloading\n",
7249 MPT2SAS_DRIVER_VERSION);
7250
7251 pci_unregister_driver(&scsih_driver);
7252
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307253 mpt2sas_ctl_exit();
7254
Eric Moore635374e2009-03-09 01:21:12 -06007255 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7256 mpt2sas_base_release_callback_handler(tm_cb_idx);
7257 mpt2sas_base_release_callback_handler(base_cb_idx);
7258 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307259 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06007260 mpt2sas_base_release_callback_handler(config_cb_idx);
7261 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7262
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307263 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307264 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307265 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7266
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307267 /* raid transport support */
7268 raid_class_release(mpt2sas_raid_template);
7269 sas_release_transport(mpt2sas_transport_template);
7270
Eric Moore635374e2009-03-09 01:21:12 -06007271}
7272
Eric Moored5d135b2009-05-18 13:02:08 -06007273module_init(_scsih_init);
7274module_exit(_scsih_exit);