blob: 33723ed488ebfa36762e57c4c6fd80b7c734d5b5 [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
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300400 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600401 * 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);
Christoph Hellwiga7c44d42011-04-04 09:42:30 -04002136 if (scmd_lookup)
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302137 rc = FAILED;
2138 else
2139 rc = SUCCESS;
2140 break;
2141
2142 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2143 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2144 rc = FAILED;
2145 else
2146 rc = SUCCESS;
2147 break;
2148
2149 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2150 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2151 rc = FAILED;
2152 else
2153 rc = SUCCESS;
2154 break;
2155 }
2156
2157 bypass_sanity_checks:
2158
2159 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2160 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2161 mutex_unlock(&ioc->tm_cmds.mutex);
2162
2163 return rc;
2164
2165 err_out:
2166 mutex_unlock(&ioc->tm_cmds.mutex);
2167 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002168}
2169
2170/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302171 * _scsih_tm_display_info - displays info about the device
2172 * @ioc: per adapter struct
2173 * @scmd: pointer to scsi command object
2174 *
2175 * Called by task management callback handlers.
2176 */
2177static void
2178_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2179{
2180 struct scsi_target *starget = scmd->device->sdev_target;
2181 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2182 struct _sas_device *sas_device = NULL;
2183 unsigned long flags;
2184
2185 if (!priv_target)
2186 return;
2187
2188 scsi_print_command(scmd);
2189 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2190 starget_printk(KERN_INFO, starget, "volume handle(0x%04x), "
2191 "volume wwid(0x%016llx)\n",
2192 priv_target->handle,
2193 (unsigned long long)priv_target->sas_address);
2194 } else {
2195 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2196 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2197 priv_target->sas_address);
2198 if (sas_device) {
2199 if (priv_target->flags &
2200 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2201 starget_printk(KERN_INFO, starget,
2202 "volume handle(0x%04x), "
2203 "volume wwid(0x%016llx)\n",
2204 sas_device->volume_handle,
2205 (unsigned long long)sas_device->volume_wwid);
2206 }
2207 starget_printk(KERN_INFO, starget,
2208 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2209 sas_device->handle,
2210 (unsigned long long)sas_device->sas_address,
2211 sas_device->phy);
2212 starget_printk(KERN_INFO, starget,
2213 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2214 (unsigned long long)sas_device->enclosure_logical_id,
2215 sas_device->slot);
2216 }
2217 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2218 }
2219}
2220
2221/**
Eric Moored5d135b2009-05-18 13:02:08 -06002222 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302223 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002224 *
2225 * Returns SUCCESS if command aborted else FAILED
2226 */
2227static int
Eric Moored5d135b2009-05-18 13:02:08 -06002228_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002229{
2230 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2231 struct MPT2SAS_DEVICE *sas_device_priv_data;
2232 u16 smid;
2233 u16 handle;
2234 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002235
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302236 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2237 "scmd(%p)\n", scmd);
2238 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002239
2240 sas_device_priv_data = scmd->device->hostdata;
2241 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302242 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2243 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002244 scmd->result = DID_NO_CONNECT << 16;
2245 scmd->scsi_done(scmd);
2246 r = SUCCESS;
2247 goto out;
2248 }
2249
2250 /* search for the command */
2251 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2252 if (!smid) {
2253 scmd->result = DID_RESET << 16;
2254 r = SUCCESS;
2255 goto out;
2256 }
2257
2258 /* for hidden raid components and volumes this is not supported */
2259 if (sas_device_priv_data->sas_target->flags &
2260 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2261 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2262 scmd->result = DID_RESET << 16;
2263 r = FAILED;
2264 goto out;
2265 }
2266
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302267 mpt2sas_halt_firmware(ioc);
2268
Eric Moore635374e2009-03-09 01:21:12 -06002269 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302270 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2271 scmd->device->id, scmd->device->lun,
2272 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002273
2274 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302275 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2276 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002277 return r;
2278}
2279
Eric Moore635374e2009-03-09 01:21:12 -06002280/**
Eric Moored5d135b2009-05-18 13:02:08 -06002281 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302282 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002283 *
2284 * Returns SUCCESS if command aborted else FAILED
2285 */
2286static int
Eric Moored5d135b2009-05-18 13:02:08 -06002287_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002288{
2289 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2290 struct MPT2SAS_DEVICE *sas_device_priv_data;
2291 struct _sas_device *sas_device;
2292 unsigned long flags;
2293 u16 handle;
2294 int r;
2295
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302296 struct scsi_target *starget = scmd->device->sdev_target;
2297
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302298 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302299 "scmd(%p)\n", scmd);
2300 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002301
2302 sas_device_priv_data = scmd->device->hostdata;
2303 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302304 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302305 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002306 scmd->result = DID_NO_CONNECT << 16;
2307 scmd->scsi_done(scmd);
2308 r = SUCCESS;
2309 goto out;
2310 }
2311
2312 /* for hidden raid components obtain the volume_handle */
2313 handle = 0;
2314 if (sas_device_priv_data->sas_target->flags &
2315 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2316 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2317 sas_device = _scsih_sas_device_find_by_handle(ioc,
2318 sas_device_priv_data->sas_target->handle);
2319 if (sas_device)
2320 handle = sas_device->volume_handle;
2321 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2322 } else
2323 handle = sas_device_priv_data->sas_target->handle;
2324
2325 if (!handle) {
2326 scmd->result = DID_RESET << 16;
2327 r = FAILED;
2328 goto out;
2329 }
2330
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302331 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2332 scmd->device->id, scmd->device->lun,
2333 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002334
2335 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302336 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2337 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002338 return r;
2339}
2340
2341/**
Eric Moored5d135b2009-05-18 13:02:08 -06002342 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302343 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002344 *
2345 * Returns SUCCESS if command aborted else FAILED
2346 */
2347static int
Eric Moored5d135b2009-05-18 13:02:08 -06002348_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002349{
2350 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2351 struct MPT2SAS_DEVICE *sas_device_priv_data;
2352 struct _sas_device *sas_device;
2353 unsigned long flags;
2354 u16 handle;
2355 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302356 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002357
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302358 starget_printk(KERN_INFO, starget, "attempting target reset! "
2359 "scmd(%p)\n", scmd);
2360 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002361
2362 sas_device_priv_data = scmd->device->hostdata;
2363 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302364 starget_printk(KERN_INFO, starget, "target been deleted! "
2365 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002366 scmd->result = DID_NO_CONNECT << 16;
2367 scmd->scsi_done(scmd);
2368 r = SUCCESS;
2369 goto out;
2370 }
2371
2372 /* for hidden raid components obtain the volume_handle */
2373 handle = 0;
2374 if (sas_device_priv_data->sas_target->flags &
2375 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2376 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2377 sas_device = _scsih_sas_device_find_by_handle(ioc,
2378 sas_device_priv_data->sas_target->handle);
2379 if (sas_device)
2380 handle = sas_device->volume_handle;
2381 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2382 } else
2383 handle = sas_device_priv_data->sas_target->handle;
2384
2385 if (!handle) {
2386 scmd->result = DID_RESET << 16;
2387 r = FAILED;
2388 goto out;
2389 }
2390
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302391 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2392 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2393 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002394
2395 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302396 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2397 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002398 return r;
2399}
2400
2401/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302402 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302403 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002404 *
2405 * Returns SUCCESS if command aborted else FAILED
2406 */
2407static int
Eric Moored5d135b2009-05-18 13:02:08 -06002408_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002409{
2410 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2411 int r, retval;
2412
2413 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2414 ioc->name, scmd);
2415 scsi_print_command(scmd);
2416
2417 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2418 FORCE_BIG_HAMMER);
2419 r = (retval < 0) ? FAILED : SUCCESS;
2420 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2421 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2422
2423 return r;
2424}
2425
2426/**
2427 * _scsih_fw_event_add - insert and queue up fw_event
2428 * @ioc: per adapter object
2429 * @fw_event: object describing the event
2430 * Context: This function will acquire ioc->fw_event_lock.
2431 *
2432 * This adds the firmware event object into link list, then queues it up to
2433 * be processed from user context.
2434 *
2435 * Return nothing.
2436 */
2437static void
2438_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2439{
2440 unsigned long flags;
2441
2442 if (ioc->firmware_event_thread == NULL)
2443 return;
2444
2445 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2446 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302447 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2448 queue_delayed_work(ioc->firmware_event_thread,
2449 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002450 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2451}
2452
2453/**
2454 * _scsih_fw_event_free - delete fw_event
2455 * @ioc: per adapter object
2456 * @fw_event: object describing the event
2457 * Context: This function will acquire ioc->fw_event_lock.
2458 *
2459 * This removes firmware event object from link list, frees associated memory.
2460 *
2461 * Return nothing.
2462 */
2463static void
2464_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2465 *fw_event)
2466{
2467 unsigned long flags;
2468
2469 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2470 list_del(&fw_event->list);
2471 kfree(fw_event->event_data);
2472 kfree(fw_event);
2473 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2474}
2475
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302476
Eric Moore635374e2009-03-09 01:21:12 -06002477/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302478 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002479 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002480 *
2481 * Return nothing.
2482 */
2483static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302484_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002485{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302486 struct fw_event_work *fw_event;
2487
2488 if (ioc->wait_for_port_enable_to_complete)
2489 return;
2490 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2491 if (!fw_event)
2492 return;
2493 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2494 fw_event->ioc = ioc;
2495 _scsih_fw_event_add(ioc, fw_event);
2496}
2497
2498/**
2499 * _scsih_fw_event_cleanup_queue - cleanup event queue
2500 * @ioc: per adapter object
2501 *
2502 * Walk the firmware event queue, either killing timers, or waiting
2503 * for outstanding events to complete
2504 *
2505 * Return nothing.
2506 */
2507static void
2508_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2509{
2510 struct fw_event_work *fw_event, *next;
2511
2512 if (list_empty(&ioc->fw_event_list) ||
2513 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002514 return;
2515
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302516 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2517 if (cancel_delayed_work(&fw_event->delayed_work)) {
2518 _scsih_fw_event_free(ioc, fw_event);
2519 continue;
2520 }
2521 fw_event->cancel_pending_work = 1;
2522 }
Eric Moore635374e2009-03-09 01:21:12 -06002523}
2524
Eric Moore635374e2009-03-09 01:21:12 -06002525/**
2526 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2527 * @ioc: per adapter object
2528 * @handle: device handle
2529 *
2530 * During device pull we need to appropiately set the sdev state.
2531 */
2532static void
2533_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2534{
2535 struct MPT2SAS_DEVICE *sas_device_priv_data;
2536 struct scsi_device *sdev;
2537
2538 shost_for_each_device(sdev, ioc->shost) {
2539 sas_device_priv_data = sdev->hostdata;
2540 if (!sas_device_priv_data)
2541 continue;
2542 if (!sas_device_priv_data->block)
2543 continue;
2544 if (sas_device_priv_data->sas_target->handle == handle) {
2545 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2546 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2547 "handle(0x%04x)\n", ioc->name, handle));
2548 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302549 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002550 }
2551 }
2552}
2553
2554/**
2555 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2556 * @ioc: per adapter object
2557 * @handle: device handle
2558 *
2559 * During device pull we need to appropiately set the sdev state.
2560 */
2561static void
2562_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2563{
2564 struct MPT2SAS_DEVICE *sas_device_priv_data;
2565 struct scsi_device *sdev;
2566
2567 shost_for_each_device(sdev, ioc->shost) {
2568 sas_device_priv_data = sdev->hostdata;
2569 if (!sas_device_priv_data)
2570 continue;
2571 if (sas_device_priv_data->block)
2572 continue;
2573 if (sas_device_priv_data->sas_target->handle == handle) {
2574 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2575 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2576 "handle(0x%04x)\n", ioc->name, handle));
2577 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302578 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002579 }
2580 }
2581}
2582
2583/**
2584 * _scsih_block_io_to_children_attached_to_ex
2585 * @ioc: per adapter object
2586 * @sas_expander: the sas_device object
2587 *
2588 * This routine set sdev state to SDEV_BLOCK for all devices
2589 * attached to this expander. This function called when expander is
2590 * pulled.
2591 */
2592static void
2593_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2594 struct _sas_node *sas_expander)
2595{
2596 struct _sas_port *mpt2sas_port;
2597 struct _sas_device *sas_device;
2598 struct _sas_node *expander_sibling;
2599 unsigned long flags;
2600
2601 if (!sas_expander)
2602 return;
2603
2604 list_for_each_entry(mpt2sas_port,
2605 &sas_expander->sas_port_list, port_list) {
2606 if (mpt2sas_port->remote_identify.device_type ==
2607 SAS_END_DEVICE) {
2608 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2609 sas_device =
2610 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2611 mpt2sas_port->remote_identify.sas_address);
2612 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2613 if (!sas_device)
2614 continue;
2615 _scsih_block_io_device(ioc, sas_device->handle);
2616 }
2617 }
2618
2619 list_for_each_entry(mpt2sas_port,
2620 &sas_expander->sas_port_list, port_list) {
2621
2622 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302623 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06002624 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302625 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06002626
2627 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2628 expander_sibling =
2629 mpt2sas_scsih_expander_find_by_sas_address(
2630 ioc, mpt2sas_port->remote_identify.sas_address);
2631 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2632 _scsih_block_io_to_children_attached_to_ex(ioc,
2633 expander_sibling);
2634 }
2635 }
2636}
2637
2638/**
2639 * _scsih_block_io_to_children_attached_directly
2640 * @ioc: per adapter object
2641 * @event_data: topology change event data
2642 *
2643 * This routine set sdev state to SDEV_BLOCK for all devices
2644 * direct attached during device pull.
2645 */
2646static void
2647_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2648 Mpi2EventDataSasTopologyChangeList_t *event_data)
2649{
2650 int i;
2651 u16 handle;
2652 u16 reason_code;
2653 u8 phy_number;
2654
2655 for (i = 0; i < event_data->NumEntries; i++) {
2656 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2657 if (!handle)
2658 continue;
2659 phy_number = event_data->StartPhyNum + i;
2660 reason_code = event_data->PHY[i].PhyStatus &
2661 MPI2_EVENT_SAS_TOPO_RC_MASK;
2662 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2663 _scsih_block_io_device(ioc, handle);
2664 }
2665}
2666
2667/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302668 * _scsih_tm_tr_send - send task management request
2669 * @ioc: per adapter object
2670 * @handle: device handle
2671 * Context: interrupt time.
2672 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002673 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302674 * with controller firmware. This function will issue target reset
2675 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002676 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302677 *
2678 * This is designed to send muliple task management request at the same
2679 * time to the fifo. If the fifo is full, we will append the request,
2680 * and process it in a future completion.
2681 */
2682static void
2683_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2684{
2685 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302686 u16 smid;
2687 struct _sas_device *sas_device;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302688 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302689 unsigned long flags;
2690 struct _tr_list *delayed_tr;
2691
Eric Moore3cb54692010-07-08 14:44:34 -06002692 if (ioc->shost_recovery || ioc->remove_host ||
2693 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302694 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2695 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302696 return;
2697 }
2698
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302699 /* if PD, then return */
2700 if (test_bit(handle, ioc->pd_handles))
2701 return;
2702
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302703 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2704 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302705 if (sas_device && sas_device->starget &&
2706 sas_device->starget->hostdata) {
2707 sas_target_priv_data = sas_device->starget->hostdata;
2708 sas_target_priv_data->deleted = 1;
2709 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2710 "setting delete flag: handle(0x%04x), "
2711 "sas_addr(0x%016llx)\n", ioc->name, handle,
2712 (unsigned long long) sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05302713 }
2714 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302715
2716 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2717 if (!smid) {
2718 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2719 if (!delayed_tr)
2720 return;
2721 INIT_LIST_HEAD(&delayed_tr->list);
2722 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302723 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2724 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2725 "DELAYED:tr:handle(0x%04x), (open)\n",
2726 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302727 return;
2728 }
2729
Kashyap, Desai1278b112010-03-09 17:34:13 +05302730 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2731 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2732 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302733 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2734 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2735 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2736 mpi_request->DevHandle = cpu_to_le16(handle);
2737 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302738 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2739}
2740
2741
2742
2743/**
2744 * _scsih_sas_control_complete - completion routine
2745 * @ioc: per adapter object
2746 * @smid: system request message index
2747 * @msix_index: MSIX table index supplied by the OS
2748 * @reply: reply message frame(lower 32bit addr)
2749 * Context: interrupt time.
2750 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002751 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302752 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002753 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302754 *
2755 * Return 1 meaning mf should be freed from _base_interrupt
2756 * 0 means the mf is freed from this function.
2757 */
2758static u8
2759_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2760 u8 msix_index, u32 reply)
2761{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302762#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302763 Mpi2SasIoUnitControlReply_t *mpi_reply =
2764 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302765#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05302766 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2767 "sc_complete:handle(0x%04x), (open) "
2768 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2769 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2770 le16_to_cpu(mpi_reply->IOCStatus),
2771 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302772 return 1;
2773}
2774
2775/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302776 * _scsih_tm_tr_volume_send - send target reset request for volumes
2777 * @ioc: per adapter object
2778 * @handle: device handle
2779 * Context: interrupt time.
2780 *
2781 * This is designed to send muliple task management request at the same
2782 * time to the fifo. If the fifo is full, we will append the request,
2783 * and process it in a future completion.
2784 */
2785static void
2786_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2787{
2788 Mpi2SCSITaskManagementRequest_t *mpi_request;
2789 u16 smid;
2790 struct _tr_list *delayed_tr;
2791
Eric Moore3cb54692010-07-08 14:44:34 -06002792 if (ioc->shost_recovery || ioc->remove_host ||
2793 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302794 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2795 "progress!\n", __func__, ioc->name));
2796 return;
2797 }
2798
2799 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2800 if (!smid) {
2801 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2802 if (!delayed_tr)
2803 return;
2804 INIT_LIST_HEAD(&delayed_tr->list);
2805 delayed_tr->handle = handle;
2806 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2807 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2808 "DELAYED:tr:handle(0x%04x), (open)\n",
2809 ioc->name, handle));
2810 return;
2811 }
2812
2813 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2814 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2815 ioc->tm_tr_volume_cb_idx));
2816 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2817 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2818 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2819 mpi_request->DevHandle = cpu_to_le16(handle);
2820 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2821 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2822}
2823
2824/**
2825 * _scsih_tm_volume_tr_complete - target reset completion
2826 * @ioc: per adapter object
2827 * @smid: system request message index
2828 * @msix_index: MSIX table index supplied by the OS
2829 * @reply: reply message frame(lower 32bit addr)
2830 * Context: interrupt time.
2831 *
2832 * Return 1 meaning mf should be freed from _base_interrupt
2833 * 0 means the mf is freed from this function.
2834 */
2835static u8
2836_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2837 u8 msix_index, u32 reply)
2838{
2839 u16 handle;
2840 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2841 Mpi2SCSITaskManagementReply_t *mpi_reply =
2842 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2843
Eric Moore3cb54692010-07-08 14:44:34 -06002844 if (ioc->shost_recovery || ioc->remove_host ||
2845 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302846 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2847 "progress!\n", __func__, ioc->name));
2848 return 1;
2849 }
2850
2851 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2852 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2853 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2854 dewtprintk(ioc, printk("spurious interrupt: "
2855 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2856 le16_to_cpu(mpi_reply->DevHandle), smid));
2857 return 0;
2858 }
2859
2860 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2861 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2862 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2863 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2864 le32_to_cpu(mpi_reply->IOCLogInfo),
2865 le32_to_cpu(mpi_reply->TerminationCount)));
2866
2867 return _scsih_check_for_pending_tm(ioc, smid);
2868}
2869
2870/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302871 * _scsih_tm_tr_complete -
2872 * @ioc: per adapter object
2873 * @smid: system request message index
2874 * @msix_index: MSIX table index supplied by the OS
2875 * @reply: reply message frame(lower 32bit addr)
2876 * Context: interrupt time.
2877 *
2878 * This is the target reset completion routine.
2879 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002880 * handshake protocol with controller firmware.
2881 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302882 *
2883 * Return 1 meaning mf should be freed from _base_interrupt
2884 * 0 means the mf is freed from this function.
2885 */
2886static u8
2887_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2888 u32 reply)
2889{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302890 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302891 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302892 Mpi2SCSITaskManagementReply_t *mpi_reply =
2893 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2894 Mpi2SasIoUnitControlRequest_t *mpi_request;
2895 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302896
Eric Moore3cb54692010-07-08 14:44:34 -06002897 if (ioc->shost_recovery || ioc->remove_host ||
2898 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302899 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2900 "progress!\n", __func__, ioc->name));
2901 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302902 }
2903
Kashyap, Desai1278b112010-03-09 17:34:13 +05302904 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2905 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2906 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2907 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2908 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2909 le16_to_cpu(mpi_reply->DevHandle), smid));
2910 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302911 }
2912
Kashyap, Desai1278b112010-03-09 17:34:13 +05302913 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2914 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2915 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2916 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2917 le32_to_cpu(mpi_reply->IOCLogInfo),
2918 le32_to_cpu(mpi_reply->TerminationCount)));
2919
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302920 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2921 if (!smid_sas_ctrl) {
2922 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2923 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302924 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302925 }
2926
Kashyap, Desai1278b112010-03-09 17:34:13 +05302927 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2928 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2929 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302930 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2931 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2932 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2933 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302934 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302935 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302936
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302937 return _scsih_check_for_pending_tm(ioc, smid);
2938}
2939
2940/**
2941 * _scsih_check_for_pending_tm - check for pending task management
2942 * @ioc: per adapter object
2943 * @smid: system request message index
2944 *
2945 * This will check delayed target reset list, and feed the
2946 * next reqeust.
2947 *
2948 * Return 1 meaning mf should be freed from _base_interrupt
2949 * 0 means the mf is freed from this function.
2950 */
2951static u8
2952_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2953{
2954 struct _tr_list *delayed_tr;
2955
2956 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2957 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2958 struct _tr_list, list);
2959 mpt2sas_base_free_smid(ioc, smid);
2960 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2961 list_del(&delayed_tr->list);
2962 kfree(delayed_tr);
2963 return 0;
2964 }
2965
Kashyap, Desai1278b112010-03-09 17:34:13 +05302966 if (!list_empty(&ioc->delayed_tr_list)) {
2967 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2968 struct _tr_list, list);
2969 mpt2sas_base_free_smid(ioc, smid);
2970 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2971 list_del(&delayed_tr->list);
2972 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302973 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302974 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302975
Kashyap, Desai1278b112010-03-09 17:34:13 +05302976 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302977}
2978
2979/**
Eric Moore635374e2009-03-09 01:21:12 -06002980 * _scsih_check_topo_delete_events - sanity check on topo events
2981 * @ioc: per adapter object
2982 * @event_data: the event data payload
2983 *
2984 * This routine added to better handle cable breaker.
2985 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002986 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06002987 * add and delete events in a single shot. When there is a delete event
2988 * the routine will void any pending add events waiting in the event queue.
2989 *
2990 * Return nothing.
2991 */
2992static void
2993_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2994 Mpi2EventDataSasTopologyChangeList_t *event_data)
2995{
2996 struct fw_event_work *fw_event;
2997 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2998 u16 expander_handle;
2999 struct _sas_node *sas_expander;
3000 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303001 int i, reason_code;
3002 u16 handle;
3003
3004 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303005 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3006 if (!handle)
3007 continue;
3008 reason_code = event_data->PHY[i].PhyStatus &
3009 MPI2_EVENT_SAS_TOPO_RC_MASK;
3010 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3011 _scsih_tm_tr_send(ioc, handle);
3012 }
Eric Moore635374e2009-03-09 01:21:12 -06003013
3014 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3015 if (expander_handle < ioc->sas_hba.num_phys) {
3016 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3017 return;
3018 }
3019
3020 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3021 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3022 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3023 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3024 expander_handle);
3025 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3026 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3027 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3028 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3029
3030 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3031 return;
3032
3033 /* mark ignore flag for pending events */
3034 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3035 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3036 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3037 fw_event->ignore)
3038 continue;
3039 local_event_data = fw_event->event_data;
3040 if (local_event_data->ExpStatus ==
3041 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3042 local_event_data->ExpStatus ==
3043 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3044 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3045 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303046 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003047 "setting ignoring flag\n", ioc->name));
3048 fw_event->ignore = 1;
3049 }
3050 }
3051 }
3052 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3053}
3054
3055/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303056 * _scsih_set_volume_delete_flag - setting volume delete flag
3057 * @ioc: per adapter object
3058 * @handle: device handle
3059 *
3060 * This
3061 * Return nothing.
3062 */
3063static void
3064_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3065{
3066 struct _raid_device *raid_device;
3067 struct MPT2SAS_TARGET *sas_target_priv_data;
3068 unsigned long flags;
3069
3070 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3071 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3072 if (raid_device && raid_device->starget &&
3073 raid_device->starget->hostdata) {
3074 sas_target_priv_data =
3075 raid_device->starget->hostdata;
3076 sas_target_priv_data->deleted = 1;
3077 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3078 "setting delete flag: handle(0x%04x), "
3079 "wwid(0x%016llx)\n", ioc->name, handle,
3080 (unsigned long long) raid_device->wwid));
3081 }
3082 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3083}
3084
3085/**
3086 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3087 * @handle: input handle
3088 * @a: handle for volume a
3089 * @b: handle for volume b
3090 *
3091 * IR firmware only supports two raid volumes. The purpose of this
3092 * routine is to set the volume handle in either a or b. When the given
3093 * input handle is non-zero, or when a and b have not been set before.
3094 */
3095static void
3096_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3097{
3098 if (!handle || handle == *a || handle == *b)
3099 return;
3100 if (!*a)
3101 *a = handle;
3102 else if (!*b)
3103 *b = handle;
3104}
3105
3106/**
3107 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3108 * @ioc: per adapter object
3109 * @event_data: the event data payload
3110 * Context: interrupt time.
3111 *
3112 * This routine will send target reset to volume, followed by target
3113 * resets to the PDs. This is called when a PD has been removed, or
3114 * volume has been deleted or removed. When the target reset is sent
3115 * to volume, the PD target resets need to be queued to start upon
3116 * completion of the volume target reset.
3117 *
3118 * Return nothing.
3119 */
3120static void
3121_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3122 Mpi2EventDataIrConfigChangeList_t *event_data)
3123{
3124 Mpi2EventIrConfigElement_t *element;
3125 int i;
3126 u16 handle, volume_handle, a, b;
3127 struct _tr_list *delayed_tr;
3128
3129 a = 0;
3130 b = 0;
3131
3132 /* Volume Resets for Deleted or Removed */
3133 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3134 for (i = 0; i < event_data->NumElements; i++, element++) {
3135 if (element->ReasonCode ==
3136 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3137 element->ReasonCode ==
3138 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3139 volume_handle = le16_to_cpu(element->VolDevHandle);
3140 _scsih_set_volume_delete_flag(ioc, volume_handle);
3141 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3142 }
3143 }
3144
3145 /* Volume Resets for UNHIDE events */
3146 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3147 for (i = 0; i < event_data->NumElements; i++, element++) {
3148 if (le32_to_cpu(event_data->Flags) &
3149 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3150 continue;
3151 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3152 volume_handle = le16_to_cpu(element->VolDevHandle);
3153 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3154 }
3155 }
3156
3157 if (a)
3158 _scsih_tm_tr_volume_send(ioc, a);
3159 if (b)
3160 _scsih_tm_tr_volume_send(ioc, b);
3161
3162 /* PD target resets */
3163 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3164 for (i = 0; i < event_data->NumElements; i++, element++) {
3165 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3166 continue;
3167 handle = le16_to_cpu(element->PhysDiskDevHandle);
3168 volume_handle = le16_to_cpu(element->VolDevHandle);
3169 clear_bit(handle, ioc->pd_handles);
3170 if (!volume_handle)
3171 _scsih_tm_tr_send(ioc, handle);
3172 else if (volume_handle == a || volume_handle == b) {
3173 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3174 BUG_ON(!delayed_tr);
3175 INIT_LIST_HEAD(&delayed_tr->list);
3176 delayed_tr->handle = handle;
3177 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3178 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3179 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3180 handle));
3181 } else
3182 _scsih_tm_tr_send(ioc, handle);
3183 }
3184}
3185
3186
3187/**
3188 * _scsih_check_volume_delete_events - set delete flag for volumes
3189 * @ioc: per adapter object
3190 * @event_data: the event data payload
3191 * Context: interrupt time.
3192 *
3193 * This will handle the case when the cable connected to entire volume is
3194 * pulled. We will take care of setting the deleted flag so normal IO will
3195 * not be sent.
3196 *
3197 * Return nothing.
3198 */
3199static void
3200_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3201 Mpi2EventDataIrVolume_t *event_data)
3202{
3203 u32 state;
3204
3205 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3206 return;
3207 state = le32_to_cpu(event_data->NewValue);
3208 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3209 MPI2_RAID_VOL_STATE_FAILED)
3210 _scsih_set_volume_delete_flag(ioc,
3211 le16_to_cpu(event_data->VolDevHandle));
3212}
3213
3214/**
Eric Moore635374e2009-03-09 01:21:12 -06003215 * _scsih_flush_running_cmds - completing outstanding commands.
3216 * @ioc: per adapter object
3217 *
3218 * The flushing out of all pending scmd commands following host reset,
3219 * where all IO is dropped to the floor.
3220 *
3221 * Return nothing.
3222 */
3223static void
3224_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3225{
3226 struct scsi_cmnd *scmd;
3227 u16 smid;
3228 u16 count = 0;
3229
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303230 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303231 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003232 if (!scmd)
3233 continue;
3234 count++;
3235 mpt2sas_base_free_smid(ioc, smid);
3236 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003237 if (ioc->pci_error_recovery)
3238 scmd->result = DID_NO_CONNECT << 16;
3239 else
3240 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003241 scmd->scsi_done(scmd);
3242 }
3243 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3244 ioc->name, count));
3245}
3246
3247/**
Eric Moore3c621b32009-05-18 12:59:41 -06003248 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3249 * @scmd: pointer to scsi command object
3250 * @mpi_request: pointer to the SCSI_IO reqest message frame
3251 *
3252 * Supporting protection 1 and 3.
3253 *
3254 * Returns nothing
3255 */
3256static void
3257_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3258{
3259 u16 eedp_flags;
3260 unsigned char prot_op = scsi_get_prot_op(scmd);
3261 unsigned char prot_type = scsi_get_prot_type(scmd);
3262
Eric Moored334aa72010-04-22 10:47:40 -06003263 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003264 return;
3265
3266 if (prot_op == SCSI_PROT_READ_STRIP)
3267 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3268 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3269 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3270 else
3271 return;
3272
Eric Moore3c621b32009-05-18 12:59:41 -06003273 switch (prot_type) {
3274 case SCSI_PROT_DIF_TYPE1:
3275
3276 /*
3277 * enable ref/guard checking
3278 * auto increment ref tag
3279 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303280 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003281 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3282 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3283 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3284 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003285 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003286
Eric Moored334aa72010-04-22 10:47:40 -06003287 case SCSI_PROT_DIF_TYPE2:
3288
3289 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3290 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3291 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003292 break;
3293
3294 case SCSI_PROT_DIF_TYPE3:
3295
3296 /*
3297 * enable guard checking
3298 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303299 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003300 break;
3301 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303302 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3303 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003304}
3305
3306/**
3307 * _scsih_eedp_error_handling - return sense code for EEDP errors
3308 * @scmd: pointer to scsi command object
3309 * @ioc_status: ioc status
3310 *
3311 * Returns nothing
3312 */
3313static void
3314_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3315{
3316 u8 ascq;
3317 u8 sk;
3318 u8 host_byte;
3319
3320 switch (ioc_status) {
3321 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3322 ascq = 0x01;
3323 break;
3324 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3325 ascq = 0x02;
3326 break;
3327 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3328 ascq = 0x03;
3329 break;
3330 default:
3331 ascq = 0x00;
3332 break;
3333 }
3334
3335 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3336 sk = ILLEGAL_REQUEST;
3337 host_byte = DID_ABORT;
3338 } else {
3339 sk = ABORTED_COMMAND;
3340 host_byte = DID_OK;
3341 }
3342
3343 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3344 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3345 SAM_STAT_CHECK_CONDITION;
3346}
3347
3348/**
Eric Moored5d135b2009-05-18 13:02:08 -06003349 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003350 * @scmd: pointer to scsi command object
3351 * @done: function pointer to be invoked on completion
3352 *
3353 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3354 *
3355 * Returns 0 on success. If there's a failure, return either:
3356 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3357 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3358 */
3359static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003360_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003361{
3362 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3363 struct MPT2SAS_DEVICE *sas_device_priv_data;
3364 struct MPT2SAS_TARGET *sas_target_priv_data;
3365 Mpi2SCSIIORequest_t *mpi_request;
3366 u32 mpi_control;
3367 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003368
3369 scmd->scsi_done = done;
3370 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303371 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003372 scmd->result = DID_NO_CONNECT << 16;
3373 scmd->scsi_done(scmd);
3374 return 0;
3375 }
3376
Eric Moore3cb54692010-07-08 14:44:34 -06003377 if (ioc->pci_error_recovery) {
3378 scmd->result = DID_NO_CONNECT << 16;
3379 scmd->scsi_done(scmd);
3380 return 0;
3381 }
3382
Eric Moore635374e2009-03-09 01:21:12 -06003383 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303384 /* invalid device handle */
3385 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003386 scmd->result = DID_NO_CONNECT << 16;
3387 scmd->scsi_done(scmd);
3388 return 0;
3389 }
3390
Kashyap, Desai130b9582010-04-08 17:54:32 +05303391 /* host recovery or link resets sent via IOCTLs */
3392 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003393 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003394 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303395 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3396 return SCSI_MLQUEUE_DEVICE_BUSY;
3397 /* device has been deleted */
3398 else if (sas_target_priv_data->deleted) {
3399 scmd->result = DID_NO_CONNECT << 16;
3400 scmd->scsi_done(scmd);
3401 return 0;
3402 }
Eric Moore635374e2009-03-09 01:21:12 -06003403
3404 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3405 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3406 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3407 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3408 else
3409 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3410
3411 /* set tags */
3412 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3413 if (scmd->device->tagged_supported) {
3414 if (scmd->device->ordered_tags)
3415 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3416 else
3417 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3418 } else
3419/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3420/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3421 */
3422 mpi_control |= (0x500);
3423
3424 } else
3425 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303426 /* Make sure Device is not raid volume */
3427 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003428 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003429 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3430
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303431 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003432 if (!smid) {
3433 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3434 ioc->name, __func__);
3435 goto out;
3436 }
3437 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3438 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003439 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003440 if (scmd->cmd_len == 32)
3441 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003442 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3443 if (sas_device_priv_data->sas_target->flags &
3444 MPT_TARGET_FLAGS_RAID_COMPONENT)
3445 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3446 else
3447 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3448 mpi_request->DevHandle =
3449 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3450 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3451 mpi_request->Control = cpu_to_le32(mpi_control);
3452 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3453 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3454 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3455 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303456 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003457 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3458 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3459 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303460 mpi_request->VF_ID = 0; /* TODO */
3461 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003462 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3463 mpi_request->LUN);
3464 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3465
3466 if (!mpi_request->DataLength) {
3467 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3468 } else {
3469 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3470 mpt2sas_base_free_smid(ioc, smid);
3471 goto out;
3472 }
3473 }
3474
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303475 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3476 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3477 sas_device_priv_data->sas_target->handle);
3478 else
3479 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003480 return 0;
3481
3482 out:
3483 return SCSI_MLQUEUE_HOST_BUSY;
3484}
3485
Jeff Garzikf2812332010-11-16 02:10:29 -05003486static DEF_SCSI_QCMD(_scsih_qcmd)
3487
Eric Moore635374e2009-03-09 01:21:12 -06003488/**
3489 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3490 * @sense_buffer: sense data returned by target
3491 * @data: normalized skey/asc/ascq
3492 *
3493 * Return nothing.
3494 */
3495static void
3496_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3497{
3498 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3499 /* descriptor format */
3500 data->skey = sense_buffer[1] & 0x0F;
3501 data->asc = sense_buffer[2];
3502 data->ascq = sense_buffer[3];
3503 } else {
3504 /* fixed format */
3505 data->skey = sense_buffer[2] & 0x0F;
3506 data->asc = sense_buffer[12];
3507 data->ascq = sense_buffer[13];
3508 }
3509}
3510
3511#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3512/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003513 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003514 * @ioc: per adapter object
3515 * @scmd: pointer to scsi command object
3516 * @mpi_reply: reply mf payload returned from firmware
3517 *
3518 * scsi_status - SCSI Status code returned from target device
3519 * scsi_state - state info associated with SCSI_IO determined by ioc
3520 * ioc_status - ioc supplied status info
3521 *
3522 * Return nothing.
3523 */
3524static void
3525_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3526 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3527{
3528 u32 response_info;
3529 u8 *response_bytes;
3530 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3531 MPI2_IOCSTATUS_MASK;
3532 u8 scsi_state = mpi_reply->SCSIState;
3533 u8 scsi_status = mpi_reply->SCSIStatus;
3534 char *desc_ioc_state = NULL;
3535 char *desc_scsi_status = NULL;
3536 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303537 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303538 struct _sas_device *sas_device = NULL;
3539 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303540 struct scsi_target *starget = scmd->device->sdev_target;
3541 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3542
3543 if (!priv_target)
3544 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303545
3546 if (log_info == 0x31170000)
3547 return;
Eric Moore635374e2009-03-09 01:21:12 -06003548
3549 switch (ioc_status) {
3550 case MPI2_IOCSTATUS_SUCCESS:
3551 desc_ioc_state = "success";
3552 break;
3553 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3554 desc_ioc_state = "invalid function";
3555 break;
3556 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3557 desc_ioc_state = "scsi recovered error";
3558 break;
3559 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3560 desc_ioc_state = "scsi invalid dev handle";
3561 break;
3562 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3563 desc_ioc_state = "scsi device not there";
3564 break;
3565 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3566 desc_ioc_state = "scsi data overrun";
3567 break;
3568 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3569 desc_ioc_state = "scsi data underrun";
3570 break;
3571 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3572 desc_ioc_state = "scsi io data error";
3573 break;
3574 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3575 desc_ioc_state = "scsi protocol error";
3576 break;
3577 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3578 desc_ioc_state = "scsi task terminated";
3579 break;
3580 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3581 desc_ioc_state = "scsi residual mismatch";
3582 break;
3583 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3584 desc_ioc_state = "scsi task mgmt failed";
3585 break;
3586 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3587 desc_ioc_state = "scsi ioc terminated";
3588 break;
3589 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3590 desc_ioc_state = "scsi ext terminated";
3591 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003592 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3593 desc_ioc_state = "eedp guard error";
3594 break;
3595 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3596 desc_ioc_state = "eedp ref tag error";
3597 break;
3598 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3599 desc_ioc_state = "eedp app tag error";
3600 break;
Eric Moore635374e2009-03-09 01:21:12 -06003601 default:
3602 desc_ioc_state = "unknown";
3603 break;
3604 }
3605
3606 switch (scsi_status) {
3607 case MPI2_SCSI_STATUS_GOOD:
3608 desc_scsi_status = "good";
3609 break;
3610 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3611 desc_scsi_status = "check condition";
3612 break;
3613 case MPI2_SCSI_STATUS_CONDITION_MET:
3614 desc_scsi_status = "condition met";
3615 break;
3616 case MPI2_SCSI_STATUS_BUSY:
3617 desc_scsi_status = "busy";
3618 break;
3619 case MPI2_SCSI_STATUS_INTERMEDIATE:
3620 desc_scsi_status = "intermediate";
3621 break;
3622 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3623 desc_scsi_status = "intermediate condmet";
3624 break;
3625 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3626 desc_scsi_status = "reservation conflict";
3627 break;
3628 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3629 desc_scsi_status = "command terminated";
3630 break;
3631 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3632 desc_scsi_status = "task set full";
3633 break;
3634 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3635 desc_scsi_status = "aca active";
3636 break;
3637 case MPI2_SCSI_STATUS_TASK_ABORTED:
3638 desc_scsi_status = "task aborted";
3639 break;
3640 default:
3641 desc_scsi_status = "unknown";
3642 break;
3643 }
3644
3645 desc_scsi_state[0] = '\0';
3646 if (!scsi_state)
3647 desc_scsi_state = " ";
3648 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3649 strcat(desc_scsi_state, "response info ");
3650 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3651 strcat(desc_scsi_state, "state terminated ");
3652 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3653 strcat(desc_scsi_state, "no status ");
3654 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3655 strcat(desc_scsi_state, "autosense failed ");
3656 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3657 strcat(desc_scsi_state, "autosense valid ");
3658
3659 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303660
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303661 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3662 printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
3663 (unsigned long long)priv_target->sas_address);
3664 } else {
3665 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3666 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3667 priv_target->sas_address);
3668 if (sas_device) {
3669 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
3670 "phy(%d)\n", ioc->name, sas_device->sas_address,
3671 sas_device->phy);
3672 printk(MPT2SAS_WARN_FMT
3673 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
3674 ioc->name, sas_device->enclosure_logical_id,
3675 sas_device->slot);
3676 }
3677 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303678 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303679
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303680 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
3681 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3682 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003683 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3684 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3685 scsi_get_resid(scmd));
3686 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3687 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3688 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3689 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3690 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3691 scsi_status, desc_scsi_state, scsi_state);
3692
3693 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3694 struct sense_info data;
3695 _scsih_normalize_sense(scmd->sense_buffer, &data);
3696 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303697 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3698 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06003699 }
3700
3701 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3702 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3703 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303704 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003705 }
3706}
3707#endif
3708
3709/**
3710 * _scsih_smart_predicted_fault - illuminate Fault LED
3711 * @ioc: per adapter object
3712 * @handle: device handle
3713 *
3714 * Return nothing.
3715 */
3716static void
3717_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3718{
3719 Mpi2SepReply_t mpi_reply;
3720 Mpi2SepRequest_t mpi_request;
3721 struct scsi_target *starget;
3722 struct MPT2SAS_TARGET *sas_target_priv_data;
3723 Mpi2EventNotificationReply_t *event_reply;
3724 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3725 struct _sas_device *sas_device;
3726 ssize_t sz;
3727 unsigned long flags;
3728
3729 /* only handle non-raid devices */
3730 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3731 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3732 if (!sas_device) {
3733 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3734 return;
3735 }
3736 starget = sas_device->starget;
3737 sas_target_priv_data = starget->hostdata;
3738
3739 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3740 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3741 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3742 return;
3743 }
3744 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3745 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3746
3747 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3748 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3749 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3750 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3751 mpi_request.SlotStatus =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303752 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
Eric Moore635374e2009-03-09 01:21:12 -06003753 mpi_request.DevHandle = cpu_to_le16(handle);
3754 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3755 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3756 &mpi_request)) != 0) {
3757 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3758 ioc->name, __FILE__, __LINE__, __func__);
3759 return;
3760 }
3761
3762 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3763 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3764 "enclosure_processor: ioc_status (0x%04x), "
3765 "loginfo(0x%08x)\n", ioc->name,
3766 le16_to_cpu(mpi_reply.IOCStatus),
3767 le32_to_cpu(mpi_reply.IOCLogInfo)));
3768 return;
3769 }
3770 }
3771
3772 /* insert into event log */
3773 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3774 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3775 event_reply = kzalloc(sz, GFP_KERNEL);
3776 if (!event_reply) {
3777 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3778 ioc->name, __FILE__, __LINE__, __func__);
3779 return;
3780 }
3781
3782 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3783 event_reply->Event =
3784 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3785 event_reply->MsgLength = sz/4;
3786 event_reply->EventDataLength =
3787 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3788 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3789 event_reply->EventData;
3790 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3791 event_data->ASC = 0x5D;
3792 event_data->DevHandle = cpu_to_le16(handle);
3793 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3794 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3795 kfree(event_reply);
3796}
3797
3798/**
Eric Moored5d135b2009-05-18 13:02:08 -06003799 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003800 * @ioc: per adapter object
3801 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303802 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003803 * @reply: reply message frame(lower 32bit addr)
3804 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303805 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003806 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303807 * Return 1 meaning mf should be freed from _base_interrupt
3808 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003809 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303810static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303811_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003812{
3813 Mpi2SCSIIORequest_t *mpi_request;
3814 Mpi2SCSIIOReply_t *mpi_reply;
3815 struct scsi_cmnd *scmd;
3816 u16 ioc_status;
3817 u32 xfer_cnt;
3818 u8 scsi_state;
3819 u8 scsi_status;
3820 u32 log_info;
3821 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303822 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003823
3824 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303825 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003826 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303827 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003828
3829 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3830
3831 if (mpi_reply == NULL) {
3832 scmd->result = DID_OK << 16;
3833 goto out;
3834 }
3835
3836 sas_device_priv_data = scmd->device->hostdata;
3837 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3838 sas_device_priv_data->sas_target->deleted) {
3839 scmd->result = DID_NO_CONNECT << 16;
3840 goto out;
3841 }
3842
3843 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303844 scsi_state = mpi_reply->SCSIState;
3845 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3846 response_code =
3847 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003848 if (!sas_device_priv_data->tlr_snoop_check) {
3849 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303850 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3851 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303852 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3853 sas_disable_tlr(scmd->device);
3854 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3855 }
Eric Moore635374e2009-03-09 01:21:12 -06003856 }
3857
3858 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3859 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3860 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3861 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3862 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3863 else
3864 log_info = 0;
3865 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003866 scsi_status = mpi_reply->SCSIStatus;
3867
3868 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3869 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3870 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3871 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3872 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3873 }
3874
3875 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3876 struct sense_info data;
3877 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3878 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003879 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003880 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003881 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003882 _scsih_normalize_sense(scmd->sense_buffer, &data);
3883 /* failure prediction threshold exceeded */
3884 if (data.asc == 0x5D)
3885 _scsih_smart_predicted_fault(ioc,
3886 le16_to_cpu(mpi_reply->DevHandle));
3887 }
3888
3889 switch (ioc_status) {
3890 case MPI2_IOCSTATUS_BUSY:
3891 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3892 scmd->result = SAM_STAT_BUSY;
3893 break;
3894
3895 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3896 scmd->result = DID_NO_CONNECT << 16;
3897 break;
3898
3899 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3900 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303901 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3902 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003903 }
Eric Moore635374e2009-03-09 01:21:12 -06003904 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3905 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3906 scmd->result = DID_RESET << 16;
3907 break;
3908
3909 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3910 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3911 scmd->result = DID_SOFT_ERROR << 16;
3912 else
3913 scmd->result = (DID_OK << 16) | scsi_status;
3914 break;
3915
3916 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3917 scmd->result = (DID_OK << 16) | scsi_status;
3918
3919 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3920 break;
3921
3922 if (xfer_cnt < scmd->underflow) {
3923 if (scsi_status == SAM_STAT_BUSY)
3924 scmd->result = SAM_STAT_BUSY;
3925 else
3926 scmd->result = DID_SOFT_ERROR << 16;
3927 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3928 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3929 scmd->result = DID_SOFT_ERROR << 16;
3930 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3931 scmd->result = DID_RESET << 16;
3932 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3933 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3934 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3935 scmd->result = (DRIVER_SENSE << 24) |
3936 SAM_STAT_CHECK_CONDITION;
3937 scmd->sense_buffer[0] = 0x70;
3938 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3939 scmd->sense_buffer[12] = 0x20;
3940 scmd->sense_buffer[13] = 0;
3941 }
3942 break;
3943
3944 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3945 scsi_set_resid(scmd, 0);
3946 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3947 case MPI2_IOCSTATUS_SUCCESS:
3948 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303949 if (response_code ==
3950 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3951 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3952 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003953 scmd->result = DID_SOFT_ERROR << 16;
3954 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3955 scmd->result = DID_RESET << 16;
3956 break;
3957
Eric Moore3c621b32009-05-18 12:59:41 -06003958 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3959 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3960 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3961 _scsih_eedp_error_handling(scmd, ioc_status);
3962 break;
Eric Moore635374e2009-03-09 01:21:12 -06003963 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3964 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3965 case MPI2_IOCSTATUS_INVALID_SGL:
3966 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3967 case MPI2_IOCSTATUS_INVALID_FIELD:
3968 case MPI2_IOCSTATUS_INVALID_STATE:
3969 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3970 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3971 default:
3972 scmd->result = DID_SOFT_ERROR << 16;
3973 break;
3974
3975 }
3976
3977#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3978 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3979 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3980#endif
3981
3982 out:
3983 scsi_dma_unmap(scmd);
3984 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303985 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003986}
3987
3988/**
Eric Moore635374e2009-03-09 01:21:12 -06003989 * _scsih_sas_host_refresh - refreshing sas host object contents
3990 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003991 * Context: user
3992 *
3993 * During port enable, fw will send topology events for every device. Its
3994 * possible that the handles may change from the previous setting, so this
3995 * code keeping handles updating if changed.
3996 *
3997 * Return nothing.
3998 */
3999static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304000_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004001{
4002 u16 sz;
4003 u16 ioc_status;
4004 int i;
4005 Mpi2ConfigReply_t mpi_reply;
4006 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304007 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304008 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004009
4010 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4011 "updating handles for sas_host(0x%016llx)\n",
4012 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4013
4014 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4015 * sizeof(Mpi2SasIOUnit0PhyData_t));
4016 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4017 if (!sas_iounit_pg0) {
4018 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4019 ioc->name, __FILE__, __LINE__, __func__);
4020 return;
4021 }
Eric Moore635374e2009-03-09 01:21:12 -06004022
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304023 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4024 sas_iounit_pg0, sz)) != 0)
4025 goto out;
4026 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4027 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4028 goto out;
4029 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304030 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304031 if (i == 0)
4032 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4033 PhyData[0].ControllerDevHandle);
4034 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4035 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4036 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304037 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4038 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304039 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304040 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304041 }
Eric Moore635374e2009-03-09 01:21:12 -06004042 out:
4043 kfree(sas_iounit_pg0);
4044}
4045
4046/**
4047 * _scsih_sas_host_add - create sas host object
4048 * @ioc: per adapter object
4049 *
4050 * Creating host side data object, stored in ioc->sas_hba
4051 *
4052 * Return nothing.
4053 */
4054static void
4055_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4056{
4057 int i;
4058 Mpi2ConfigReply_t mpi_reply;
4059 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4060 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4061 Mpi2SasPhyPage0_t phy_pg0;
4062 Mpi2SasDevicePage0_t sas_device_pg0;
4063 Mpi2SasEnclosurePage0_t enclosure_pg0;
4064 u16 ioc_status;
4065 u16 sz;
4066 u16 device_missing_delay;
4067
4068 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4069 if (!ioc->sas_hba.num_phys) {
4070 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4071 ioc->name, __FILE__, __LINE__, __func__);
4072 return;
4073 }
4074
4075 /* sas_iounit page 0 */
4076 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4077 sizeof(Mpi2SasIOUnit0PhyData_t));
4078 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4079 if (!sas_iounit_pg0) {
4080 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4081 ioc->name, __FILE__, __LINE__, __func__);
4082 return;
4083 }
4084 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4085 sas_iounit_pg0, sz))) {
4086 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4087 ioc->name, __FILE__, __LINE__, __func__);
4088 goto out;
4089 }
4090 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4091 MPI2_IOCSTATUS_MASK;
4092 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4093 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4094 ioc->name, __FILE__, __LINE__, __func__);
4095 goto out;
4096 }
4097
4098 /* sas_iounit page 1 */
4099 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4100 sizeof(Mpi2SasIOUnit1PhyData_t));
4101 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4102 if (!sas_iounit_pg1) {
4103 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4104 ioc->name, __FILE__, __LINE__, __func__);
4105 goto out;
4106 }
4107 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4108 sas_iounit_pg1, sz))) {
4109 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4110 ioc->name, __FILE__, __LINE__, __func__);
4111 goto out;
4112 }
4113 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4114 MPI2_IOCSTATUS_MASK;
4115 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4116 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4117 ioc->name, __FILE__, __LINE__, __func__);
4118 goto out;
4119 }
4120
4121 ioc->io_missing_delay =
4122 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4123 device_missing_delay =
4124 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4125 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4126 ioc->device_missing_delay = (device_missing_delay &
4127 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4128 else
4129 ioc->device_missing_delay = device_missing_delay &
4130 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4131
4132 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4133 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4134 sizeof(struct _sas_phy), GFP_KERNEL);
4135 if (!ioc->sas_hba.phy) {
4136 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4137 ioc->name, __FILE__, __LINE__, __func__);
4138 goto out;
4139 }
4140 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4141 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4142 i))) {
4143 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4144 ioc->name, __FILE__, __LINE__, __func__);
4145 goto out;
4146 }
4147 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4148 MPI2_IOCSTATUS_MASK;
4149 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4150 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4151 ioc->name, __FILE__, __LINE__, __func__);
4152 goto out;
4153 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304154
4155 if (i == 0)
4156 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4157 PhyData[0].ControllerDevHandle);
4158 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004159 ioc->sas_hba.phy[i].phy_id = i;
4160 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4161 phy_pg0, ioc->sas_hba.parent_dev);
4162 }
4163 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304164 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004165 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4166 ioc->name, __FILE__, __LINE__, __func__);
4167 goto out;
4168 }
Eric Moore635374e2009-03-09 01:21:12 -06004169 ioc->sas_hba.enclosure_handle =
4170 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4171 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4172 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4173 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4174 (unsigned long long) ioc->sas_hba.sas_address,
4175 ioc->sas_hba.num_phys) ;
4176
4177 if (ioc->sas_hba.enclosure_handle) {
4178 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4179 &enclosure_pg0,
4180 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4181 ioc->sas_hba.enclosure_handle))) {
4182 ioc->sas_hba.enclosure_logical_id =
4183 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4184 }
4185 }
4186
4187 out:
4188 kfree(sas_iounit_pg1);
4189 kfree(sas_iounit_pg0);
4190}
4191
4192/**
4193 * _scsih_expander_add - creating expander object
4194 * @ioc: per adapter object
4195 * @handle: expander handle
4196 *
4197 * Creating expander object, stored in ioc->sas_expander_list.
4198 *
4199 * Return 0 for success, else error.
4200 */
4201static int
4202_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4203{
4204 struct _sas_node *sas_expander;
4205 Mpi2ConfigReply_t mpi_reply;
4206 Mpi2ExpanderPage0_t expander_pg0;
4207 Mpi2ExpanderPage1_t expander_pg1;
4208 Mpi2SasEnclosurePage0_t enclosure_pg0;
4209 u32 ioc_status;
4210 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304211 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004212 int i;
4213 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304214 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004215 int rc = 0;
4216
4217 if (!handle)
4218 return -1;
4219
Eric Moore3cb54692010-07-08 14:44:34 -06004220 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304221 return -1;
4222
Eric Moore635374e2009-03-09 01:21:12 -06004223 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4224 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4225 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4226 ioc->name, __FILE__, __LINE__, __func__);
4227 return -1;
4228 }
4229
4230 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4231 MPI2_IOCSTATUS_MASK;
4232 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4233 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4234 ioc->name, __FILE__, __LINE__, __func__);
4235 return -1;
4236 }
4237
4238 /* handle out of order topology events */
4239 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304240 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4241 != 0) {
4242 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4243 ioc->name, __FILE__, __LINE__, __func__);
4244 return -1;
4245 }
4246 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004247 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304248 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4249 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004250 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4251 if (!sas_expander) {
4252 rc = _scsih_expander_add(ioc, parent_handle);
4253 if (rc != 0)
4254 return rc;
4255 }
4256 }
4257
Eric Moore635374e2009-03-09 01:21:12 -06004258 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304259 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004260 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4261 sas_address);
4262 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4263
4264 if (sas_expander)
4265 return 0;
4266
4267 sas_expander = kzalloc(sizeof(struct _sas_node),
4268 GFP_KERNEL);
4269 if (!sas_expander) {
4270 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4271 ioc->name, __FILE__, __LINE__, __func__);
4272 return -1;
4273 }
4274
4275 sas_expander->handle = handle;
4276 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304277 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004278 sas_expander->sas_address = sas_address;
4279
4280 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4281 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304282 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004283 sas_expander->sas_address, sas_expander->num_phys);
4284
4285 if (!sas_expander->num_phys)
4286 goto out_fail;
4287 sas_expander->phy = kcalloc(sas_expander->num_phys,
4288 sizeof(struct _sas_phy), GFP_KERNEL);
4289 if (!sas_expander->phy) {
4290 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4291 ioc->name, __FILE__, __LINE__, __func__);
4292 rc = -1;
4293 goto out_fail;
4294 }
4295
4296 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4297 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304298 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004299 if (!mpt2sas_port) {
4300 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4301 ioc->name, __FILE__, __LINE__, __func__);
4302 rc = -1;
4303 goto out_fail;
4304 }
4305 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4306
4307 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4308 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4309 &expander_pg1, i, handle))) {
4310 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4311 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304312 rc = -1;
4313 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004314 }
4315 sas_expander->phy[i].handle = handle;
4316 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304317
4318 if ((mpt2sas_transport_add_expander_phy(ioc,
4319 &sas_expander->phy[i], expander_pg1,
4320 sas_expander->parent_dev))) {
4321 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4322 ioc->name, __FILE__, __LINE__, __func__);
4323 rc = -1;
4324 goto out_fail;
4325 }
Eric Moore635374e2009-03-09 01:21:12 -06004326 }
4327
4328 if (sas_expander->enclosure_handle) {
4329 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4330 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4331 sas_expander->enclosure_handle))) {
4332 sas_expander->enclosure_logical_id =
4333 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4334 }
4335 }
4336
4337 _scsih_expander_node_add(ioc, sas_expander);
4338 return 0;
4339
4340 out_fail:
4341
Kashyap, Desai20f58952009-08-07 19:34:26 +05304342 if (mpt2sas_port)
4343 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304344 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004345 kfree(sas_expander);
4346 return rc;
4347}
4348
4349/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304350 * _scsih_done - scsih callback handler.
4351 * @ioc: per adapter object
4352 * @smid: system request message index
4353 * @msix_index: MSIX table index supplied by the OS
4354 * @reply: reply message frame(lower 32bit addr)
4355 *
4356 * Callback handler when sending internal generated message frames.
4357 * The callback index passed is `ioc->scsih_cb_idx`
4358 *
4359 * Return 1 meaning mf should be freed from _base_interrupt
4360 * 0 means the mf is freed from this function.
4361 */
4362static u8
4363_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4364{
4365 MPI2DefaultReply_t *mpi_reply;
4366
4367 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4368 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4369 return 1;
4370 if (ioc->scsih_cmds.smid != smid)
4371 return 1;
4372 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4373 if (mpi_reply) {
4374 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4375 mpi_reply->MsgLength*4);
4376 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4377 }
4378 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4379 complete(&ioc->scsih_cmds.done);
4380 return 1;
4381}
4382
4383/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304384 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004385 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304386 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004387 *
4388 * Return nothing.
4389 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304390void
4391mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004392{
4393 struct _sas_node *sas_expander;
4394 unsigned long flags;
4395
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304396 if (ioc->shost_recovery)
4397 return;
4398
Eric Moore635374e2009-03-09 01:21:12 -06004399 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304400 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4401 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304402 if (!sas_expander) {
4403 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4404 return;
4405 }
4406 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004407 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4408 _scsih_expander_node_remove(ioc, sas_expander);
4409}
4410
4411/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304412 * _scsih_check_access_status - check access flags
4413 * @ioc: per adapter object
4414 * @sas_address: sas address
4415 * @handle: sas device handle
4416 * @access_flags: errors returned during discovery of the device
4417 *
4418 * Return 0 for success, else failure
4419 */
4420static u8
4421_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4422 u16 handle, u8 access_status)
4423{
4424 u8 rc = 1;
4425 char *desc = NULL;
4426
4427 switch (access_status) {
4428 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4429 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4430 rc = 0;
4431 break;
4432 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4433 desc = "sata capability failed";
4434 break;
4435 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4436 desc = "sata affiliation conflict";
4437 break;
4438 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4439 desc = "route not addressable";
4440 break;
4441 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4442 desc = "smp error not addressable";
4443 break;
4444 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4445 desc = "device blocked";
4446 break;
4447 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4448 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4449 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4450 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4451 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4452 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4453 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4454 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4455 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4456 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4457 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4458 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4459 desc = "sata initialization failed";
4460 break;
4461 default:
4462 desc = "unknown";
4463 break;
4464 }
4465
4466 if (!rc)
4467 return 0;
4468
4469 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4470 "handle(0x%04x)\n", ioc->name, desc,
4471 (unsigned long long)sas_address, handle);
4472 return rc;
4473}
4474
4475static void
4476_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4477{
4478 Mpi2ConfigReply_t mpi_reply;
4479 Mpi2SasDevicePage0_t sas_device_pg0;
4480 struct _sas_device *sas_device;
4481 u32 ioc_status;
4482 unsigned long flags;
4483 u64 sas_address;
4484 struct scsi_target *starget;
4485 struct MPT2SAS_TARGET *sas_target_priv_data;
4486 u32 device_info;
4487
4488 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4489 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4490 return;
4491
4492 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4493 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4494 return;
4495
4496 /* check if this is end device */
4497 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4498 if (!(_scsih_is_end_device(device_info)))
4499 return;
4500
4501 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4502 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4503 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4504 sas_address);
4505
4506 if (!sas_device) {
4507 printk(MPT2SAS_ERR_FMT "device is not present "
4508 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4509 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4510 return;
4511 }
4512
4513 if (unlikely(sas_device->handle != handle)) {
4514 starget = sas_device->starget;
4515 sas_target_priv_data = starget->hostdata;
4516 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4517 " to (0x%04x)!!!\n", sas_device->handle, handle);
4518 sas_target_priv_data->handle = handle;
4519 sas_device->handle = handle;
4520 }
4521 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4522
4523 /* check if device is present */
4524 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4525 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4526 printk(MPT2SAS_ERR_FMT "device is not present "
4527 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4528 return;
4529 }
4530
4531 /* check if there were any issues with discovery */
4532 if (_scsih_check_access_status(ioc, sas_address, handle,
4533 sas_device_pg0.AccessStatus))
4534 return;
4535 _scsih_ublock_io_device(ioc, handle);
4536
4537}
4538
4539/**
Eric Moore635374e2009-03-09 01:21:12 -06004540 * _scsih_add_device - creating sas device object
4541 * @ioc: per adapter object
4542 * @handle: sas device handle
4543 * @phy_num: phy number end device attached to
4544 * @is_pd: is this hidden raid component
4545 *
4546 * Creating end device object, stored in ioc->sas_device_list.
4547 *
4548 * Returns 0 for success, non-zero for failure.
4549 */
4550static int
4551_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4552{
4553 Mpi2ConfigReply_t mpi_reply;
4554 Mpi2SasDevicePage0_t sas_device_pg0;
4555 Mpi2SasEnclosurePage0_t enclosure_pg0;
4556 struct _sas_device *sas_device;
4557 u32 ioc_status;
4558 __le64 sas_address;
4559 u32 device_info;
4560 unsigned long flags;
4561
4562 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4563 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4564 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4565 ioc->name, __FILE__, __LINE__, __func__);
4566 return -1;
4567 }
4568
4569 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4570 MPI2_IOCSTATUS_MASK;
4571 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4572 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4573 ioc->name, __FILE__, __LINE__, __func__);
4574 return -1;
4575 }
4576
Kashyap, Desaib4344272010-03-17 16:24:14 +05304577 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4578
Eric Moore635374e2009-03-09 01:21:12 -06004579 /* check if device is present */
4580 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4581 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4582 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4583 ioc->name, __FILE__, __LINE__, __func__);
4584 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4585 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4586 return -1;
4587 }
4588
Kashyap, Desaib4344272010-03-17 16:24:14 +05304589 /* check if there were any issues with discovery */
4590 if (_scsih_check_access_status(ioc, sas_address, handle,
4591 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06004592 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06004593
4594 /* check if this is end device */
4595 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4596 if (!(_scsih_is_end_device(device_info))) {
4597 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4598 ioc->name, __FILE__, __LINE__, __func__);
4599 return -1;
4600 }
4601
Eric Moore635374e2009-03-09 01:21:12 -06004602
4603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4604 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4605 sas_address);
4606 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4607
Kashyap, Desaib4344272010-03-17 16:24:14 +05304608 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004609 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06004610
4611 sas_device = kzalloc(sizeof(struct _sas_device),
4612 GFP_KERNEL);
4613 if (!sas_device) {
4614 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4615 ioc->name, __FILE__, __LINE__, __func__);
4616 return -1;
4617 }
4618
4619 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304620 if (_scsih_get_sas_address(ioc, le16_to_cpu
4621 (sas_device_pg0.ParentDevHandle),
4622 &sas_device->sas_address_parent) != 0)
4623 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4624 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004625 sas_device->enclosure_handle =
4626 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4627 sas_device->slot =
4628 le16_to_cpu(sas_device_pg0.Slot);
4629 sas_device->device_info = device_info;
4630 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304631 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06004632
4633 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304634 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4635 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4636 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004637 sas_device->enclosure_logical_id =
4638 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004639
4640 /* get device name */
4641 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4642
4643 if (ioc->wait_for_port_enable_to_complete)
4644 _scsih_sas_device_init_add(ioc, sas_device);
4645 else
4646 _scsih_sas_device_add(ioc, sas_device);
4647
4648 return 0;
4649}
4650
4651/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05304652 * _scsih_remove_device - removing sas device object
4653 * @ioc: per adapter object
4654 * @sas_device_delete: the sas_device object
4655 *
4656 * Return nothing.
4657 */
4658static void
4659_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4660 struct _sas_device *sas_device)
4661{
4662 struct _sas_device sas_device_backup;
4663 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304664
Kashyap, Desai1278b112010-03-09 17:34:13 +05304665 if (!sas_device)
4666 return;
Eric Moore635374e2009-03-09 01:21:12 -06004667
Kashyap, Desai1278b112010-03-09 17:34:13 +05304668 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06004669 _scsih_sas_device_remove(ioc, sas_device);
4670
Kashyap, Desai1278b112010-03-09 17:34:13 +05304671 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4672 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4673 sas_device_backup.handle, (unsigned long long)
4674 sas_device_backup.sas_address));
4675
4676 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4677 sas_target_priv_data = sas_device_backup.starget->hostdata;
4678 sas_target_priv_data->deleted = 1;
4679 }
4680
Kashyap, Desai1278b112010-03-09 17:34:13 +05304681 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4682
4683 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4684 sas_device_backup.sas_address_parent);
4685
4686 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4687 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4688 (unsigned long long) sas_device_backup.sas_address);
4689
4690 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4691 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4692 sas_device_backup.handle, (unsigned long long)
4693 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004694}
4695
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304696/**
4697 * mpt2sas_device_remove - removing device object
4698 * @ioc: per adapter object
4699 * @sas_address: expander sas_address
4700 *
4701 * Return nothing.
4702 */
4703void
4704mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4705{
4706 struct _sas_device *sas_device;
4707 unsigned long flags;
4708
4709 if (ioc->shost_recovery)
4710 return;
4711
4712 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4713 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4714 sas_address);
4715 if (!sas_device) {
4716 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4717 return;
4718 }
4719 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4720 _scsih_remove_device(ioc, sas_device);
4721}
4722
Eric Moore635374e2009-03-09 01:21:12 -06004723#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4724/**
4725 * _scsih_sas_topology_change_event_debug - debug for topology event
4726 * @ioc: per adapter object
4727 * @event_data: event data payload
4728 * Context: user.
4729 */
4730static void
4731_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4732 Mpi2EventDataSasTopologyChangeList_t *event_data)
4733{
4734 int i;
4735 u16 handle;
4736 u16 reason_code;
4737 u8 phy_number;
4738 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304739 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004740
4741 switch (event_data->ExpStatus) {
4742 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4743 status_str = "add";
4744 break;
4745 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4746 status_str = "remove";
4747 break;
4748 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304749 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004750 status_str = "responding";
4751 break;
4752 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4753 status_str = "remove delay";
4754 break;
4755 default:
4756 status_str = "unknown status";
4757 break;
4758 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304759 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06004760 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304761 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06004762 "start_phy(%02d), count(%d)\n",
4763 le16_to_cpu(event_data->ExpanderDevHandle),
4764 le16_to_cpu(event_data->EnclosureHandle),
4765 event_data->StartPhyNum, event_data->NumEntries);
4766 for (i = 0; i < event_data->NumEntries; i++) {
4767 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4768 if (!handle)
4769 continue;
4770 phy_number = event_data->StartPhyNum + i;
4771 reason_code = event_data->PHY[i].PhyStatus &
4772 MPI2_EVENT_SAS_TOPO_RC_MASK;
4773 switch (reason_code) {
4774 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304775 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004776 break;
4777 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304778 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004779 break;
4780 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304781 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004782 break;
4783 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304784 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004785 break;
4786 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304787 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004788 break;
4789 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304790 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004791 break;
4792 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304793 link_rate = event_data->PHY[i].LinkRate >> 4;
4794 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304795 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304796 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4797 handle, status_str, link_rate, prev_link_rate);
4798
Eric Moore635374e2009-03-09 01:21:12 -06004799 }
4800}
4801#endif
4802
4803/**
4804 * _scsih_sas_topology_change_event - handle topology changes
4805 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304806 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004807 * Context: user.
4808 *
4809 */
4810static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304811_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004812 struct fw_event_work *fw_event)
4813{
4814 int i;
4815 u16 parent_handle, handle;
4816 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304817 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06004818 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304819 struct _sas_device *sas_device;
4820 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004821 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304822 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304823 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004824
4825#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4826 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4827 _scsih_sas_topology_change_event_debug(ioc, event_data);
4828#endif
4829
Eric Moore3cb54692010-07-08 14:44:34 -06004830 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304831 return;
4832
Eric Moore635374e2009-03-09 01:21:12 -06004833 if (!ioc->sas_hba.num_phys)
4834 _scsih_sas_host_add(ioc);
4835 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304836 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004837
4838 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304839 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06004840 "event\n", ioc->name));
4841 return;
4842 }
4843
4844 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4845
4846 /* handle expander add */
4847 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4848 if (_scsih_expander_add(ioc, parent_handle) != 0)
4849 return;
4850
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304851 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4852 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4853 parent_handle);
4854 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304855 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304856 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304857 max_phys = sas_expander->num_phys;
4858 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304859 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304860 max_phys = ioc->sas_hba.num_phys;
4861 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304862 return;
4863
Eric Moore635374e2009-03-09 01:21:12 -06004864 /* handle siblings events */
4865 for (i = 0; i < event_data->NumEntries; i++) {
4866 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304867 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06004868 "expander event\n", ioc->name));
4869 return;
4870 }
Eric Moore3cb54692010-07-08 14:44:34 -06004871 if (ioc->shost_recovery || ioc->remove_host ||
4872 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304873 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304874 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304875 if (phy_number >= max_phys)
4876 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304877 reason_code = event_data->PHY[i].PhyStatus &
4878 MPI2_EVENT_SAS_TOPO_RC_MASK;
4879 if ((event_data->PHY[i].PhyStatus &
4880 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4881 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004882 continue;
4883 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4884 if (!handle)
4885 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304886 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304887 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004888 switch (reason_code) {
4889 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304890
4891 if (link_rate == prev_link_rate)
4892 break;
4893
4894 mpt2sas_transport_update_links(ioc, sas_address,
4895 handle, phy_number, link_rate);
4896
Kashyap, Desaib4344272010-03-17 16:24:14 +05304897 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4898 break;
4899
4900 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304901 break;
Eric Moore635374e2009-03-09 01:21:12 -06004902 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304903
4904 mpt2sas_transport_update_links(ioc, sas_address,
4905 handle, phy_number, link_rate);
4906
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304907 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004908 break;
4909 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304910
4911 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4912 sas_device = _scsih_sas_device_find_by_handle(ioc,
4913 handle);
4914 if (!sas_device) {
4915 spin_unlock_irqrestore(&ioc->sas_device_lock,
4916 flags);
4917 break;
4918 }
4919 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4920 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004921 break;
4922 }
4923 }
4924
4925 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304926 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4927 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304928 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004929
4930}
4931
4932#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4933/**
4934 * _scsih_sas_device_status_change_event_debug - debug for device event
4935 * @event_data: event data payload
4936 * Context: user.
4937 *
4938 * Return nothing.
4939 */
4940static void
4941_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4942 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4943{
4944 char *reason_str = NULL;
4945
4946 switch (event_data->ReasonCode) {
4947 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4948 reason_str = "smart data";
4949 break;
4950 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4951 reason_str = "unsupported device discovered";
4952 break;
4953 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4954 reason_str = "internal device reset";
4955 break;
4956 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4957 reason_str = "internal task abort";
4958 break;
4959 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4960 reason_str = "internal task abort set";
4961 break;
4962 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4963 reason_str = "internal clear task set";
4964 break;
4965 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4966 reason_str = "internal query task";
4967 break;
4968 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4969 reason_str = "sata init failure";
4970 break;
4971 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4972 reason_str = "internal device reset complete";
4973 break;
4974 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4975 reason_str = "internal task abort complete";
4976 break;
4977 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4978 reason_str = "internal async notification";
4979 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304980 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4981 reason_str = "expander reduced functionality";
4982 break;
4983 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4984 reason_str = "expander reduced functionality complete";
4985 break;
Eric Moore635374e2009-03-09 01:21:12 -06004986 default:
4987 reason_str = "unknown reason";
4988 break;
4989 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304990 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06004991 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4992 reason_str, le16_to_cpu(event_data->DevHandle),
4993 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4994 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304995 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004996 event_data->ASC, event_data->ASCQ);
4997 printk(KERN_INFO "\n");
4998}
4999#endif
5000
5001/**
5002 * _scsih_sas_device_status_change_event - handle device status change
5003 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305004 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005005 * Context: user.
5006 *
5007 * Return nothing.
5008 */
5009static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305010_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5011 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005012{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305013 struct MPT2SAS_TARGET *target_priv_data;
5014 struct _sas_device *sas_device;
5015 __le64 sas_address;
5016 unsigned long flags;
5017 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5018 fw_event->event_data;
5019
Eric Moore635374e2009-03-09 01:21:12 -06005020#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5021 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305022 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305023 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005024#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305025
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305026 /* In MPI Revision K (0xC), the internal device reset complete was
5027 * implemented, so avoid setting tm_busy flag for older firmware.
5028 */
5029 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5030 return;
5031
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305032 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305033 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305034 event_data->ReasonCode !=
5035 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305036 return;
5037
5038 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5039 sas_address = le64_to_cpu(event_data->SASAddress);
5040 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5041 sas_address);
5042 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5043
5044 if (!sas_device || !sas_device->starget)
5045 return;
5046
5047 target_priv_data = sas_device->starget->hostdata;
5048 if (!target_priv_data)
5049 return;
5050
5051 if (event_data->ReasonCode ==
5052 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5053 target_priv_data->tm_busy = 1;
5054 else
5055 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005056}
5057
5058#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5059/**
5060 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5061 * @ioc: per adapter object
5062 * @event_data: event data payload
5063 * Context: user.
5064 *
5065 * Return nothing.
5066 */
5067static void
5068_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5069 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5070{
5071 char *reason_str = NULL;
5072
5073 switch (event_data->ReasonCode) {
5074 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5075 reason_str = "enclosure add";
5076 break;
5077 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5078 reason_str = "enclosure remove";
5079 break;
5080 default:
5081 reason_str = "unknown reason";
5082 break;
5083 }
5084
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305085 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005086 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5087 " number slots(%d)\n", ioc->name, reason_str,
5088 le16_to_cpu(event_data->EnclosureHandle),
5089 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5090 le16_to_cpu(event_data->StartSlot));
5091}
5092#endif
5093
5094/**
5095 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5096 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305097 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005098 * Context: user.
5099 *
5100 * Return nothing.
5101 */
5102static void
5103_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305104 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005105{
5106#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5107 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5108 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305109 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005110#endif
5111}
5112
5113/**
5114 * _scsih_sas_broadcast_primative_event - handle broadcast events
5115 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305116 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005117 * Context: user.
5118 *
5119 * Return nothing.
5120 */
5121static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305122_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5123 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005124{
5125 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305126 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005127 u16 smid, handle;
5128 u32 lun;
5129 struct MPT2SAS_DEVICE *sas_device_priv_data;
5130 u32 termination_count;
5131 u32 query_count;
5132 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305133#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5134 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5135#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05305136 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305137 unsigned long flags;
5138 int r;
5139
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005140 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primitive: "
Eric Moore635374e2009-03-09 01:21:12 -06005141 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5142 event_data->PortWidth));
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305143 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005144 __func__));
5145
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305146 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5147 ioc->broadcast_aen_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005148 termination_count = 0;
5149 query_count = 0;
5150 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305151 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06005152 scmd = _scsih_scsi_lookup_get(ioc, smid);
5153 if (!scmd)
5154 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305155 sdev = scmd->device;
5156 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005157 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5158 continue;
5159 /* skip hidden raid components */
5160 if (sas_device_priv_data->sas_target->flags &
5161 MPT_TARGET_FLAGS_RAID_COMPONENT)
5162 continue;
5163 /* skip volumes */
5164 if (sas_device_priv_data->sas_target->flags &
5165 MPT_TARGET_FLAGS_VOLUME)
5166 continue;
5167
5168 handle = sas_device_priv_data->sas_target->handle;
5169 lun = sas_device_priv_data->lun;
5170 query_count++;
5171
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305172 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305173 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5174 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
Eric Moore8901cbb2009-04-21 15:41:32 -06005175 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305176 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5177 & MPI2_IOCSTATUS_MASK;
5178 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06005179 (mpi_reply->ResponseCode ==
5180 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5181 mpi_reply->ResponseCode ==
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305182 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC)) {
5183 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005184 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305185 }
5186 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5187 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5188 scmd);
5189 if (r == FAILED)
5190 sdev_printk(KERN_WARNING, sdev, "task abort: FAILED "
5191 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06005192 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305193 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005194 }
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305195 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005196
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305197 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005198 "%s - exit, query_count = %d termination_count = %d\n",
5199 ioc->name, __func__, query_count, termination_count));
5200}
5201
5202/**
5203 * _scsih_sas_discovery_event - handle discovery events
5204 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305205 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005206 * Context: user.
5207 *
5208 * Return nothing.
5209 */
5210static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305211_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5212 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005213{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305214 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5215
Eric Moore635374e2009-03-09 01:21:12 -06005216#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5217 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305218 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005219 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5220 "start" : "stop");
5221 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305222 printk("discovery_status(0x%08x)",
5223 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005224 printk("\n");
5225 }
5226#endif
5227
5228 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5229 !ioc->sas_hba.num_phys)
5230 _scsih_sas_host_add(ioc);
5231}
5232
5233/**
5234 * _scsih_reprobe_lun - reprobing lun
5235 * @sdev: scsi device struct
5236 * @no_uld_attach: sdev->no_uld_attach flag setting
5237 *
5238 **/
5239static void
5240_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5241{
5242 int rc;
5243
5244 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5245 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5246 sdev->no_uld_attach ? "hidding" : "exposing");
5247 rc = scsi_device_reprobe(sdev);
5248}
5249
5250/**
5251 * _scsih_reprobe_target - reprobing target
5252 * @starget: scsi target struct
5253 * @no_uld_attach: sdev->no_uld_attach flag setting
5254 *
5255 * Note: no_uld_attach flag determines whether the disk device is attached
5256 * to block layer. A value of `1` means to not attach.
5257 **/
5258static void
5259_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5260{
5261 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5262
5263 if (no_uld_attach)
5264 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5265 else
5266 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5267
5268 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5269 _scsih_reprobe_lun);
5270}
5271/**
5272 * _scsih_sas_volume_add - add new volume
5273 * @ioc: per adapter object
5274 * @element: IR config element data
5275 * Context: user.
5276 *
5277 * Return nothing.
5278 */
5279static void
5280_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5281 Mpi2EventIrConfigElement_t *element)
5282{
5283 struct _raid_device *raid_device;
5284 unsigned long flags;
5285 u64 wwid;
5286 u16 handle = le16_to_cpu(element->VolDevHandle);
5287 int rc;
5288
Eric Moore635374e2009-03-09 01:21:12 -06005289 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5290 if (!wwid) {
5291 printk(MPT2SAS_ERR_FMT
5292 "failure at %s:%d/%s()!\n", ioc->name,
5293 __FILE__, __LINE__, __func__);
5294 return;
5295 }
5296
5297 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5298 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5299 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5300
5301 if (raid_device)
5302 return;
5303
5304 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5305 if (!raid_device) {
5306 printk(MPT2SAS_ERR_FMT
5307 "failure at %s:%d/%s()!\n", ioc->name,
5308 __FILE__, __LINE__, __func__);
5309 return;
5310 }
5311
5312 raid_device->id = ioc->sas_id++;
5313 raid_device->channel = RAID_CHANNEL;
5314 raid_device->handle = handle;
5315 raid_device->wwid = wwid;
5316 _scsih_raid_device_add(ioc, raid_device);
5317 if (!ioc->wait_for_port_enable_to_complete) {
5318 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5319 raid_device->id, 0);
5320 if (rc)
5321 _scsih_raid_device_remove(ioc, raid_device);
5322 } else
5323 _scsih_determine_boot_device(ioc, raid_device, 1);
5324}
5325
5326/**
5327 * _scsih_sas_volume_delete - delete volume
5328 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305329 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005330 * Context: user.
5331 *
5332 * Return nothing.
5333 */
5334static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305335_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005336{
5337 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06005338 unsigned long flags;
5339 struct MPT2SAS_TARGET *sas_target_priv_data;
5340
Eric Moore635374e2009-03-09 01:21:12 -06005341 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5342 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5343 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5344 if (!raid_device)
5345 return;
5346 if (raid_device->starget) {
5347 sas_target_priv_data = raid_device->starget->hostdata;
5348 sas_target_priv_data->deleted = 1;
5349 scsi_remove_target(&raid_device->starget->dev);
5350 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305351 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5352 "(0x%016llx)\n", ioc->name, raid_device->handle,
5353 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06005354 _scsih_raid_device_remove(ioc, raid_device);
5355}
5356
5357/**
5358 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5359 * @ioc: per adapter object
5360 * @element: IR config element data
5361 * Context: user.
5362 *
5363 * Return nothing.
5364 */
5365static void
5366_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5367 Mpi2EventIrConfigElement_t *element)
5368{
5369 struct _sas_device *sas_device;
5370 unsigned long flags;
5371 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5372
5373 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5374 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5375 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5376 if (!sas_device)
5377 return;
5378
5379 /* exposing raid component */
5380 sas_device->volume_handle = 0;
5381 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305382 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005383 _scsih_reprobe_target(sas_device->starget, 0);
5384}
5385
5386/**
5387 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5388 * @ioc: per adapter object
5389 * @element: IR config element data
5390 * Context: user.
5391 *
5392 * Return nothing.
5393 */
5394static void
5395_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5396 Mpi2EventIrConfigElement_t *element)
5397{
5398 struct _sas_device *sas_device;
5399 unsigned long flags;
5400 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5401
5402 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5403 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5404 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5405 if (!sas_device)
5406 return;
5407
5408 /* hiding raid component */
5409 mpt2sas_config_get_volume_handle(ioc, handle,
5410 &sas_device->volume_handle);
5411 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5412 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305413 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005414 _scsih_reprobe_target(sas_device->starget, 1);
5415}
5416
5417/**
5418 * _scsih_sas_pd_delete - delete pd component
5419 * @ioc: per adapter object
5420 * @element: IR config element data
5421 * Context: user.
5422 *
5423 * Return nothing.
5424 */
5425static void
5426_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5427 Mpi2EventIrConfigElement_t *element)
5428{
5429 struct _sas_device *sas_device;
5430 unsigned long flags;
5431 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5432
5433 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5434 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5435 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5436 if (!sas_device)
5437 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305438 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005439}
5440
5441/**
5442 * _scsih_sas_pd_add - remove pd component
5443 * @ioc: per adapter object
5444 * @element: IR config element data
5445 * Context: user.
5446 *
5447 * Return nothing.
5448 */
5449static void
5450_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5451 Mpi2EventIrConfigElement_t *element)
5452{
5453 struct _sas_device *sas_device;
5454 unsigned long flags;
5455 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305456 Mpi2ConfigReply_t mpi_reply;
5457 Mpi2SasDevicePage0_t sas_device_pg0;
5458 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305459 u64 sas_address;
5460 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005461
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305462 set_bit(handle, ioc->pd_handles);
5463
Eric Moore635374e2009-03-09 01:21:12 -06005464 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5465 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5466 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305467 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305468 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305469
5470 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5471 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5472 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5473 ioc->name, __FILE__, __LINE__, __func__);
5474 return;
5475 }
5476
5477 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5478 MPI2_IOCSTATUS_MASK;
5479 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5480 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5481 ioc->name, __FILE__, __LINE__, __func__);
5482 return;
5483 }
5484
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305485 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5486 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5487 mpt2sas_transport_update_links(ioc, sas_address, handle,
5488 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305489
5490 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06005491}
5492
5493#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5494/**
5495 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5496 * @ioc: per adapter object
5497 * @event_data: event data payload
5498 * Context: user.
5499 *
5500 * Return nothing.
5501 */
5502static void
5503_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5504 Mpi2EventDataIrConfigChangeList_t *event_data)
5505{
5506 Mpi2EventIrConfigElement_t *element;
5507 u8 element_type;
5508 int i;
5509 char *reason_str = NULL, *element_str = NULL;
5510
5511 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5512
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305513 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005514 ioc->name, (le32_to_cpu(event_data->Flags) &
5515 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5516 "foreign" : "native", event_data->NumElements);
5517 for (i = 0; i < event_data->NumElements; i++, element++) {
5518 switch (element->ReasonCode) {
5519 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5520 reason_str = "add";
5521 break;
5522 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5523 reason_str = "remove";
5524 break;
5525 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5526 reason_str = "no change";
5527 break;
5528 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5529 reason_str = "hide";
5530 break;
5531 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5532 reason_str = "unhide";
5533 break;
5534 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5535 reason_str = "volume_created";
5536 break;
5537 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5538 reason_str = "volume_deleted";
5539 break;
5540 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5541 reason_str = "pd_created";
5542 break;
5543 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5544 reason_str = "pd_deleted";
5545 break;
5546 default:
5547 reason_str = "unknown reason";
5548 break;
5549 }
5550 element_type = le16_to_cpu(element->ElementFlags) &
5551 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5552 switch (element_type) {
5553 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5554 element_str = "volume";
5555 break;
5556 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5557 element_str = "phys disk";
5558 break;
5559 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5560 element_str = "hot spare";
5561 break;
5562 default:
5563 element_str = "unknown element";
5564 break;
5565 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305566 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005567 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5568 reason_str, le16_to_cpu(element->VolDevHandle),
5569 le16_to_cpu(element->PhysDiskDevHandle),
5570 element->PhysDiskNum);
5571 }
5572}
5573#endif
5574
5575/**
5576 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5577 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305578 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005579 * Context: user.
5580 *
5581 * Return nothing.
5582 */
5583static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305584_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5585 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005586{
5587 Mpi2EventIrConfigElement_t *element;
5588 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305589 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305590 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005591
5592#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5593 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5594 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5595
5596#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305597 foreign_config = (le32_to_cpu(event_data->Flags) &
5598 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005599
5600 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5601 for (i = 0; i < event_data->NumElements; i++, element++) {
5602
5603 switch (element->ReasonCode) {
5604 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5605 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305606 if (!foreign_config)
5607 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005608 break;
5609 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5610 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305611 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305612 _scsih_sas_volume_delete(ioc,
5613 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06005614 break;
5615 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5616 _scsih_sas_pd_hide(ioc, element);
5617 break;
5618 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5619 _scsih_sas_pd_expose(ioc, element);
5620 break;
5621 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5622 _scsih_sas_pd_add(ioc, element);
5623 break;
5624 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5625 _scsih_sas_pd_delete(ioc, element);
5626 break;
5627 }
5628 }
5629}
5630
5631/**
5632 * _scsih_sas_ir_volume_event - IR volume event
5633 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305634 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005635 * Context: user.
5636 *
5637 * Return nothing.
5638 */
5639static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305640_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5641 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005642{
5643 u64 wwid;
5644 unsigned long flags;
5645 struct _raid_device *raid_device;
5646 u16 handle;
5647 u32 state;
5648 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305649 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005650
5651 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5652 return;
5653
5654 handle = le16_to_cpu(event_data->VolDevHandle);
5655 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305656 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005657 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5658 le32_to_cpu(event_data->PreviousValue), state));
5659
Eric Moore635374e2009-03-09 01:21:12 -06005660 switch (state) {
5661 case MPI2_RAID_VOL_STATE_MISSING:
5662 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305663 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005664 break;
5665
5666 case MPI2_RAID_VOL_STATE_ONLINE:
5667 case MPI2_RAID_VOL_STATE_DEGRADED:
5668 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305669
5670 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5671 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5672 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5673
Eric Moore635374e2009-03-09 01:21:12 -06005674 if (raid_device)
5675 break;
5676
5677 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5678 if (!wwid) {
5679 printk(MPT2SAS_ERR_FMT
5680 "failure at %s:%d/%s()!\n", ioc->name,
5681 __FILE__, __LINE__, __func__);
5682 break;
5683 }
5684
5685 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5686 if (!raid_device) {
5687 printk(MPT2SAS_ERR_FMT
5688 "failure at %s:%d/%s()!\n", ioc->name,
5689 __FILE__, __LINE__, __func__);
5690 break;
5691 }
5692
5693 raid_device->id = ioc->sas_id++;
5694 raid_device->channel = RAID_CHANNEL;
5695 raid_device->handle = handle;
5696 raid_device->wwid = wwid;
5697 _scsih_raid_device_add(ioc, raid_device);
5698 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5699 raid_device->id, 0);
5700 if (rc)
5701 _scsih_raid_device_remove(ioc, raid_device);
5702 break;
5703
5704 case MPI2_RAID_VOL_STATE_INITIALIZING:
5705 default:
5706 break;
5707 }
5708}
5709
5710/**
5711 * _scsih_sas_ir_physical_disk_event - PD event
5712 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305713 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005714 * Context: user.
5715 *
5716 * Return nothing.
5717 */
5718static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305719_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5720 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005721{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305722 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005723 u32 state;
5724 struct _sas_device *sas_device;
5725 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305726 Mpi2ConfigReply_t mpi_reply;
5727 Mpi2SasDevicePage0_t sas_device_pg0;
5728 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305729 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305730 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005731
5732 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5733 return;
5734
5735 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5736 state = le32_to_cpu(event_data->NewValue);
5737
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305738 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005739 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5740 le32_to_cpu(event_data->PreviousValue), state));
5741
Eric Moore635374e2009-03-09 01:21:12 -06005742 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005743 case MPI2_RAID_PD_STATE_ONLINE:
5744 case MPI2_RAID_PD_STATE_DEGRADED:
5745 case MPI2_RAID_PD_STATE_REBUILDING:
5746 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305747 case MPI2_RAID_PD_STATE_HOT_SPARE:
5748
5749 set_bit(handle, ioc->pd_handles);
5750
5751 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5752 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5753 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5754
5755 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305756 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305757
5758 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5759 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5760 handle))) {
5761 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5762 ioc->name, __FILE__, __LINE__, __func__);
5763 return;
5764 }
5765
5766 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5767 MPI2_IOCSTATUS_MASK;
5768 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5769 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5770 ioc->name, __FILE__, __LINE__, __func__);
5771 return;
5772 }
5773
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305774 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5775 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5776 mpt2sas_transport_update_links(ioc, sas_address, handle,
5777 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305778
5779 _scsih_add_device(ioc, handle, 0, 1);
5780
Eric Moore635374e2009-03-09 01:21:12 -06005781 break;
5782
Kashyap, Desai62727a72009-08-07 19:35:18 +05305783 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005784 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5785 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06005786 default:
5787 break;
5788 }
5789}
5790
5791#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5792/**
5793 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5794 * @ioc: per adapter object
5795 * @event_data: event data payload
5796 * Context: user.
5797 *
5798 * Return nothing.
5799 */
5800static void
5801_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5802 Mpi2EventDataIrOperationStatus_t *event_data)
5803{
5804 char *reason_str = NULL;
5805
5806 switch (event_data->RAIDOperation) {
5807 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5808 reason_str = "resync";
5809 break;
5810 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5811 reason_str = "online capacity expansion";
5812 break;
5813 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5814 reason_str = "consistency check";
5815 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305816 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5817 reason_str = "background init";
5818 break;
5819 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5820 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005821 break;
5822 }
5823
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305824 if (!reason_str)
5825 return;
5826
Eric Moore635374e2009-03-09 01:21:12 -06005827 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5828 "\thandle(0x%04x), percent complete(%d)\n",
5829 ioc->name, reason_str,
5830 le16_to_cpu(event_data->VolDevHandle),
5831 event_data->PercentComplete);
5832}
5833#endif
5834
5835/**
5836 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5837 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305838 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005839 * Context: user.
5840 *
5841 * Return nothing.
5842 */
5843static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305844_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5845 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005846{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305847 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5848 static struct _raid_device *raid_device;
5849 unsigned long flags;
5850 u16 handle;
5851
Eric Moore635374e2009-03-09 01:21:12 -06005852#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5853 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305854 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305855 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005856#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305857
5858 /* code added for raid transport support */
5859 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5860
5861 handle = le16_to_cpu(event_data->VolDevHandle);
5862
5863 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5864 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5865 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5866
5867 if (!raid_device)
5868 return;
5869
5870 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5871 raid_device->percent_complete =
5872 event_data->PercentComplete;
5873 }
Eric Moore635374e2009-03-09 01:21:12 -06005874}
5875
5876/**
Kashyap, Desai14695852010-03-30 10:52:44 +05305877 * _scsih_prep_device_scan - initialize parameters prior to device scan
5878 * @ioc: per adapter object
5879 *
5880 * Set the deleted flag prior to device scan. If the device is found during
5881 * the scan, then we clear the deleted flag.
5882 */
5883static void
5884_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5885{
5886 struct MPT2SAS_DEVICE *sas_device_priv_data;
5887 struct scsi_device *sdev;
5888
5889 shost_for_each_device(sdev, ioc->shost) {
5890 sas_device_priv_data = sdev->hostdata;
5891 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5892 sas_device_priv_data->sas_target->deleted = 1;
5893 }
5894}
5895
5896/**
Eric Moore635374e2009-03-09 01:21:12 -06005897 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5898 * @ioc: per adapter object
5899 * @sas_address: sas address
5900 * @slot: enclosure slot id
5901 * @handle: device handle
5902 *
5903 * After host reset, find out whether devices are still responding.
5904 * Used in _scsi_remove_unresponsive_sas_devices.
5905 *
5906 * Return nothing.
5907 */
5908static void
5909_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5910 u16 slot, u16 handle)
5911{
5912 struct MPT2SAS_TARGET *sas_target_priv_data;
5913 struct scsi_target *starget;
5914 struct _sas_device *sas_device;
5915 unsigned long flags;
5916
5917 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5918 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5919 if (sas_device->sas_address == sas_address &&
5920 sas_device->slot == slot && sas_device->starget) {
5921 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305922 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05305923 if (starget && starget->hostdata) {
5924 sas_target_priv_data = starget->hostdata;
5925 sas_target_priv_data->tm_busy = 0;
5926 sas_target_priv_data->deleted = 0;
5927 } else
5928 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005929 starget_printk(KERN_INFO, sas_device->starget,
5930 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5931 "logical id(0x%016llx), slot(%d)\n", handle,
5932 (unsigned long long)sas_device->sas_address,
5933 (unsigned long long)
5934 sas_device->enclosure_logical_id,
5935 sas_device->slot);
5936 if (sas_device->handle == handle)
5937 goto out;
5938 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5939 sas_device->handle);
5940 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305941 if (sas_target_priv_data)
5942 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005943 goto out;
5944 }
5945 }
5946 out:
5947 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5948}
5949
5950/**
5951 * _scsih_search_responding_sas_devices -
5952 * @ioc: per adapter object
5953 *
5954 * After host reset, find out whether devices are still responding.
5955 * If not remove.
5956 *
5957 * Return nothing.
5958 */
5959static void
5960_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5961{
5962 Mpi2SasDevicePage0_t sas_device_pg0;
5963 Mpi2ConfigReply_t mpi_reply;
5964 u16 ioc_status;
5965 __le64 sas_address;
5966 u16 handle;
5967 u32 device_info;
5968 u16 slot;
5969
5970 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5971
5972 if (list_empty(&ioc->sas_device_list))
5973 return;
5974
5975 handle = 0xFFFF;
5976 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5977 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5978 handle))) {
5979 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5980 MPI2_IOCSTATUS_MASK;
5981 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5982 break;
5983 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5984 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5985 if (!(_scsih_is_end_device(device_info)))
5986 continue;
5987 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5988 slot = le16_to_cpu(sas_device_pg0.Slot);
5989 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5990 handle);
5991 }
5992}
5993
5994/**
5995 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5996 * @ioc: per adapter object
5997 * @wwid: world wide identifier for raid volume
5998 * @handle: device handle
5999 *
6000 * After host reset, find out whether devices are still responding.
6001 * Used in _scsi_remove_unresponsive_raid_devices.
6002 *
6003 * Return nothing.
6004 */
6005static void
6006_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6007 u16 handle)
6008{
6009 struct MPT2SAS_TARGET *sas_target_priv_data;
6010 struct scsi_target *starget;
6011 struct _raid_device *raid_device;
6012 unsigned long flags;
6013
6014 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6015 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6016 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306017 starget = raid_device->starget;
6018 if (starget && starget->hostdata) {
6019 sas_target_priv_data = starget->hostdata;
6020 sas_target_priv_data->deleted = 0;
6021 } else
6022 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006023 raid_device->responding = 1;
6024 starget_printk(KERN_INFO, raid_device->starget,
6025 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6026 (unsigned long long)raid_device->wwid);
6027 if (raid_device->handle == handle)
6028 goto out;
6029 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6030 raid_device->handle);
6031 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306032 if (sas_target_priv_data)
6033 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006034 goto out;
6035 }
6036 }
6037 out:
6038 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6039}
6040
6041/**
6042 * _scsih_search_responding_raid_devices -
6043 * @ioc: per adapter object
6044 *
6045 * After host reset, find out whether devices are still responding.
6046 * If not remove.
6047 *
6048 * Return nothing.
6049 */
6050static void
6051_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6052{
6053 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306054 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306055 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006056 Mpi2ConfigReply_t mpi_reply;
6057 u16 ioc_status;
6058 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306059 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006060
6061 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6062
6063 if (list_empty(&ioc->raid_device_list))
6064 return;
6065
6066 handle = 0xFFFF;
6067 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6068 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6069 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6070 MPI2_IOCSTATUS_MASK;
6071 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6072 break;
6073 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306074
6075 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6076 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6077 sizeof(Mpi2RaidVolPage0_t)))
6078 continue;
6079
6080 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6081 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6082 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6083 _scsih_mark_responding_raid_device(ioc,
6084 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006085 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306086
6087 /* refresh the pd_handles */
6088 phys_disk_num = 0xFF;
6089 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6090 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6091 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6092 phys_disk_num))) {
6093 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6094 MPI2_IOCSTATUS_MASK;
6095 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6096 break;
6097 phys_disk_num = pd_pg0.PhysDiskNum;
6098 handle = le16_to_cpu(pd_pg0.DevHandle);
6099 set_bit(handle, ioc->pd_handles);
6100 }
Eric Moore635374e2009-03-09 01:21:12 -06006101}
6102
6103/**
6104 * _scsih_mark_responding_expander - mark a expander as responding
6105 * @ioc: per adapter object
6106 * @sas_address: sas address
6107 * @handle:
6108 *
6109 * After host reset, find out whether devices are still responding.
6110 * Used in _scsi_remove_unresponsive_expanders.
6111 *
6112 * Return nothing.
6113 */
6114static void
6115_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6116 u16 handle)
6117{
6118 struct _sas_node *sas_expander;
6119 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306120 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006121
6122 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6123 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306124 if (sas_expander->sas_address != sas_address)
6125 continue;
6126 sas_expander->responding = 1;
6127 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006128 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306129 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6130 " from(0x%04x) to (0x%04x)!!!\n",
6131 (unsigned long long)sas_expander->sas_address,
6132 sas_expander->handle, handle);
6133 sas_expander->handle = handle;
6134 for (i = 0 ; i < sas_expander->num_phys ; i++)
6135 sas_expander->phy[i].handle = handle;
6136 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006137 }
6138 out:
6139 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6140}
6141
6142/**
6143 * _scsih_search_responding_expanders -
6144 * @ioc: per adapter object
6145 *
6146 * After host reset, find out whether devices are still responding.
6147 * If not remove.
6148 *
6149 * Return nothing.
6150 */
6151static void
6152_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6153{
6154 Mpi2ExpanderPage0_t expander_pg0;
6155 Mpi2ConfigReply_t mpi_reply;
6156 u16 ioc_status;
6157 __le64 sas_address;
6158 u16 handle;
6159
6160 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6161
6162 if (list_empty(&ioc->sas_expander_list))
6163 return;
6164
6165 handle = 0xFFFF;
6166 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6167 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6168
6169 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6170 MPI2_IOCSTATUS_MASK;
6171 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6172 break;
6173
6174 handle = le16_to_cpu(expander_pg0.DevHandle);
6175 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6176 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6177 "sas_addr(0x%016llx)\n", handle,
6178 (unsigned long long)sas_address);
6179 _scsih_mark_responding_expander(ioc, sas_address, handle);
6180 }
6181
6182}
6183
6184/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306185 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006186 * @ioc: per adapter object
6187 *
6188 * Return nothing.
6189 */
6190static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306191_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006192{
6193 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306194 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006195 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006196
Eric Moore635374e2009-03-09 01:21:12 -06006197
6198 list_for_each_entry_safe(sas_device, sas_device_next,
6199 &ioc->sas_device_list, list) {
6200 if (sas_device->responding) {
6201 sas_device->responding = 0;
6202 continue;
6203 }
6204 if (sas_device->starget)
6205 starget_printk(KERN_INFO, sas_device->starget,
6206 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6207 "enclosure logical id(0x%016llx), slot(%d)\n",
6208 sas_device->handle,
6209 (unsigned long long)sas_device->sas_address,
6210 (unsigned long long)
6211 sas_device->enclosure_logical_id,
6212 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306213 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006214 }
6215
6216 list_for_each_entry_safe(raid_device, raid_device_next,
6217 &ioc->raid_device_list, list) {
6218 if (raid_device->responding) {
6219 raid_device->responding = 0;
6220 continue;
6221 }
6222 if (raid_device->starget) {
6223 starget_printk(KERN_INFO, raid_device->starget,
6224 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6225 raid_device->handle,
6226 (unsigned long long)raid_device->wwid);
6227 scsi_remove_target(&raid_device->starget->dev);
6228 }
6229 _scsih_raid_device_remove(ioc, raid_device);
6230 }
6231
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306232 retry_expander_search:
6233 sas_expander = NULL;
6234 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006235 if (sas_expander->responding) {
6236 sas_expander->responding = 0;
6237 continue;
6238 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306239 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306240 goto retry_expander_search;
6241 }
6242}
6243
6244/**
6245 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6246 * @ioc: per adapter object
6247 * @reset_phase: phase
6248 *
6249 * The handler for doing any required cleanup or initialization.
6250 *
6251 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6252 * MPT2_IOC_DONE_RESET
6253 *
6254 * Return nothing.
6255 */
6256void
6257mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6258{
6259 switch (reset_phase) {
6260 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306261 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306262 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306263 break;
6264 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306265 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306266 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306267 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6268 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6269 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6270 complete(&ioc->scsih_cmds.done);
6271 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306272 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6273 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6274 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6275 complete(&ioc->tm_cmds.done);
6276 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306277 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306278 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306279 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306280 break;
6281 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306282 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306283 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306284 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05306285 _scsih_prep_device_scan(ioc);
6286 _scsih_search_responding_sas_devices(ioc);
6287 _scsih_search_responding_raid_devices(ioc);
6288 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306289 break;
Eric Moore635374e2009-03-09 01:21:12 -06006290 }
6291}
6292
6293/**
6294 * _firmware_event_work - delayed task for processing firmware events
6295 * @ioc: per adapter object
6296 * @work: equal to the fw_event_work object
6297 * Context: user.
6298 *
6299 * Return nothing.
6300 */
6301static void
6302_firmware_event_work(struct work_struct *work)
6303{
6304 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306305 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06006306 unsigned long flags;
6307 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6308
Eric Moore635374e2009-03-09 01:21:12 -06006309 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06006310 if (ioc->remove_host || fw_event->cancel_pending_work ||
6311 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06006312 _scsih_fw_event_free(ioc, fw_event);
6313 return;
6314 }
Eric Moore635374e2009-03-09 01:21:12 -06006315
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306316 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6317 _scsih_fw_event_free(ioc, fw_event);
6318 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6319 if (ioc->shost_recovery) {
6320 init_completion(&ioc->shost_recovery_done);
6321 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6322 flags);
6323 wait_for_completion(&ioc->shost_recovery_done);
6324 } else
6325 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6326 flags);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306327 _scsih_remove_unresponding_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006328 return;
6329 }
Eric Moore635374e2009-03-09 01:21:12 -06006330
6331 switch (fw_event->event) {
6332 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306333 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006334 break;
6335 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306336 _scsih_sas_device_status_change_event(ioc,
6337 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006338 break;
6339 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306340 _scsih_sas_discovery_event(ioc,
6341 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006342 break;
6343 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306344 _scsih_sas_broadcast_primative_event(ioc,
6345 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006346 break;
6347 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6348 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306349 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006350 break;
6351 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306352 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006353 break;
6354 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306355 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006356 break;
6357 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306358 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006359 break;
6360 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306361 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006362 break;
Eric Moore635374e2009-03-09 01:21:12 -06006363 }
6364 _scsih_fw_event_free(ioc, fw_event);
6365}
6366
6367/**
6368 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6369 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306370 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06006371 * @reply: reply message frame(lower 32bit addr)
6372 * Context: interrupt.
6373 *
6374 * This function merely adds a new work task into ioc->firmware_event_thread.
6375 * The tasks are worked from _firmware_event_work in user context.
6376 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306377 * Return 1 meaning mf should be freed from _base_interrupt
6378 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006379 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306380u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306381mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6382 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006383{
6384 struct fw_event_work *fw_event;
6385 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006386 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306387 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006388
6389 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06006390 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306391 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006392
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306393 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006394 event = le16_to_cpu(mpi_reply->Event);
6395
6396 switch (event) {
6397 /* handle these */
6398 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6399 {
6400 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6401 (Mpi2EventDataSasBroadcastPrimitive_t *)
6402 mpi_reply->EventData;
6403
6404 if (baen_data->Primitive !=
6405 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6406 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306407 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006408 ioc->broadcast_aen_busy = 1;
6409 break;
6410 }
6411
6412 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6413 _scsih_check_topo_delete_events(ioc,
6414 (Mpi2EventDataSasTopologyChangeList_t *)
6415 mpi_reply->EventData);
6416 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306417 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6418 _scsih_check_ir_config_unhide_events(ioc,
6419 (Mpi2EventDataIrConfigChangeList_t *)
6420 mpi_reply->EventData);
6421 break;
6422 case MPI2_EVENT_IR_VOLUME:
6423 _scsih_check_volume_delete_events(ioc,
6424 (Mpi2EventDataIrVolume_t *)
6425 mpi_reply->EventData);
6426 break;
Eric Moore635374e2009-03-09 01:21:12 -06006427 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6428 case MPI2_EVENT_IR_OPERATION_STATUS:
6429 case MPI2_EVENT_SAS_DISCOVERY:
6430 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06006431 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06006432 break;
6433
6434 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306435 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006436 }
6437
6438 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6439 if (!fw_event) {
6440 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6441 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306442 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006443 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306444 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6445 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06006446 if (!fw_event->event_data) {
6447 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6448 ioc->name, __FILE__, __LINE__, __func__);
6449 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306450 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006451 }
6452
6453 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306454 sz);
Eric Moore635374e2009-03-09 01:21:12 -06006455 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306456 fw_event->VF_ID = mpi_reply->VF_ID;
6457 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06006458 fw_event->event = event;
6459 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306460 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006461}
6462
6463/* shost template */
6464static struct scsi_host_template scsih_driver_template = {
6465 .module = THIS_MODULE,
6466 .name = "Fusion MPT SAS Host",
6467 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06006468 .queuecommand = _scsih_qcmd,
6469 .target_alloc = _scsih_target_alloc,
6470 .slave_alloc = _scsih_slave_alloc,
6471 .slave_configure = _scsih_slave_configure,
6472 .target_destroy = _scsih_target_destroy,
6473 .slave_destroy = _scsih_slave_destroy,
6474 .change_queue_depth = _scsih_change_queue_depth,
6475 .change_queue_type = _scsih_change_queue_type,
6476 .eh_abort_handler = _scsih_abort,
6477 .eh_device_reset_handler = _scsih_dev_reset,
6478 .eh_target_reset_handler = _scsih_target_reset,
6479 .eh_host_reset_handler = _scsih_host_reset,
6480 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06006481 .can_queue = 1,
6482 .this_id = -1,
6483 .sg_tablesize = MPT2SAS_SG_DEPTH,
6484 .max_sectors = 8192,
6485 .cmd_per_lun = 7,
6486 .use_clustering = ENABLE_CLUSTERING,
6487 .shost_attrs = mpt2sas_host_attrs,
6488 .sdev_attrs = mpt2sas_dev_attrs,
6489};
6490
6491/**
6492 * _scsih_expander_node_remove - removing expander device from list.
6493 * @ioc: per adapter object
6494 * @sas_expander: the sas_device object
6495 * Context: Calling function should acquire ioc->sas_node_lock.
6496 *
6497 * Removing object and freeing associated memory from the
6498 * ioc->sas_expander_list.
6499 *
6500 * Return nothing.
6501 */
6502static void
6503_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6504 struct _sas_node *sas_expander)
6505{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306506 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06006507
6508 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306509 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06006510 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306511 if (ioc->shost_recovery)
6512 return;
Eric Moore635374e2009-03-09 01:21:12 -06006513 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306514 SAS_END_DEVICE)
6515 mpt2sas_device_remove(ioc,
6516 mpt2sas_port->remote_identify.sas_address);
6517 else if (mpt2sas_port->remote_identify.device_type ==
6518 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06006519 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306520 SAS_FANOUT_EXPANDER_DEVICE)
6521 mpt2sas_expander_remove(ioc,
6522 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006523 }
6524
6525 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306526 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006527
6528 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6529 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6530 sas_expander->handle, (unsigned long long)
6531 sas_expander->sas_address);
6532
Eric Moore635374e2009-03-09 01:21:12 -06006533 kfree(sas_expander->phy);
6534 kfree(sas_expander);
6535}
6536
6537/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05306538 * _scsih_ir_shutdown - IR shutdown notification
6539 * @ioc: per adapter object
6540 *
6541 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6542 * the host system is shutting down.
6543 *
6544 * Return nothing.
6545 */
6546static void
6547_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6548{
6549 Mpi2RaidActionRequest_t *mpi_request;
6550 Mpi2RaidActionReply_t *mpi_reply;
6551 u16 smid;
6552
6553 /* is IR firmware build loaded ? */
6554 if (!ioc->ir_firmware)
6555 return;
6556
6557 /* are there any volumes ? */
6558 if (list_empty(&ioc->raid_device_list))
6559 return;
6560
6561 mutex_lock(&ioc->scsih_cmds.mutex);
6562
6563 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6564 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6565 ioc->name, __func__);
6566 goto out;
6567 }
6568 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6569
6570 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6571 if (!smid) {
6572 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6573 ioc->name, __func__);
6574 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6575 goto out;
6576 }
6577
6578 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6579 ioc->scsih_cmds.smid = smid;
6580 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6581
6582 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6583 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6584
6585 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6586 init_completion(&ioc->scsih_cmds.done);
6587 mpt2sas_base_put_smid_default(ioc, smid);
6588 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6589
6590 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6591 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6592 ioc->name, __func__);
6593 goto out;
6594 }
6595
6596 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6597 mpi_reply = ioc->scsih_cmds.reply;
6598
6599 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6600 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6601 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6602 le32_to_cpu(mpi_reply->IOCLogInfo));
6603 }
6604
6605 out:
6606 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6607 mutex_unlock(&ioc->scsih_cmds.mutex);
6608}
6609
6610/**
6611 * _scsih_shutdown - routine call during system shutdown
6612 * @pdev: PCI device struct
6613 *
6614 * Return nothing.
6615 */
6616static void
6617_scsih_shutdown(struct pci_dev *pdev)
6618{
6619 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6620 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306621 struct workqueue_struct *wq;
6622 unsigned long flags;
6623
6624 ioc->remove_host = 1;
6625 _scsih_fw_event_cleanup_queue(ioc);
6626
6627 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6628 wq = ioc->firmware_event_thread;
6629 ioc->firmware_event_thread = NULL;
6630 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6631 if (wq)
6632 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306633
6634 _scsih_ir_shutdown(ioc);
6635 mpt2sas_base_detach(ioc);
6636}
6637
6638/**
Eric Moored5d135b2009-05-18 13:02:08 -06006639 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006640 * @pdev: PCI device struct
6641 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306642 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006643 * Return nothing.
6644 */
6645static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006646_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006647{
6648 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6649 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306650 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306651 struct _raid_device *raid_device, *next;
6652 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006653 struct workqueue_struct *wq;
6654 unsigned long flags;
6655
6656 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306657 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006658
6659 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6660 wq = ioc->firmware_event_thread;
6661 ioc->firmware_event_thread = NULL;
6662 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6663 if (wq)
6664 destroy_workqueue(wq);
6665
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306666 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05306667 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306668 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 scsi_block_requests(shost);
6978 device_state = pci_choose_state(pdev, state);
6979 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6980 "operating state [D%d]\n", ioc->name, pdev,
6981 pci_name(pdev), device_state);
6982
6983 mpt2sas_base_free_resources(ioc);
6984 pci_save_state(pdev);
6985 pci_disable_device(pdev);
6986 pci_set_power_state(pdev, device_state);
6987 return 0;
6988}
6989
6990/**
Eric Moored5d135b2009-05-18 13:02:08 -06006991 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006992 * @pdev: PCI device struct
6993 *
6994 * Returns 0 success, anything else error.
6995 */
6996static int
Eric Moored5d135b2009-05-18 13:02:08 -06006997_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006998{
6999 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7000 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7001 u32 device_state = pdev->current_state;
7002 int r;
7003
7004 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7005 "operating state [D%d]\n", ioc->name, pdev,
7006 pci_name(pdev), device_state);
7007
7008 pci_set_power_state(pdev, PCI_D0);
7009 pci_enable_wake(pdev, PCI_D0, 0);
7010 pci_restore_state(pdev);
7011 ioc->pdev = pdev;
7012 r = mpt2sas_base_map_resources(ioc);
7013 if (r)
7014 return r;
7015
7016 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7017 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05307018 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007019 return 0;
7020}
7021#endif /* CONFIG_PM */
7022
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307023/**
7024 * _scsih_pci_error_detected - Called when a PCI error is detected.
7025 * @pdev: PCI device struct
7026 * @state: PCI channel state
7027 *
7028 * Description: Called when a PCI error is detected.
7029 *
7030 * Return value:
7031 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7032 */
7033static pci_ers_result_t
7034_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7035{
7036 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7037 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7038
7039 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7040 ioc->name, state);
7041
7042 switch (state) {
7043 case pci_channel_io_normal:
7044 return PCI_ERS_RESULT_CAN_RECOVER;
7045 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06007046 /* Fatal error, prepare for slot reset */
7047 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307048 scsi_block_requests(ioc->shost);
7049 mpt2sas_base_stop_watchdog(ioc);
7050 mpt2sas_base_free_resources(ioc);
7051 return PCI_ERS_RESULT_NEED_RESET;
7052 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06007053 /* Permanent error, prepare for device removal */
7054 ioc->pci_error_recovery = 1;
7055 mpt2sas_base_stop_watchdog(ioc);
7056 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307057 return PCI_ERS_RESULT_DISCONNECT;
7058 }
7059 return PCI_ERS_RESULT_NEED_RESET;
7060}
7061
7062/**
7063 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7064 * @pdev: PCI device struct
7065 *
7066 * Description: This routine is called by the pci error recovery
7067 * code after the PCI slot has been reset, just before we
7068 * should resume normal operations.
7069 */
7070static pci_ers_result_t
7071_scsih_pci_slot_reset(struct pci_dev *pdev)
7072{
7073 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7074 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7075 int rc;
7076
7077 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7078 ioc->name);
7079
Eric Moore3cb54692010-07-08 14:44:34 -06007080 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307081 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06007082 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307083 rc = mpt2sas_base_map_resources(ioc);
7084 if (rc)
7085 return PCI_ERS_RESULT_DISCONNECT;
7086
7087
7088 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7089 FORCE_BIG_HAMMER);
7090
7091 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7092 (rc == 0) ? "success" : "failed");
7093
7094 if (!rc)
7095 return PCI_ERS_RESULT_RECOVERED;
7096 else
7097 return PCI_ERS_RESULT_DISCONNECT;
7098}
7099
7100/**
7101 * _scsih_pci_resume() - resume normal ops after PCI reset
7102 * @pdev: pointer to PCI device
7103 *
7104 * Called when the error recovery driver tells us that its
7105 * OK to resume normal operation. Use completion to allow
7106 * halted scsi ops to resume.
7107 */
7108static void
7109_scsih_pci_resume(struct pci_dev *pdev)
7110{
7111 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7112 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7113
7114 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7115
7116 pci_cleanup_aer_uncorrect_error_status(pdev);
7117 mpt2sas_base_start_watchdog(ioc);
7118 scsi_unblock_requests(ioc->shost);
7119}
7120
7121/**
7122 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7123 * @pdev: pointer to PCI device
7124 */
7125static pci_ers_result_t
7126_scsih_pci_mmio_enabled(struct pci_dev *pdev)
7127{
7128 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7129 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7130
7131 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7132 ioc->name);
7133
7134 /* TODO - dump whatever for debugging purposes */
7135
7136 /* Request a slot reset. */
7137 return PCI_ERS_RESULT_NEED_RESET;
7138}
7139
7140static struct pci_error_handlers _scsih_err_handler = {
7141 .error_detected = _scsih_pci_error_detected,
7142 .mmio_enabled = _scsih_pci_mmio_enabled,
7143 .slot_reset = _scsih_pci_slot_reset,
7144 .resume = _scsih_pci_resume,
7145};
Eric Moore635374e2009-03-09 01:21:12 -06007146
7147static struct pci_driver scsih_driver = {
7148 .name = MPT2SAS_DRIVER_NAME,
7149 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06007150 .probe = _scsih_probe,
7151 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05307152 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307153 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06007154#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06007155 .suspend = _scsih_suspend,
7156 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06007157#endif
7158};
7159
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307160/* raid transport support */
7161static struct raid_function_template mpt2sas_raid_functions = {
7162 .cookie = &scsih_driver_template,
7163 .is_raid = _scsih_is_raid,
7164 .get_resync = _scsih_get_resync,
7165 .get_state = _scsih_get_state,
7166};
Eric Moore635374e2009-03-09 01:21:12 -06007167
7168/**
Eric Moored5d135b2009-05-18 13:02:08 -06007169 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06007170 *
7171 * Returns 0 success, anything else error.
7172 */
7173static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06007174_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06007175{
7176 int error;
7177
7178 mpt_ids = 0;
7179 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7180 MPT2SAS_DRIVER_VERSION);
7181
7182 mpt2sas_transport_template =
7183 sas_attach_transport(&mpt2sas_transport_functions);
7184 if (!mpt2sas_transport_template)
7185 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307186 /* raid transport support */
7187 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7188 if (!mpt2sas_raid_template) {
7189 sas_release_transport(mpt2sas_transport_template);
7190 return -ENODEV;
7191 }
Eric Moore635374e2009-03-09 01:21:12 -06007192
7193 mpt2sas_base_initialize_callback_handler();
7194
7195 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06007196 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06007197
Uwe Kleine-König65155b32010-06-11 12:17:01 +02007198 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06007199 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06007200
7201 /* base internal commands callback handler */
7202 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7203
7204 /* transport internal commands callback handler */
7205 transport_cb_idx = mpt2sas_base_register_callback_handler(
7206 mpt2sas_transport_done);
7207
Kashyap, Desai744090d2009-10-05 15:56:56 +05307208 /* scsih internal commands callback handler */
7209 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7210
Eric Moore635374e2009-03-09 01:21:12 -06007211 /* configuration page API internal commands callback handler */
7212 config_cb_idx = mpt2sas_base_register_callback_handler(
7213 mpt2sas_config_done);
7214
7215 /* ctl module callback handler */
7216 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7217
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307218 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7219 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307220
7221 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7222 _scsih_tm_volume_tr_complete);
7223
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307224 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7225 _scsih_sas_control_complete);
7226
Eric Moore635374e2009-03-09 01:21:12 -06007227 mpt2sas_ctl_init();
7228
7229 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307230 if (error) {
7231 /* raid transport support */
7232 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06007233 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307234 }
Eric Moore635374e2009-03-09 01:21:12 -06007235
7236 return error;
7237}
7238
7239/**
Eric Moored5d135b2009-05-18 13:02:08 -06007240 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06007241 *
7242 * Returns 0 success, anything else error.
7243 */
7244static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06007245_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06007246{
7247 printk(KERN_INFO "mpt2sas version %s unloading\n",
7248 MPT2SAS_DRIVER_VERSION);
7249
7250 pci_unregister_driver(&scsih_driver);
7251
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307252 mpt2sas_ctl_exit();
7253
Eric Moore635374e2009-03-09 01:21:12 -06007254 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7255 mpt2sas_base_release_callback_handler(tm_cb_idx);
7256 mpt2sas_base_release_callback_handler(base_cb_idx);
7257 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307258 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06007259 mpt2sas_base_release_callback_handler(config_cb_idx);
7260 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7261
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307262 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307263 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307264 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7265
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307266 /* raid transport support */
7267 raid_class_release(mpt2sas_raid_template);
7268 sas_release_transport(mpt2sas_transport_template);
7269
Eric Moore635374e2009-03-09 01:21:12 -06007270}
7271
Eric Moored5d135b2009-05-18 13:02:08 -06007272module_init(_scsih_init);
7273module_exit(_scsih_exit);