blob: a16f2a05736ff3d3fd73562b01248d2372bc987e [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05305 * Copyright (C) 2007-2010 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053055#include <linux/aer.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053056#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090057#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060058
59#include "mpt2sas_base.h"
60
61MODULE_AUTHOR(MPT2SAS_AUTHOR);
62MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
63MODULE_LICENSE("GPL");
64MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
65
66#define RAID_CHANNEL 1
67
68/* forward proto's */
69static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
70 struct _sas_node *sas_expander);
71static void _firmware_event_work(struct work_struct *work);
72
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053073static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
74
Eric Moore635374e2009-03-09 01:21:12 -060075/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060076LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060077
78/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060079static u8 scsi_io_cb_idx = -1;
80static u8 tm_cb_idx = -1;
81static u8 ctl_cb_idx = -1;
82static u8 base_cb_idx = -1;
83static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053084static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060085static u8 config_cb_idx = -1;
86static int mpt_ids;
87
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053088static u8 tm_tr_cb_idx = -1 ;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053089static u8 tm_tr_volume_cb_idx = -1 ;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053090static u8 tm_sas_control_cb_idx = -1;
91
Eric Moore635374e2009-03-09 01:21:12 -060092/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060093static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060094MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
95 "(default=0)");
96
97/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
98#define MPT2SAS_MAX_LUN (16895)
99static int max_lun = MPT2SAS_MAX_LUN;
100module_param(max_lun, int, 0);
101MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
102
103/**
104 * struct sense_info - common structure for obtaining sense keys
105 * @skey: sense key
106 * @asc: additional sense code
107 * @ascq: additional sense code qualifier
108 */
109struct sense_info {
110 u8 skey;
111 u8 asc;
112 u8 ascq;
113};
114
115
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530116#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
117
Eric Moore635374e2009-03-09 01:21:12 -0600118/**
119 * struct fw_event_work - firmware event struct
120 * @list: link list framework
121 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530122 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600123 * @ioc: per adapter object
124 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530125 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600126 * @ignore: flag meaning this event has been marked to ignore
127 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
128 * @event_data: reply event data payload follows
129 *
130 * This object stored on ioc->fw_event_list.
131 */
132struct fw_event_work {
133 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530134 u8 cancel_pending_work;
135 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600136 struct MPT2SAS_ADAPTER *ioc;
137 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530138 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600139 u8 ignore;
140 u16 event;
141 void *event_data;
142};
143
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530144/* raid transport support */
145static struct raid_template *mpt2sas_raid_template;
146
Eric Moore635374e2009-03-09 01:21:12 -0600147/**
148 * struct _scsi_io_transfer - scsi io transfer
149 * @handle: sas device handle (assigned by firmware)
150 * @is_raid: flag set for hidden raid components
151 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
152 * @data_length: data transfer length
153 * @data_dma: dma pointer to data
154 * @sense: sense data
155 * @lun: lun number
156 * @cdb_length: cdb length
157 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600158 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530159 * @VF_ID: virtual function id
160 * @VP_ID: virtual port id
161 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600162 * @sense_length: sense length
163 * @ioc_status: ioc status
164 * @scsi_state: scsi state
165 * @scsi_status: scsi staus
166 * @log_info: log information
167 * @transfer_length: data length transfer when there is a reply message
168 *
169 * Used for sending internal scsi commands to devices within this module.
170 * Refer to _scsi_send_scsi_io().
171 */
172struct _scsi_io_transfer {
173 u16 handle;
174 u8 is_raid;
175 enum dma_data_direction dir;
176 u32 data_length;
177 dma_addr_t data_dma;
178 u8 sense[SCSI_SENSE_BUFFERSIZE];
179 u32 lun;
180 u8 cdb_length;
181 u8 cdb[32];
182 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530183 u8 VF_ID;
184 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600185 u8 valid_reply;
186 /* the following bits are only valid when 'valid_reply = 1' */
187 u32 sense_length;
188 u16 ioc_status;
189 u8 scsi_state;
190 u8 scsi_status;
191 u32 log_info;
192 u32 transfer_length;
193};
194
195/*
196 * The pci device ids are defined in mpi/mpi2_cnfg.h.
197 */
198static struct pci_device_id scsih_pci_table[] = {
199 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
200 PCI_ANY_ID, PCI_ANY_ID },
201 /* Falcon ~ 2008*/
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
203 PCI_ANY_ID, PCI_ANY_ID },
204 /* Liberator ~ 2108 */
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
206 PCI_ANY_ID, PCI_ANY_ID },
207 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
208 PCI_ANY_ID, PCI_ANY_ID },
209 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
210 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530211 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600212 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
213 PCI_ANY_ID, PCI_ANY_ID },
214 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
215 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530216 /* Thunderbolt ~ 2208 */
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
218 PCI_ANY_ID, PCI_ANY_ID },
219 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
220 PCI_ANY_ID, PCI_ANY_ID },
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
222 PCI_ANY_ID, PCI_ANY_ID },
223 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
224 PCI_ANY_ID, PCI_ANY_ID },
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
226 PCI_ANY_ID, PCI_ANY_ID },
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
228 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530229 /* Mustang ~ 2308 */
230 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530231 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530232 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
233 PCI_ANY_ID, PCI_ANY_ID },
234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530235 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600236 {0} /* Terminating entry */
237};
238MODULE_DEVICE_TABLE(pci, scsih_pci_table);
239
240/**
Eric Moored5d135b2009-05-18 13:02:08 -0600241 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600242 *
243 * Note: The logging levels are defined in mpt2sas_debug.h.
244 */
245static int
Eric Moored5d135b2009-05-18 13:02:08 -0600246_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600247{
248 int ret = param_set_int(val, kp);
249 struct MPT2SAS_ADAPTER *ioc;
250
251 if (ret)
252 return ret;
253
254 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600255 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600256 ioc->logging_level = logging_level;
257 return 0;
258}
Eric Moored5d135b2009-05-18 13:02:08 -0600259module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600260 &logging_level, 0644);
261
262/**
263 * _scsih_srch_boot_sas_address - search based on sas_address
264 * @sas_address: sas address
265 * @boot_device: boot device object from bios page 2
266 *
267 * Returns 1 when there's a match, 0 means no match.
268 */
269static inline int
270_scsih_srch_boot_sas_address(u64 sas_address,
271 Mpi2BootDeviceSasWwid_t *boot_device)
272{
273 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
274}
275
276/**
277 * _scsih_srch_boot_device_name - search based on device name
278 * @device_name: device name specified in INDENTIFY fram
279 * @boot_device: boot device object from bios page 2
280 *
281 * Returns 1 when there's a match, 0 means no match.
282 */
283static inline int
284_scsih_srch_boot_device_name(u64 device_name,
285 Mpi2BootDeviceDeviceName_t *boot_device)
286{
287 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
288}
289
290/**
291 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
292 * @enclosure_logical_id: enclosure logical id
293 * @slot_number: slot number
294 * @boot_device: boot device object from bios page 2
295 *
296 * Returns 1 when there's a match, 0 means no match.
297 */
298static inline int
299_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
300 Mpi2BootDeviceEnclosureSlot_t *boot_device)
301{
302 return (enclosure_logical_id == le64_to_cpu(boot_device->
303 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
304 SlotNumber)) ? 1 : 0;
305}
306
307/**
308 * _scsih_is_boot_device - search for matching boot device.
309 * @sas_address: sas address
310 * @device_name: device name specified in INDENTIFY fram
311 * @enclosure_logical_id: enclosure logical id
312 * @slot_number: slot number
313 * @form: specifies boot device form
314 * @boot_device: boot device object from bios page 2
315 *
316 * Returns 1 when there's a match, 0 means no match.
317 */
318static int
319_scsih_is_boot_device(u64 sas_address, u64 device_name,
320 u64 enclosure_logical_id, u16 slot, u8 form,
321 Mpi2BiosPage2BootDevice_t *boot_device)
322{
323 int rc = 0;
324
325 switch (form) {
326 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
327 if (!sas_address)
328 break;
329 rc = _scsih_srch_boot_sas_address(
330 sas_address, &boot_device->SasWwid);
331 break;
332 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
333 if (!enclosure_logical_id)
334 break;
335 rc = _scsih_srch_boot_encl_slot(
336 enclosure_logical_id,
337 slot, &boot_device->EnclosureSlot);
338 break;
339 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
340 if (!device_name)
341 break;
342 rc = _scsih_srch_boot_device_name(
343 device_name, &boot_device->DeviceName);
344 break;
345 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
346 break;
347 }
348
349 return rc;
350}
351
352/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530353 * _scsih_get_sas_address - set the sas_address for given device handle
354 * @handle: device handle
355 * @sas_address: sas address
356 *
357 * Returns 0 success, non-zero when failure
358 */
359static int
360_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
361 u64 *sas_address)
362{
363 Mpi2SasDevicePage0_t sas_device_pg0;
364 Mpi2ConfigReply_t mpi_reply;
365 u32 ioc_status;
366
367 if (handle <= ioc->sas_hba.num_phys) {
368 *sas_address = ioc->sas_hba.sas_address;
369 return 0;
370 } else
371 *sas_address = 0;
372
373 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
374 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
375 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
376 ioc->name, __FILE__, __LINE__, __func__);
377 return -ENXIO;
378 }
379
380 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
381 MPI2_IOCSTATUS_MASK;
382 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
383 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
384 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
385 __FILE__, __LINE__, __func__);
386 return -EIO;
387 }
388
389 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
390 return 0;
391}
392
393/**
Eric Moore635374e2009-03-09 01:21:12 -0600394 * _scsih_determine_boot_device - determine boot device.
395 * @ioc: per adapter object
396 * @device: either sas_device or raid_device object
397 * @is_raid: [flag] 1 = raid object, 0 = sas object
398 *
399 * Determines whether this device should be first reported device to
400 * to scsi-ml or sas transport, this purpose is for persistant boot device.
401 * There are primary, alternate, and current entries in bios page 2. The order
402 * priority is primary, alternate, then current. This routine saves
403 * the corresponding device object and is_raid flag in the ioc object.
404 * The saved data to be used later in _scsih_probe_boot_devices().
405 */
406static void
407_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
408 void *device, u8 is_raid)
409{
410 struct _sas_device *sas_device;
411 struct _raid_device *raid_device;
412 u64 sas_address;
413 u64 device_name;
414 u64 enclosure_logical_id;
415 u16 slot;
416
417 /* only process this function when driver loads */
418 if (!ioc->wait_for_port_enable_to_complete)
419 return;
420
421 if (!is_raid) {
422 sas_device = device;
423 sas_address = sas_device->sas_address;
424 device_name = sas_device->device_name;
425 enclosure_logical_id = sas_device->enclosure_logical_id;
426 slot = sas_device->slot;
427 } else {
428 raid_device = device;
429 sas_address = raid_device->wwid;
430 device_name = 0;
431 enclosure_logical_id = 0;
432 slot = 0;
433 }
434
435 if (!ioc->req_boot_device.device) {
436 if (_scsih_is_boot_device(sas_address, device_name,
437 enclosure_logical_id, slot,
438 (ioc->bios_pg2.ReqBootDeviceForm &
439 MPI2_BIOSPAGE2_FORM_MASK),
440 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530441 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600442 "%s: req_boot_device(0x%016llx)\n",
443 ioc->name, __func__,
444 (unsigned long long)sas_address));
445 ioc->req_boot_device.device = device;
446 ioc->req_boot_device.is_raid = is_raid;
447 }
448 }
449
450 if (!ioc->req_alt_boot_device.device) {
451 if (_scsih_is_boot_device(sas_address, device_name,
452 enclosure_logical_id, slot,
453 (ioc->bios_pg2.ReqAltBootDeviceForm &
454 MPI2_BIOSPAGE2_FORM_MASK),
455 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530456 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600457 "%s: req_alt_boot_device(0x%016llx)\n",
458 ioc->name, __func__,
459 (unsigned long long)sas_address));
460 ioc->req_alt_boot_device.device = device;
461 ioc->req_alt_boot_device.is_raid = is_raid;
462 }
463 }
464
465 if (!ioc->current_boot_device.device) {
466 if (_scsih_is_boot_device(sas_address, device_name,
467 enclosure_logical_id, slot,
468 (ioc->bios_pg2.CurrentBootDeviceForm &
469 MPI2_BIOSPAGE2_FORM_MASK),
470 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530471 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600472 "%s: current_boot_device(0x%016llx)\n",
473 ioc->name, __func__,
474 (unsigned long long)sas_address));
475 ioc->current_boot_device.device = device;
476 ioc->current_boot_device.is_raid = is_raid;
477 }
478 }
479}
480
481/**
482 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
483 * @ioc: per adapter object
484 * @sas_address: sas address
485 * Context: Calling function should acquire ioc->sas_device_lock
486 *
487 * This searches for sas_device based on sas_address, then return sas_device
488 * object.
489 */
490struct _sas_device *
491mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
492 u64 sas_address)
493{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530494 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600495
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530496 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
497 if (sas_device->sas_address == sas_address)
498 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600499
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530500 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
501 if (sas_device->sas_address == sas_address)
502 return sas_device;
503
504 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600505}
506
507/**
508 * _scsih_sas_device_find_by_handle - sas device search
509 * @ioc: per adapter object
510 * @handle: sas device handle (assigned by firmware)
511 * Context: Calling function should acquire ioc->sas_device_lock
512 *
513 * This searches for sas_device based on sas_address, then return sas_device
514 * object.
515 */
516static struct _sas_device *
517_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
518{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530519 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600520
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530521 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
522 if (sas_device->handle == handle)
523 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600524
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530525 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
526 if (sas_device->handle == handle)
527 return sas_device;
528
529 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600530}
531
532/**
533 * _scsih_sas_device_remove - remove sas_device from list.
534 * @ioc: per adapter object
535 * @sas_device: the sas_device object
536 * Context: This function will acquire ioc->sas_device_lock.
537 *
538 * Removing object and freeing associated memory from the ioc->sas_device_list.
539 */
540static void
541_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
542 struct _sas_device *sas_device)
543{
544 unsigned long flags;
545
Kashyap, Desai980ead32010-04-08 17:55:22 +0530546 if (!sas_device)
547 return;
548
Eric Moore635374e2009-03-09 01:21:12 -0600549 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530550 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
551 sas_device->sas_address)) {
552 list_del(&sas_device->list);
553 kfree(sas_device);
554 }
Eric Moore635374e2009-03-09 01:21:12 -0600555 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
556}
557
558/**
559 * _scsih_sas_device_add - insert sas_device to the list.
560 * @ioc: per adapter object
561 * @sas_device: the sas_device object
562 * Context: This function will acquire ioc->sas_device_lock.
563 *
564 * Adding new object to the ioc->sas_device_list.
565 */
566static void
567_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
568 struct _sas_device *sas_device)
569{
570 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600571
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530572 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600573 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
574 sas_device->handle, (unsigned long long)sas_device->sas_address));
575
576 spin_lock_irqsave(&ioc->sas_device_lock, flags);
577 list_add_tail(&sas_device->list, &ioc->sas_device_list);
578 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
579
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530580 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
581 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600582 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600583}
584
585/**
586 * _scsih_sas_device_init_add - insert sas_device to the list.
587 * @ioc: per adapter object
588 * @sas_device: the sas_device object
589 * Context: This function will acquire ioc->sas_device_lock.
590 *
591 * Adding new object at driver load time to the ioc->sas_device_init_list.
592 */
593static void
594_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
595 struct _sas_device *sas_device)
596{
597 unsigned long flags;
598
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530599 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600600 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
601 sas_device->handle, (unsigned long long)sas_device->sas_address));
602
603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
604 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
605 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
606 _scsih_determine_boot_device(ioc, sas_device, 0);
607}
608
609/**
Eric Moore635374e2009-03-09 01:21:12 -0600610 * _scsih_raid_device_find_by_id - raid device search
611 * @ioc: per adapter object
612 * @id: sas device target id
613 * @channel: sas device channel
614 * Context: Calling function should acquire ioc->raid_device_lock
615 *
616 * This searches for raid_device based on target id, then return raid_device
617 * object.
618 */
619static struct _raid_device *
620_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
621{
622 struct _raid_device *raid_device, *r;
623
624 r = NULL;
625 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
626 if (raid_device->id == id && raid_device->channel == channel) {
627 r = raid_device;
628 goto out;
629 }
630 }
631
632 out:
633 return r;
634}
635
636/**
637 * _scsih_raid_device_find_by_handle - raid device search
638 * @ioc: per adapter object
639 * @handle: sas device handle (assigned by firmware)
640 * Context: Calling function should acquire ioc->raid_device_lock
641 *
642 * This searches for raid_device based on handle, then return raid_device
643 * object.
644 */
645static struct _raid_device *
646_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
647{
648 struct _raid_device *raid_device, *r;
649
650 r = NULL;
651 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
652 if (raid_device->handle != handle)
653 continue;
654 r = raid_device;
655 goto out;
656 }
657
658 out:
659 return r;
660}
661
662/**
663 * _scsih_raid_device_find_by_wwid - raid device search
664 * @ioc: per adapter object
665 * @handle: sas device handle (assigned by firmware)
666 * Context: Calling function should acquire ioc->raid_device_lock
667 *
668 * This searches for raid_device based on wwid, then return raid_device
669 * object.
670 */
671static struct _raid_device *
672_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
673{
674 struct _raid_device *raid_device, *r;
675
676 r = NULL;
677 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
678 if (raid_device->wwid != wwid)
679 continue;
680 r = raid_device;
681 goto out;
682 }
683
684 out:
685 return r;
686}
687
688/**
689 * _scsih_raid_device_add - add raid_device object
690 * @ioc: per adapter object
691 * @raid_device: raid_device object
692 *
693 * This is added to the raid_device_list link list.
694 */
695static void
696_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
697 struct _raid_device *raid_device)
698{
699 unsigned long flags;
700
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530701 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600702 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
703 raid_device->handle, (unsigned long long)raid_device->wwid));
704
705 spin_lock_irqsave(&ioc->raid_device_lock, flags);
706 list_add_tail(&raid_device->list, &ioc->raid_device_list);
707 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
708}
709
710/**
711 * _scsih_raid_device_remove - delete raid_device object
712 * @ioc: per adapter object
713 * @raid_device: raid_device object
714 *
715 * This is removed from the raid_device_list link list.
716 */
717static void
718_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
719 struct _raid_device *raid_device)
720{
721 unsigned long flags;
722
723 spin_lock_irqsave(&ioc->raid_device_lock, flags);
724 list_del(&raid_device->list);
725 memset(raid_device, 0, sizeof(struct _raid_device));
726 kfree(raid_device);
727 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
728}
729
730/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530731 * mpt2sas_scsih_expander_find_by_handle - expander device search
732 * @ioc: per adapter object
733 * @handle: expander handle (assigned by firmware)
734 * Context: Calling function should acquire ioc->sas_device_lock
735 *
736 * This searches for expander device based on handle, then returns the
737 * sas_node object.
738 */
739struct _sas_node *
740mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
741{
742 struct _sas_node *sas_expander, *r;
743
744 r = NULL;
745 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
746 if (sas_expander->handle != handle)
747 continue;
748 r = sas_expander;
749 goto out;
750 }
751 out:
752 return r;
753}
754
755/**
Eric Moore635374e2009-03-09 01:21:12 -0600756 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
757 * @ioc: per adapter object
758 * @sas_address: sas address
759 * Context: Calling function should acquire ioc->sas_node_lock.
760 *
761 * This searches for expander device based on sas_address, then returns the
762 * sas_node object.
763 */
764struct _sas_node *
765mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
766 u64 sas_address)
767{
768 struct _sas_node *sas_expander, *r;
769
770 r = NULL;
771 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
772 if (sas_expander->sas_address != sas_address)
773 continue;
774 r = sas_expander;
775 goto out;
776 }
777 out:
778 return r;
779}
780
781/**
782 * _scsih_expander_node_add - insert expander device to the list.
783 * @ioc: per adapter object
784 * @sas_expander: the sas_device object
785 * Context: This function will acquire ioc->sas_node_lock.
786 *
787 * Adding new object to the ioc->sas_expander_list.
788 *
789 * Return nothing.
790 */
791static void
792_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
793 struct _sas_node *sas_expander)
794{
795 unsigned long flags;
796
797 spin_lock_irqsave(&ioc->sas_node_lock, flags);
798 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
800}
801
802/**
803 * _scsih_is_end_device - determines if device is an end device
804 * @device_info: bitfield providing information about the device.
805 * Context: none
806 *
807 * Returns 1 if end device.
808 */
809static int
810_scsih_is_end_device(u32 device_info)
811{
812 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
813 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
814 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
815 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
816 return 1;
817 else
818 return 0;
819}
820
821/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530822 * mptscsih_get_scsi_lookup - 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/**
835 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
836 * @ioc: per adapter object
837 * @smid: system request message index
838 * @scmd: pointer to scsi command object
839 * Context: This function will acquire ioc->scsi_lookup_lock.
840 *
841 * This will search for a scmd pointer in the scsi_lookup array,
842 * returning the revelent smid. A returned value of zero means invalid.
843 */
844static u16
845_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
846 *scmd)
847{
848 u16 smid;
849 unsigned long flags;
850 int i;
851
852 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
853 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530854 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600855 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530856 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600857 goto out;
858 }
859 }
860 out:
861 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
862 return smid;
863}
864
865/**
866 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
867 * @ioc: per adapter object
868 * @id: target id
869 * @channel: channel
870 * Context: This function will acquire ioc->scsi_lookup_lock.
871 *
872 * This will search for a matching channel:id in the scsi_lookup array,
873 * returning 1 if found.
874 */
875static u8
876_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
877 int channel)
878{
879 u8 found;
880 unsigned long flags;
881 int i;
882
883 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
884 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530885 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600886 if (ioc->scsi_lookup[i].scmd &&
887 (ioc->scsi_lookup[i].scmd->device->id == id &&
888 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
889 found = 1;
890 goto out;
891 }
892 }
893 out:
894 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
895 return found;
896}
897
898/**
Eric Moore993e0da2009-05-18 13:00:45 -0600899 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
900 * @ioc: per adapter object
901 * @id: target id
902 * @lun: lun number
903 * @channel: channel
904 * Context: This function will acquire ioc->scsi_lookup_lock.
905 *
906 * This will search for a matching channel:id:lun in the scsi_lookup array,
907 * returning 1 if found.
908 */
909static u8
910_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
911 unsigned int lun, int channel)
912{
913 u8 found;
914 unsigned long flags;
915 int i;
916
917 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
918 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530919 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600920 if (ioc->scsi_lookup[i].scmd &&
921 (ioc->scsi_lookup[i].scmd->device->id == id &&
922 ioc->scsi_lookup[i].scmd->device->channel == channel &&
923 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
924 found = 1;
925 goto out;
926 }
927 }
928 out:
929 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
930 return found;
931}
932
933/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530934 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -0600935 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530936 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -0600937 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530938 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -0600939 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530940static struct chain_tracker *
941_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -0600942{
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530943 struct chain_tracker *chain_req;
944 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600945
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530946 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
947 if (list_empty(&ioc->free_chain_list)) {
948 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
949 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
950 ioc->name);
951 return NULL;
952 }
953 chain_req = list_entry(ioc->free_chain_list.next,
954 struct chain_tracker, tracker_list);
955 list_del_init(&chain_req->tracker_list);
956 list_add_tail(&chain_req->tracker_list,
957 &ioc->scsi_lookup[smid - 1].chain_list);
958 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
959 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -0600960}
961
962/**
963 * _scsih_build_scatter_gather - main sg creation routine
964 * @ioc: per adapter object
965 * @scmd: scsi command
966 * @smid: system request message index
967 * Context: none.
968 *
969 * The main routine that builds scatter gather table from a given
970 * scsi request sent via the .queuecommand main handler.
971 *
972 * Returns 0 success, anything else error
973 */
974static int
975_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
976 struct scsi_cmnd *scmd, u16 smid)
977{
978 Mpi2SCSIIORequest_t *mpi_request;
979 dma_addr_t chain_dma;
980 struct scatterlist *sg_scmd;
981 void *sg_local, *chain;
982 u32 chain_offset;
983 u32 chain_length;
984 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +0900985 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -0600986 u32 sges_in_segment;
987 u32 sgl_flags;
988 u32 sgl_flags_last_element;
989 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530990 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -0600991
992 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
993
994 /* init scatter gather flags */
995 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
996 if (scmd->sc_data_direction == DMA_TO_DEVICE)
997 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
998 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
999 << MPI2_SGE_FLAGS_SHIFT;
1000 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1001 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1002 << MPI2_SGE_FLAGS_SHIFT;
1003 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1004
1005 sg_scmd = scsi_sglist(scmd);
1006 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001007 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001008 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1009 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1010 return -ENOMEM;
1011 }
1012
1013 sg_local = &mpi_request->SGL;
1014 sges_in_segment = ioc->max_sges_in_main_message;
1015 if (sges_left <= sges_in_segment)
1016 goto fill_in_last_segment;
1017
1018 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1019 (sges_in_segment * ioc->sge_size))/4;
1020
1021 /* fill in main message segment when there is a chain following */
1022 while (sges_in_segment) {
1023 if (sges_in_segment == 1)
1024 ioc->base_add_sg_single(sg_local,
1025 sgl_flags_last_element | sg_dma_len(sg_scmd),
1026 sg_dma_address(sg_scmd));
1027 else
1028 ioc->base_add_sg_single(sg_local, sgl_flags |
1029 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1030 sg_scmd = sg_next(sg_scmd);
1031 sg_local += ioc->sge_size;
1032 sges_left--;
1033 sges_in_segment--;
1034 }
1035
1036 /* initializing the chain flags and pointers */
1037 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301038 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1039 if (!chain_req)
1040 return -1;
1041 chain = chain_req->chain_buffer;
1042 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001043 do {
1044 sges_in_segment = (sges_left <=
1045 ioc->max_sges_in_chain_message) ? sges_left :
1046 ioc->max_sges_in_chain_message;
1047 chain_offset = (sges_left == sges_in_segment) ?
1048 0 : (sges_in_segment * ioc->sge_size)/4;
1049 chain_length = sges_in_segment * ioc->sge_size;
1050 if (chain_offset) {
1051 chain_offset = chain_offset <<
1052 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1053 chain_length += ioc->sge_size;
1054 }
1055 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1056 chain_length, chain_dma);
1057 sg_local = chain;
1058 if (!chain_offset)
1059 goto fill_in_last_segment;
1060
1061 /* fill in chain segments */
1062 while (sges_in_segment) {
1063 if (sges_in_segment == 1)
1064 ioc->base_add_sg_single(sg_local,
1065 sgl_flags_last_element |
1066 sg_dma_len(sg_scmd),
1067 sg_dma_address(sg_scmd));
1068 else
1069 ioc->base_add_sg_single(sg_local, sgl_flags |
1070 sg_dma_len(sg_scmd),
1071 sg_dma_address(sg_scmd));
1072 sg_scmd = sg_next(sg_scmd);
1073 sg_local += ioc->sge_size;
1074 sges_left--;
1075 sges_in_segment--;
1076 }
1077
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301078 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1079 if (!chain_req)
1080 return -1;
1081 chain = chain_req->chain_buffer;
1082 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001083 } while (1);
1084
1085
1086 fill_in_last_segment:
1087
1088 /* fill the last segment */
1089 while (sges_left) {
1090 if (sges_left == 1)
1091 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1092 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1093 else
1094 ioc->base_add_sg_single(sg_local, sgl_flags |
1095 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1096 sg_scmd = sg_next(sg_scmd);
1097 sg_local += ioc->sge_size;
1098 sges_left--;
1099 }
1100
1101 return 0;
1102}
1103
1104/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301105 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001106 * @sdev: scsi device struct
1107 * @qdepth: requested queue depth
1108 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301109 *
1110 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001111 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301112static void
1113_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001114{
1115 struct Scsi_Host *shost = sdev->host;
1116 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301117 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1118 struct MPT2SAS_DEVICE *sas_device_priv_data;
1119 struct MPT2SAS_TARGET *sas_target_priv_data;
1120 struct _sas_device *sas_device;
1121 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001122
1123 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301124
1125 /* limit max device queue for SATA to 32 */
1126 sas_device_priv_data = sdev->hostdata;
1127 if (!sas_device_priv_data)
1128 goto not_sata;
1129 sas_target_priv_data = sas_device_priv_data->sas_target;
1130 if (!sas_target_priv_data)
1131 goto not_sata;
1132 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1133 goto not_sata;
1134 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1135 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1136 sas_device_priv_data->sas_target->sas_address);
1137 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1138 if (sas_device && sas_device->device_info &
1139 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1140 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1141
1142 not_sata:
1143
Eric Moore635374e2009-03-09 01:21:12 -06001144 if (!sdev->tagged_supported)
1145 max_depth = 1;
1146 if (qdepth > max_depth)
1147 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301148 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1149}
1150
1151/**
1152 * _scsih_change_queue_depth - setting device queue depth
1153 * @sdev: scsi device struct
1154 * @qdepth: requested queue depth
1155 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1156 * (see include/scsi/scsi_host.h for definition)
1157 *
1158 * Returns queue depth.
1159 */
1160static int
1161_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1162{
1163 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1164 _scsih_adjust_queue_depth(sdev, qdepth);
1165 else if (reason == SCSI_QDEPTH_QFULL)
1166 scsi_track_queue_full(sdev, qdepth);
1167 else
1168 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001169
1170 if (sdev->inquiry_len > 7)
1171 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1172 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1173 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1174 sdev->ordered_tags, sdev->scsi_level,
1175 (sdev->inquiry[7] & 2) >> 1);
1176
1177 return sdev->queue_depth;
1178}
1179
1180/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301181 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001182 * @sdev: scsi device struct
1183 * @tag_type: requested tag type
1184 *
1185 * Returns queue tag type.
1186 */
1187static int
Eric Moored5d135b2009-05-18 13:02:08 -06001188_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001189{
1190 if (sdev->tagged_supported) {
1191 scsi_set_tag_type(sdev, tag_type);
1192 if (tag_type)
1193 scsi_activate_tcq(sdev, sdev->queue_depth);
1194 else
1195 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1196 } else
1197 tag_type = 0;
1198
1199 return tag_type;
1200}
1201
1202/**
Eric Moored5d135b2009-05-18 13:02:08 -06001203 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001204 * @starget: scsi target struct
1205 *
1206 * Returns 0 if ok. Any other return is assumed to be an error and
1207 * the device is ignored.
1208 */
1209static int
Eric Moored5d135b2009-05-18 13:02:08 -06001210_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001211{
1212 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1213 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1214 struct MPT2SAS_TARGET *sas_target_priv_data;
1215 struct _sas_device *sas_device;
1216 struct _raid_device *raid_device;
1217 unsigned long flags;
1218 struct sas_rphy *rphy;
1219
1220 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1221 if (!sas_target_priv_data)
1222 return -ENOMEM;
1223
1224 starget->hostdata = sas_target_priv_data;
1225 sas_target_priv_data->starget = starget;
1226 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1227
1228 /* RAID volumes */
1229 if (starget->channel == RAID_CHANNEL) {
1230 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1231 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1232 starget->channel);
1233 if (raid_device) {
1234 sas_target_priv_data->handle = raid_device->handle;
1235 sas_target_priv_data->sas_address = raid_device->wwid;
1236 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1237 raid_device->starget = starget;
1238 }
1239 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1240 return 0;
1241 }
1242
1243 /* sas/sata devices */
1244 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1245 rphy = dev_to_rphy(starget->dev.parent);
1246 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1247 rphy->identify.sas_address);
1248
1249 if (sas_device) {
1250 sas_target_priv_data->handle = sas_device->handle;
1251 sas_target_priv_data->sas_address = sas_device->sas_address;
1252 sas_device->starget = starget;
1253 sas_device->id = starget->id;
1254 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301255 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001256 sas_target_priv_data->flags |=
1257 MPT_TARGET_FLAGS_RAID_COMPONENT;
1258 }
1259 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1260
1261 return 0;
1262}
1263
1264/**
Eric Moored5d135b2009-05-18 13:02:08 -06001265 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001266 * @starget: scsi target struct
1267 *
1268 * Returns nothing.
1269 */
1270static void
Eric Moored5d135b2009-05-18 13:02:08 -06001271_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001272{
1273 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1274 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1275 struct MPT2SAS_TARGET *sas_target_priv_data;
1276 struct _sas_device *sas_device;
1277 struct _raid_device *raid_device;
1278 unsigned long flags;
1279 struct sas_rphy *rphy;
1280
1281 sas_target_priv_data = starget->hostdata;
1282 if (!sas_target_priv_data)
1283 return;
1284
1285 if (starget->channel == RAID_CHANNEL) {
1286 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1287 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1288 starget->channel);
1289 if (raid_device) {
1290 raid_device->starget = NULL;
1291 raid_device->sdev = NULL;
1292 }
1293 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1294 goto out;
1295 }
1296
1297 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1298 rphy = dev_to_rphy(starget->dev.parent);
1299 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1300 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001301 if (sas_device && (sas_device->starget == starget) &&
1302 (sas_device->id == starget->id) &&
1303 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001304 sas_device->starget = NULL;
1305
1306 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1307
1308 out:
1309 kfree(sas_target_priv_data);
1310 starget->hostdata = NULL;
1311}
1312
1313/**
Eric Moored5d135b2009-05-18 13:02:08 -06001314 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001315 * @sdev: scsi device struct
1316 *
1317 * Returns 0 if ok. Any other return is assumed to be an error and
1318 * the device is ignored.
1319 */
1320static int
Eric Moored5d135b2009-05-18 13:02:08 -06001321_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001322{
1323 struct Scsi_Host *shost;
1324 struct MPT2SAS_ADAPTER *ioc;
1325 struct MPT2SAS_TARGET *sas_target_priv_data;
1326 struct MPT2SAS_DEVICE *sas_device_priv_data;
1327 struct scsi_target *starget;
1328 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001329 unsigned long flags;
1330
1331 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1332 if (!sas_device_priv_data)
1333 return -ENOMEM;
1334
1335 sas_device_priv_data->lun = sdev->lun;
1336 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1337
1338 starget = scsi_target(sdev);
1339 sas_target_priv_data = starget->hostdata;
1340 sas_target_priv_data->num_luns++;
1341 sas_device_priv_data->sas_target = sas_target_priv_data;
1342 sdev->hostdata = sas_device_priv_data;
1343 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1344 sdev->no_uld_attach = 1;
1345
1346 shost = dev_to_shost(&starget->dev);
1347 ioc = shost_priv(shost);
1348 if (starget->channel == RAID_CHANNEL) {
1349 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1350 raid_device = _scsih_raid_device_find_by_id(ioc,
1351 starget->id, starget->channel);
1352 if (raid_device)
1353 raid_device->sdev = sdev; /* raid is single lun */
1354 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001355 }
1356
Eric Moore635374e2009-03-09 01:21:12 -06001357 return 0;
1358}
1359
1360/**
Eric Moored5d135b2009-05-18 13:02:08 -06001361 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001362 * @sdev: scsi device struct
1363 *
1364 * Returns nothing.
1365 */
1366static void
Eric Moored5d135b2009-05-18 13:02:08 -06001367_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001368{
1369 struct MPT2SAS_TARGET *sas_target_priv_data;
1370 struct scsi_target *starget;
1371
1372 if (!sdev->hostdata)
1373 return;
1374
1375 starget = scsi_target(sdev);
1376 sas_target_priv_data = starget->hostdata;
1377 sas_target_priv_data->num_luns--;
1378 kfree(sdev->hostdata);
1379 sdev->hostdata = NULL;
1380}
1381
1382/**
Eric Moored5d135b2009-05-18 13:02:08 -06001383 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001384 * @ioc: per adapter object
1385 * @sas_device: the sas_device object
1386 * @sdev: scsi device struct
1387 */
1388static void
Eric Moored5d135b2009-05-18 13:02:08 -06001389_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001390 struct _sas_device *sas_device, struct scsi_device *sdev)
1391{
1392 Mpi2ConfigReply_t mpi_reply;
1393 Mpi2SasDevicePage0_t sas_device_pg0;
1394 u32 ioc_status;
1395 u16 flags;
1396 u32 device_info;
1397
1398 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1399 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1400 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1401 ioc->name, __FILE__, __LINE__, __func__);
1402 return;
1403 }
1404
1405 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1406 MPI2_IOCSTATUS_MASK;
1407 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1408 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1409 ioc->name, __FILE__, __LINE__, __func__);
1410 return;
1411 }
1412
1413 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301414 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001415
1416 sdev_printk(KERN_INFO, sdev,
1417 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1418 "sw_preserve(%s)\n",
1419 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1420 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1421 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1422 "n",
1423 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1424 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1425 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1426}
1427
1428/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301429 * _scsih_is_raid - return boolean indicating device is raid volume
1430 * @dev the device struct object
1431 */
1432static int
1433_scsih_is_raid(struct device *dev)
1434{
1435 struct scsi_device *sdev = to_scsi_device(dev);
1436
1437 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1438}
1439
1440/**
1441 * _scsih_get_resync - get raid volume resync percent complete
1442 * @dev the device struct object
1443 */
1444static void
1445_scsih_get_resync(struct device *dev)
1446{
1447 struct scsi_device *sdev = to_scsi_device(dev);
1448 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1449 static struct _raid_device *raid_device;
1450 unsigned long flags;
1451 Mpi2RaidVolPage0_t vol_pg0;
1452 Mpi2ConfigReply_t mpi_reply;
1453 u32 volume_status_flags;
1454 u8 percent_complete = 0;
1455
1456 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1457 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1458 sdev->channel);
1459 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1460
1461 if (!raid_device)
1462 goto out;
1463
1464 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1465 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1466 sizeof(Mpi2RaidVolPage0_t))) {
1467 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1468 ioc->name, __FILE__, __LINE__, __func__);
1469 goto out;
1470 }
1471
1472 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1473 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1474 percent_complete = raid_device->percent_complete;
1475 out:
1476 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1477}
1478
1479/**
1480 * _scsih_get_state - get raid volume level
1481 * @dev the device struct object
1482 */
1483static void
1484_scsih_get_state(struct device *dev)
1485{
1486 struct scsi_device *sdev = to_scsi_device(dev);
1487 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1488 static struct _raid_device *raid_device;
1489 unsigned long flags;
1490 Mpi2RaidVolPage0_t vol_pg0;
1491 Mpi2ConfigReply_t mpi_reply;
1492 u32 volstate;
1493 enum raid_state state = RAID_STATE_UNKNOWN;
1494
1495 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1496 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1497 sdev->channel);
1498 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1499
1500 if (!raid_device)
1501 goto out;
1502
1503 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1504 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1505 sizeof(Mpi2RaidVolPage0_t))) {
1506 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1507 ioc->name, __FILE__, __LINE__, __func__);
1508 goto out;
1509 }
1510
1511 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1512 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1513 state = RAID_STATE_RESYNCING;
1514 goto out;
1515 }
1516
1517 switch (vol_pg0.VolumeState) {
1518 case MPI2_RAID_VOL_STATE_OPTIMAL:
1519 case MPI2_RAID_VOL_STATE_ONLINE:
1520 state = RAID_STATE_ACTIVE;
1521 break;
1522 case MPI2_RAID_VOL_STATE_DEGRADED:
1523 state = RAID_STATE_DEGRADED;
1524 break;
1525 case MPI2_RAID_VOL_STATE_FAILED:
1526 case MPI2_RAID_VOL_STATE_MISSING:
1527 state = RAID_STATE_OFFLINE;
1528 break;
1529 }
1530 out:
1531 raid_set_state(mpt2sas_raid_template, dev, state);
1532}
1533
1534/**
1535 * _scsih_set_level - set raid level
1536 * @sdev: scsi device struct
1537 * @raid_device: raid_device object
1538 */
1539static void
1540_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1541{
1542 enum raid_level level = RAID_LEVEL_UNKNOWN;
1543
1544 switch (raid_device->volume_type) {
1545 case MPI2_RAID_VOL_TYPE_RAID0:
1546 level = RAID_LEVEL_0;
1547 break;
1548 case MPI2_RAID_VOL_TYPE_RAID10:
1549 level = RAID_LEVEL_10;
1550 break;
1551 case MPI2_RAID_VOL_TYPE_RAID1E:
1552 level = RAID_LEVEL_1E;
1553 break;
1554 case MPI2_RAID_VOL_TYPE_RAID1:
1555 level = RAID_LEVEL_1;
1556 break;
1557 }
1558
1559 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1560}
1561
1562/**
Eric Moore635374e2009-03-09 01:21:12 -06001563 * _scsih_get_volume_capabilities - volume capabilities
1564 * @ioc: per adapter object
1565 * @sas_device: the raid_device object
1566 */
1567static void
1568_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1569 struct _raid_device *raid_device)
1570{
1571 Mpi2RaidVolPage0_t *vol_pg0;
1572 Mpi2RaidPhysDiskPage0_t pd_pg0;
1573 Mpi2SasDevicePage0_t sas_device_pg0;
1574 Mpi2ConfigReply_t mpi_reply;
1575 u16 sz;
1576 u8 num_pds;
1577
1578 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1579 &num_pds)) || !num_pds) {
1580 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1581 ioc->name, __FILE__, __LINE__, __func__);
1582 return;
1583 }
1584
1585 raid_device->num_pds = num_pds;
1586 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1587 sizeof(Mpi2RaidVol0PhysDisk_t));
1588 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1589 if (!vol_pg0) {
1590 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1591 ioc->name, __FILE__, __LINE__, __func__);
1592 return;
1593 }
1594
1595 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1596 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1597 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1598 ioc->name, __FILE__, __LINE__, __func__);
1599 kfree(vol_pg0);
1600 return;
1601 }
1602
1603 raid_device->volume_type = vol_pg0->VolumeType;
1604
1605 /* figure out what the underlying devices are by
1606 * obtaining the device_info bits for the 1st device
1607 */
1608 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1609 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1610 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1611 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1612 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1613 le16_to_cpu(pd_pg0.DevHandle)))) {
1614 raid_device->device_info =
1615 le32_to_cpu(sas_device_pg0.DeviceInfo);
1616 }
1617 }
1618
1619 kfree(vol_pg0);
1620}
1621
1622/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301623 * _scsih_enable_tlr - setting TLR flags
1624 * @ioc: per adapter object
1625 * @sdev: scsi device struct
1626 *
1627 * Enabling Transaction Layer Retries for tape devices when
1628 * vpd page 0x90 is present
1629 *
1630 */
1631static void
1632_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1633{
1634 /* only for TAPE */
1635 if (sdev->type != TYPE_TAPE)
1636 return;
1637
1638 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1639 return;
1640
1641 sas_enable_tlr(sdev);
1642 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1643 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1644 return;
1645
1646}
1647
1648/**
Eric Moored5d135b2009-05-18 13:02:08 -06001649 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001650 * @sdev: scsi device struct
1651 *
1652 * Returns 0 if ok. Any other return is assumed to be an error and
1653 * the device is ignored.
1654 */
1655static int
Eric Moored5d135b2009-05-18 13:02:08 -06001656_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001657{
1658 struct Scsi_Host *shost = sdev->host;
1659 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1660 struct MPT2SAS_DEVICE *sas_device_priv_data;
1661 struct MPT2SAS_TARGET *sas_target_priv_data;
1662 struct _sas_device *sas_device;
1663 struct _raid_device *raid_device;
1664 unsigned long flags;
1665 int qdepth;
1666 u8 ssp_target = 0;
1667 char *ds = "";
1668 char *r_level = "";
1669
1670 qdepth = 1;
1671 sas_device_priv_data = sdev->hostdata;
1672 sas_device_priv_data->configured_lun = 1;
1673 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1674 sas_target_priv_data = sas_device_priv_data->sas_target;
1675
1676 /* raid volume handling */
1677 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1678
1679 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1680 raid_device = _scsih_raid_device_find_by_handle(ioc,
1681 sas_target_priv_data->handle);
1682 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1683 if (!raid_device) {
1684 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1685 ioc->name, __FILE__, __LINE__, __func__);
1686 return 0;
1687 }
1688
1689 _scsih_get_volume_capabilities(ioc, raid_device);
1690
1691 /* RAID Queue Depth Support
1692 * IS volume = underlying qdepth of drive type, either
1693 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1694 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1695 */
1696 if (raid_device->device_info &
1697 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1698 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1699 ds = "SSP";
1700 } else {
1701 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1702 if (raid_device->device_info &
1703 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1704 ds = "SATA";
1705 else
1706 ds = "STP";
1707 }
1708
1709 switch (raid_device->volume_type) {
1710 case MPI2_RAID_VOL_TYPE_RAID0:
1711 r_level = "RAID0";
1712 break;
1713 case MPI2_RAID_VOL_TYPE_RAID1E:
1714 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301715 if (ioc->manu_pg10.OEMIdentifier &&
1716 (ioc->manu_pg10.GenericFlags0 &
1717 MFG10_GF0_R10_DISPLAY) &&
1718 !(raid_device->num_pds % 2))
1719 r_level = "RAID10";
1720 else
1721 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001722 break;
1723 case MPI2_RAID_VOL_TYPE_RAID1:
1724 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1725 r_level = "RAID1";
1726 break;
1727 case MPI2_RAID_VOL_TYPE_RAID10:
1728 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1729 r_level = "RAID10";
1730 break;
1731 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1732 default:
1733 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1734 r_level = "RAIDX";
1735 break;
1736 }
1737
1738 sdev_printk(KERN_INFO, sdev, "%s: "
1739 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1740 r_level, raid_device->handle,
1741 (unsigned long long)raid_device->wwid,
1742 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001743 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301744 /* raid transport support */
1745 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001746 return 0;
1747 }
1748
1749 /* non-raid handling */
1750 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1751 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1752 sas_device_priv_data->sas_target->sas_address);
1753 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1754 if (sas_device) {
1755 if (sas_target_priv_data->flags &
1756 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1757 mpt2sas_config_get_volume_handle(ioc,
1758 sas_device->handle, &sas_device->volume_handle);
1759 mpt2sas_config_get_volume_wwid(ioc,
1760 sas_device->volume_handle,
1761 &sas_device->volume_wwid);
1762 }
1763 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1764 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1765 ssp_target = 1;
1766 ds = "SSP";
1767 } else {
1768 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1769 if (sas_device->device_info &
1770 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1771 ds = "STP";
1772 else if (sas_device->device_info &
1773 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1774 ds = "SATA";
1775 }
1776
1777 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05301778 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06001779 ds, sas_device->handle,
1780 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05301781 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06001782 (unsigned long long)sas_device->device_name);
1783 sdev_printk(KERN_INFO, sdev, "%s: "
1784 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1785 (unsigned long long) sas_device->enclosure_logical_id,
1786 sas_device->slot);
1787
1788 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001789 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001790 }
1791
Mike Christiee881a172009-10-15 17:46:39 -07001792 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06001793
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301794 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06001795 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301796 _scsih_enable_tlr(ioc, sdev);
1797 }
Eric Moore635374e2009-03-09 01:21:12 -06001798 return 0;
1799}
1800
1801/**
Eric Moored5d135b2009-05-18 13:02:08 -06001802 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001803 * @sdev: scsi device struct
1804 * @bdev: pointer to block device context
1805 * @capacity: device size (in 512 byte sectors)
1806 * @params: three element array to place output:
1807 * params[0] number of heads (max 255)
1808 * params[1] number of sectors (max 63)
1809 * params[2] number of cylinders
1810 *
1811 * Return nothing.
1812 */
1813static int
Eric Moored5d135b2009-05-18 13:02:08 -06001814_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001815 sector_t capacity, int params[])
1816{
1817 int heads;
1818 int sectors;
1819 sector_t cylinders;
1820 ulong dummy;
1821
1822 heads = 64;
1823 sectors = 32;
1824
1825 dummy = heads * sectors;
1826 cylinders = capacity;
1827 sector_div(cylinders, dummy);
1828
1829 /*
1830 * Handle extended translation size for logical drives
1831 * > 1Gb
1832 */
1833 if ((ulong)capacity >= 0x200000) {
1834 heads = 255;
1835 sectors = 63;
1836 dummy = heads * sectors;
1837 cylinders = capacity;
1838 sector_div(cylinders, dummy);
1839 }
1840
1841 /* return result */
1842 params[0] = heads;
1843 params[1] = sectors;
1844 params[2] = cylinders;
1845
1846 return 0;
1847}
1848
1849/**
1850 * _scsih_response_code - translation of device response code
1851 * @ioc: per adapter object
1852 * @response_code: response code returned by the device
1853 *
1854 * Return nothing.
1855 */
1856static void
1857_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1858{
1859 char *desc;
1860
1861 switch (response_code) {
1862 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1863 desc = "task management request completed";
1864 break;
1865 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1866 desc = "invalid frame";
1867 break;
1868 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1869 desc = "task management request not supported";
1870 break;
1871 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1872 desc = "task management request failed";
1873 break;
1874 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1875 desc = "task management request succeeded";
1876 break;
1877 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1878 desc = "invalid lun";
1879 break;
1880 case 0xA:
1881 desc = "overlapped tag attempted";
1882 break;
1883 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1884 desc = "task queued, however not sent to target";
1885 break;
1886 default:
1887 desc = "unknown";
1888 break;
1889 }
1890 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1891 ioc->name, response_code, desc);
1892}
1893
1894/**
Eric Moored5d135b2009-05-18 13:02:08 -06001895 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001896 * @ioc: per adapter object
1897 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301898 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001899 * @reply: reply message frame(lower 32bit addr)
1900 * Context: none.
1901 *
1902 * The callback handler when using scsih_issue_tm.
1903 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301904 * Return 1 meaning mf should be freed from _base_interrupt
1905 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001906 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301907static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301908_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001909{
1910 MPI2DefaultReply_t *mpi_reply;
1911
1912 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301913 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001914 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301915 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001916 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1917 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1918 if (mpi_reply) {
1919 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1920 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1921 }
1922 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1923 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301924 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001925}
1926
1927/**
1928 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1929 * @ioc: per adapter object
1930 * @handle: device handle
1931 *
1932 * During taskmangement request, we need to freeze the device queue.
1933 */
1934void
1935mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1936{
1937 struct MPT2SAS_DEVICE *sas_device_priv_data;
1938 struct scsi_device *sdev;
1939 u8 skip = 0;
1940
1941 shost_for_each_device(sdev, ioc->shost) {
1942 if (skip)
1943 continue;
1944 sas_device_priv_data = sdev->hostdata;
1945 if (!sas_device_priv_data)
1946 continue;
1947 if (sas_device_priv_data->sas_target->handle == handle) {
1948 sas_device_priv_data->sas_target->tm_busy = 1;
1949 skip = 1;
1950 ioc->ignore_loginfos = 1;
1951 }
1952 }
1953}
1954
1955/**
1956 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1957 * @ioc: per adapter object
1958 * @handle: device handle
1959 *
1960 * During taskmangement request, we need to freeze the device queue.
1961 */
1962void
1963mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1964{
1965 struct MPT2SAS_DEVICE *sas_device_priv_data;
1966 struct scsi_device *sdev;
1967 u8 skip = 0;
1968
1969 shost_for_each_device(sdev, ioc->shost) {
1970 if (skip)
1971 continue;
1972 sas_device_priv_data = sdev->hostdata;
1973 if (!sas_device_priv_data)
1974 continue;
1975 if (sas_device_priv_data->sas_target->handle == handle) {
1976 sas_device_priv_data->sas_target->tm_busy = 0;
1977 skip = 1;
1978 ioc->ignore_loginfos = 0;
1979 }
1980 }
1981}
1982
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301983
Eric Moore635374e2009-03-09 01:21:12 -06001984/**
1985 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1986 * @ioc: per adapter struct
1987 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301988 * @channel: the channel assigned by the OS
1989 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001990 * @lun: lun number
1991 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1992 * @smid_task: smid assigned to the task
1993 * @timeout: timeout in seconds
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301994 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06001995 *
1996 * A generic API for sending task management requests to firmware.
1997 *
Eric Moore635374e2009-03-09 01:21:12 -06001998 * The callback index is set inside `ioc->tm_cb_idx`.
1999 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302000 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002001 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302002int
2003mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2004 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2005 struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002006{
2007 Mpi2SCSITaskManagementRequest_t *mpi_request;
2008 Mpi2SCSITaskManagementReply_t *mpi_reply;
2009 u16 smid = 0;
2010 u32 ioc_state;
2011 unsigned long timeleft;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302012 struct scsi_cmnd *scmd_lookup;
2013 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002014
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302015 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302016 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2017 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2018 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302019 rc = FAILED;
2020 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302021 }
2022
Eric Moore3cb54692010-07-08 14:44:34 -06002023 if (ioc->shost_recovery || ioc->remove_host ||
2024 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002025 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2026 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302027 rc = FAILED;
2028 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002029 }
Eric Moore635374e2009-03-09 01:21:12 -06002030
2031 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2032 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302033 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002034 "active!\n", ioc->name));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302035 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2036 FORCE_BIG_HAMMER);
2037 rc = SUCCESS;
2038 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002039 }
2040
2041 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2042 mpt2sas_base_fault_info(ioc, ioc_state &
2043 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302044 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2045 FORCE_BIG_HAMMER);
2046 rc = SUCCESS;
2047 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002048 }
2049
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302050 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002051 if (!smid) {
2052 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2053 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302054 rc = FAILED;
2055 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002056 }
2057
2058 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302059 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2060 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002061 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2062 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2063 ioc->tm_cmds.smid = smid;
2064 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2065 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2066 mpi_request->DevHandle = cpu_to_le16(handle);
2067 mpi_request->TaskType = type;
2068 mpi_request->TaskMID = cpu_to_le16(smid_task);
2069 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2070 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302071 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302072 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002073 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002074 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2075 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2076 ioc->name, __func__);
2077 _debug_dump_mf(mpi_request,
2078 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302079 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2080 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2081 FORCE_BIG_HAMMER);
2082 rc = SUCCESS;
2083 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2084 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2085 goto err_out;
2086 }
Eric Moore635374e2009-03-09 01:21:12 -06002087 }
2088
2089 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2090 mpi_reply = ioc->tm_cmds.reply;
2091 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2092 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2093 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2094 le32_to_cpu(mpi_reply->IOCLogInfo),
2095 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302096 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002097 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302098 if (mpi_reply->IOCStatus)
2099 _debug_dump_mf(mpi_request,
2100 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2101 }
Eric Moore635374e2009-03-09 01:21:12 -06002102 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302103
2104 /* sanity check:
2105 * Check to see the commands were terminated.
2106 * This is only needed for eh callbacks, hence the scmd check.
2107 */
2108 rc = FAILED;
2109 if (scmd == NULL)
2110 goto bypass_sanity_checks;
2111 switch (type) {
2112 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2113 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2114 if (scmd_lookup && (scmd_lookup->serial_number ==
2115 scmd->serial_number))
2116 rc = FAILED;
2117 else
2118 rc = SUCCESS;
2119 break;
2120
2121 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2122 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2123 rc = FAILED;
2124 else
2125 rc = SUCCESS;
2126 break;
2127
2128 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2129 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2130 rc = FAILED;
2131 else
2132 rc = SUCCESS;
2133 break;
2134 }
2135
2136 bypass_sanity_checks:
2137
2138 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2139 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2140 mutex_unlock(&ioc->tm_cmds.mutex);
2141
2142 return rc;
2143
2144 err_out:
2145 mutex_unlock(&ioc->tm_cmds.mutex);
2146 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002147}
2148
2149/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302150 * _scsih_tm_display_info - displays info about the device
2151 * @ioc: per adapter struct
2152 * @scmd: pointer to scsi command object
2153 *
2154 * Called by task management callback handlers.
2155 */
2156static void
2157_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2158{
2159 struct scsi_target *starget = scmd->device->sdev_target;
2160 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2161 struct _sas_device *sas_device = NULL;
2162 unsigned long flags;
2163
2164 if (!priv_target)
2165 return;
2166
2167 scsi_print_command(scmd);
2168 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2169 starget_printk(KERN_INFO, starget, "volume handle(0x%04x), "
2170 "volume wwid(0x%016llx)\n",
2171 priv_target->handle,
2172 (unsigned long long)priv_target->sas_address);
2173 } else {
2174 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2175 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2176 priv_target->sas_address);
2177 if (sas_device) {
2178 if (priv_target->flags &
2179 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2180 starget_printk(KERN_INFO, starget,
2181 "volume handle(0x%04x), "
2182 "volume wwid(0x%016llx)\n",
2183 sas_device->volume_handle,
2184 (unsigned long long)sas_device->volume_wwid);
2185 }
2186 starget_printk(KERN_INFO, starget,
2187 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2188 sas_device->handle,
2189 (unsigned long long)sas_device->sas_address,
2190 sas_device->phy);
2191 starget_printk(KERN_INFO, starget,
2192 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2193 (unsigned long long)sas_device->enclosure_logical_id,
2194 sas_device->slot);
2195 }
2196 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2197 }
2198}
2199
2200/**
Eric Moored5d135b2009-05-18 13:02:08 -06002201 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302202 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002203 *
2204 * Returns SUCCESS if command aborted else FAILED
2205 */
2206static int
Eric Moored5d135b2009-05-18 13:02:08 -06002207_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002208{
2209 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2210 struct MPT2SAS_DEVICE *sas_device_priv_data;
2211 u16 smid;
2212 u16 handle;
2213 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002214
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302215 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2216 "scmd(%p)\n", scmd);
2217 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002218
2219 sas_device_priv_data = scmd->device->hostdata;
2220 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302221 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2222 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002223 scmd->result = DID_NO_CONNECT << 16;
2224 scmd->scsi_done(scmd);
2225 r = SUCCESS;
2226 goto out;
2227 }
2228
2229 /* search for the command */
2230 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2231 if (!smid) {
2232 scmd->result = DID_RESET << 16;
2233 r = SUCCESS;
2234 goto out;
2235 }
2236
2237 /* for hidden raid components and volumes this is not supported */
2238 if (sas_device_priv_data->sas_target->flags &
2239 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2240 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2241 scmd->result = DID_RESET << 16;
2242 r = FAILED;
2243 goto out;
2244 }
2245
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302246 mpt2sas_halt_firmware(ioc);
2247
Eric Moore635374e2009-03-09 01:21:12 -06002248 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302249 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2250 scmd->device->id, scmd->device->lun,
2251 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002252
2253 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302254 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2255 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002256 return r;
2257}
2258
Eric Moore635374e2009-03-09 01:21:12 -06002259/**
Eric Moored5d135b2009-05-18 13:02:08 -06002260 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302261 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002262 *
2263 * Returns SUCCESS if command aborted else FAILED
2264 */
2265static int
Eric Moored5d135b2009-05-18 13:02:08 -06002266_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002267{
2268 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2269 struct MPT2SAS_DEVICE *sas_device_priv_data;
2270 struct _sas_device *sas_device;
2271 unsigned long flags;
2272 u16 handle;
2273 int r;
2274
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302275 struct scsi_target *starget = scmd->device->sdev_target;
2276
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302277 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302278 "scmd(%p)\n", scmd);
2279 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002280
2281 sas_device_priv_data = scmd->device->hostdata;
2282 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302283 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302284 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002285 scmd->result = DID_NO_CONNECT << 16;
2286 scmd->scsi_done(scmd);
2287 r = SUCCESS;
2288 goto out;
2289 }
2290
2291 /* for hidden raid components obtain the volume_handle */
2292 handle = 0;
2293 if (sas_device_priv_data->sas_target->flags &
2294 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2295 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2296 sas_device = _scsih_sas_device_find_by_handle(ioc,
2297 sas_device_priv_data->sas_target->handle);
2298 if (sas_device)
2299 handle = sas_device->volume_handle;
2300 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2301 } else
2302 handle = sas_device_priv_data->sas_target->handle;
2303
2304 if (!handle) {
2305 scmd->result = DID_RESET << 16;
2306 r = FAILED;
2307 goto out;
2308 }
2309
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302310 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2311 scmd->device->id, scmd->device->lun,
2312 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002313
2314 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302315 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2316 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002317 return r;
2318}
2319
2320/**
Eric Moored5d135b2009-05-18 13:02:08 -06002321 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302322 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002323 *
2324 * Returns SUCCESS if command aborted else FAILED
2325 */
2326static int
Eric Moored5d135b2009-05-18 13:02:08 -06002327_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002328{
2329 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2330 struct MPT2SAS_DEVICE *sas_device_priv_data;
2331 struct _sas_device *sas_device;
2332 unsigned long flags;
2333 u16 handle;
2334 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302335 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002336
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302337 starget_printk(KERN_INFO, starget, "attempting target reset! "
2338 "scmd(%p)\n", scmd);
2339 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002340
2341 sas_device_priv_data = scmd->device->hostdata;
2342 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302343 starget_printk(KERN_INFO, starget, "target been deleted! "
2344 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002345 scmd->result = DID_NO_CONNECT << 16;
2346 scmd->scsi_done(scmd);
2347 r = SUCCESS;
2348 goto out;
2349 }
2350
2351 /* for hidden raid components obtain the volume_handle */
2352 handle = 0;
2353 if (sas_device_priv_data->sas_target->flags &
2354 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2355 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2356 sas_device = _scsih_sas_device_find_by_handle(ioc,
2357 sas_device_priv_data->sas_target->handle);
2358 if (sas_device)
2359 handle = sas_device->volume_handle;
2360 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2361 } else
2362 handle = sas_device_priv_data->sas_target->handle;
2363
2364 if (!handle) {
2365 scmd->result = DID_RESET << 16;
2366 r = FAILED;
2367 goto out;
2368 }
2369
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302370 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2371 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2372 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002373
2374 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302375 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2376 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002377 return r;
2378}
2379
2380/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302381 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302382 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002383 *
2384 * Returns SUCCESS if command aborted else FAILED
2385 */
2386static int
Eric Moored5d135b2009-05-18 13:02:08 -06002387_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002388{
2389 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2390 int r, retval;
2391
2392 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2393 ioc->name, scmd);
2394 scsi_print_command(scmd);
2395
2396 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2397 FORCE_BIG_HAMMER);
2398 r = (retval < 0) ? FAILED : SUCCESS;
2399 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2400 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2401
2402 return r;
2403}
2404
2405/**
2406 * _scsih_fw_event_add - insert and queue up fw_event
2407 * @ioc: per adapter object
2408 * @fw_event: object describing the event
2409 * Context: This function will acquire ioc->fw_event_lock.
2410 *
2411 * This adds the firmware event object into link list, then queues it up to
2412 * be processed from user context.
2413 *
2414 * Return nothing.
2415 */
2416static void
2417_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2418{
2419 unsigned long flags;
2420
2421 if (ioc->firmware_event_thread == NULL)
2422 return;
2423
2424 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2425 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302426 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2427 queue_delayed_work(ioc->firmware_event_thread,
2428 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002429 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2430}
2431
2432/**
2433 * _scsih_fw_event_free - delete fw_event
2434 * @ioc: per adapter object
2435 * @fw_event: object describing the event
2436 * Context: This function will acquire ioc->fw_event_lock.
2437 *
2438 * This removes firmware event object from link list, frees associated memory.
2439 *
2440 * Return nothing.
2441 */
2442static void
2443_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2444 *fw_event)
2445{
2446 unsigned long flags;
2447
2448 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2449 list_del(&fw_event->list);
2450 kfree(fw_event->event_data);
2451 kfree(fw_event);
2452 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2453}
2454
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302455
Eric Moore635374e2009-03-09 01:21:12 -06002456/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302457 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002458 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002459 *
2460 * Return nothing.
2461 */
2462static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302463_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002464{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302465 struct fw_event_work *fw_event;
2466
2467 if (ioc->wait_for_port_enable_to_complete)
2468 return;
2469 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2470 if (!fw_event)
2471 return;
2472 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2473 fw_event->ioc = ioc;
2474 _scsih_fw_event_add(ioc, fw_event);
2475}
2476
2477/**
2478 * _scsih_fw_event_cleanup_queue - cleanup event queue
2479 * @ioc: per adapter object
2480 *
2481 * Walk the firmware event queue, either killing timers, or waiting
2482 * for outstanding events to complete
2483 *
2484 * Return nothing.
2485 */
2486static void
2487_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2488{
2489 struct fw_event_work *fw_event, *next;
2490
2491 if (list_empty(&ioc->fw_event_list) ||
2492 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002493 return;
2494
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302495 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2496 if (cancel_delayed_work(&fw_event->delayed_work)) {
2497 _scsih_fw_event_free(ioc, fw_event);
2498 continue;
2499 }
2500 fw_event->cancel_pending_work = 1;
2501 }
Eric Moore635374e2009-03-09 01:21:12 -06002502}
2503
Eric Moore635374e2009-03-09 01:21:12 -06002504/**
2505 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2506 * @ioc: per adapter object
2507 * @handle: device handle
2508 *
2509 * During device pull we need to appropiately set the sdev state.
2510 */
2511static void
2512_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2513{
2514 struct MPT2SAS_DEVICE *sas_device_priv_data;
2515 struct scsi_device *sdev;
2516
2517 shost_for_each_device(sdev, ioc->shost) {
2518 sas_device_priv_data = sdev->hostdata;
2519 if (!sas_device_priv_data)
2520 continue;
2521 if (!sas_device_priv_data->block)
2522 continue;
2523 if (sas_device_priv_data->sas_target->handle == handle) {
2524 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2525 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2526 "handle(0x%04x)\n", ioc->name, handle));
2527 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302528 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002529 }
2530 }
2531}
2532
2533/**
2534 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2535 * @ioc: per adapter object
2536 * @handle: device handle
2537 *
2538 * During device pull we need to appropiately set the sdev state.
2539 */
2540static void
2541_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2542{
2543 struct MPT2SAS_DEVICE *sas_device_priv_data;
2544 struct scsi_device *sdev;
2545
2546 shost_for_each_device(sdev, ioc->shost) {
2547 sas_device_priv_data = sdev->hostdata;
2548 if (!sas_device_priv_data)
2549 continue;
2550 if (sas_device_priv_data->block)
2551 continue;
2552 if (sas_device_priv_data->sas_target->handle == handle) {
2553 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2554 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2555 "handle(0x%04x)\n", ioc->name, handle));
2556 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302557 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002558 }
2559 }
2560}
2561
2562/**
2563 * _scsih_block_io_to_children_attached_to_ex
2564 * @ioc: per adapter object
2565 * @sas_expander: the sas_device object
2566 *
2567 * This routine set sdev state to SDEV_BLOCK for all devices
2568 * attached to this expander. This function called when expander is
2569 * pulled.
2570 */
2571static void
2572_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2573 struct _sas_node *sas_expander)
2574{
2575 struct _sas_port *mpt2sas_port;
2576 struct _sas_device *sas_device;
2577 struct _sas_node *expander_sibling;
2578 unsigned long flags;
2579
2580 if (!sas_expander)
2581 return;
2582
2583 list_for_each_entry(mpt2sas_port,
2584 &sas_expander->sas_port_list, port_list) {
2585 if (mpt2sas_port->remote_identify.device_type ==
2586 SAS_END_DEVICE) {
2587 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2588 sas_device =
2589 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2590 mpt2sas_port->remote_identify.sas_address);
2591 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2592 if (!sas_device)
2593 continue;
2594 _scsih_block_io_device(ioc, sas_device->handle);
2595 }
2596 }
2597
2598 list_for_each_entry(mpt2sas_port,
2599 &sas_expander->sas_port_list, port_list) {
2600
2601 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302602 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06002603 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302604 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06002605
2606 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2607 expander_sibling =
2608 mpt2sas_scsih_expander_find_by_sas_address(
2609 ioc, mpt2sas_port->remote_identify.sas_address);
2610 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2611 _scsih_block_io_to_children_attached_to_ex(ioc,
2612 expander_sibling);
2613 }
2614 }
2615}
2616
2617/**
2618 * _scsih_block_io_to_children_attached_directly
2619 * @ioc: per adapter object
2620 * @event_data: topology change event data
2621 *
2622 * This routine set sdev state to SDEV_BLOCK for all devices
2623 * direct attached during device pull.
2624 */
2625static void
2626_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2627 Mpi2EventDataSasTopologyChangeList_t *event_data)
2628{
2629 int i;
2630 u16 handle;
2631 u16 reason_code;
2632 u8 phy_number;
2633
2634 for (i = 0; i < event_data->NumEntries; i++) {
2635 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2636 if (!handle)
2637 continue;
2638 phy_number = event_data->StartPhyNum + i;
2639 reason_code = event_data->PHY[i].PhyStatus &
2640 MPI2_EVENT_SAS_TOPO_RC_MASK;
2641 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2642 _scsih_block_io_device(ioc, handle);
2643 }
2644}
2645
2646/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302647 * _scsih_tm_tr_send - send task management request
2648 * @ioc: per adapter object
2649 * @handle: device handle
2650 * Context: interrupt time.
2651 *
2652 * This code is to initiate the device removal handshake protocal
2653 * with controller firmware. This function will issue target reset
2654 * using high priority request queue. It will send a sas iounit
2655 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2656 *
2657 * This is designed to send muliple task management request at the same
2658 * time to the fifo. If the fifo is full, we will append the request,
2659 * and process it in a future completion.
2660 */
2661static void
2662_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2663{
2664 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302665 u16 smid;
2666 struct _sas_device *sas_device;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302667 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302668 unsigned long flags;
2669 struct _tr_list *delayed_tr;
2670
Eric Moore3cb54692010-07-08 14:44:34 -06002671 if (ioc->shost_recovery || ioc->remove_host ||
2672 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302673 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2674 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302675 return;
2676 }
2677
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302678 /* if PD, then return */
2679 if (test_bit(handle, ioc->pd_handles))
2680 return;
2681
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302682 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2683 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302684 if (sas_device && sas_device->starget &&
2685 sas_device->starget->hostdata) {
2686 sas_target_priv_data = sas_device->starget->hostdata;
2687 sas_target_priv_data->deleted = 1;
2688 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2689 "setting delete flag: handle(0x%04x), "
2690 "sas_addr(0x%016llx)\n", ioc->name, handle,
2691 (unsigned long long) sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05302692 }
2693 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302694
2695 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2696 if (!smid) {
2697 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2698 if (!delayed_tr)
2699 return;
2700 INIT_LIST_HEAD(&delayed_tr->list);
2701 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302702 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2703 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2704 "DELAYED:tr:handle(0x%04x), (open)\n",
2705 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302706 return;
2707 }
2708
Kashyap, Desai1278b112010-03-09 17:34:13 +05302709 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2710 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2711 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302712 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2713 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2714 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2715 mpi_request->DevHandle = cpu_to_le16(handle);
2716 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302717 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2718}
2719
2720
2721
2722/**
2723 * _scsih_sas_control_complete - completion routine
2724 * @ioc: per adapter object
2725 * @smid: system request message index
2726 * @msix_index: MSIX table index supplied by the OS
2727 * @reply: reply message frame(lower 32bit addr)
2728 * Context: interrupt time.
2729 *
2730 * This is the sas iounit controll completion routine.
2731 * This code is part of the code to initiate the device removal
2732 * handshake protocal with controller firmware.
2733 *
2734 * Return 1 meaning mf should be freed from _base_interrupt
2735 * 0 means the mf is freed from this function.
2736 */
2737static u8
2738_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2739 u8 msix_index, u32 reply)
2740{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302741#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302742 Mpi2SasIoUnitControlReply_t *mpi_reply =
2743 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302744#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05302745 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2746 "sc_complete:handle(0x%04x), (open) "
2747 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2748 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2749 le16_to_cpu(mpi_reply->IOCStatus),
2750 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302751 return 1;
2752}
2753
2754/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302755 * _scsih_tm_tr_volume_send - send target reset request for volumes
2756 * @ioc: per adapter object
2757 * @handle: device handle
2758 * Context: interrupt time.
2759 *
2760 * This is designed to send muliple task management request at the same
2761 * time to the fifo. If the fifo is full, we will append the request,
2762 * and process it in a future completion.
2763 */
2764static void
2765_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2766{
2767 Mpi2SCSITaskManagementRequest_t *mpi_request;
2768 u16 smid;
2769 struct _tr_list *delayed_tr;
2770
Eric Moore3cb54692010-07-08 14:44:34 -06002771 if (ioc->shost_recovery || ioc->remove_host ||
2772 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302773 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2774 "progress!\n", __func__, ioc->name));
2775 return;
2776 }
2777
2778 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2779 if (!smid) {
2780 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2781 if (!delayed_tr)
2782 return;
2783 INIT_LIST_HEAD(&delayed_tr->list);
2784 delayed_tr->handle = handle;
2785 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2786 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2787 "DELAYED:tr:handle(0x%04x), (open)\n",
2788 ioc->name, handle));
2789 return;
2790 }
2791
2792 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2793 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2794 ioc->tm_tr_volume_cb_idx));
2795 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2796 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2797 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2798 mpi_request->DevHandle = cpu_to_le16(handle);
2799 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2800 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2801}
2802
2803/**
2804 * _scsih_tm_volume_tr_complete - target reset completion
2805 * @ioc: per adapter object
2806 * @smid: system request message index
2807 * @msix_index: MSIX table index supplied by the OS
2808 * @reply: reply message frame(lower 32bit addr)
2809 * Context: interrupt time.
2810 *
2811 * Return 1 meaning mf should be freed from _base_interrupt
2812 * 0 means the mf is freed from this function.
2813 */
2814static u8
2815_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2816 u8 msix_index, u32 reply)
2817{
2818 u16 handle;
2819 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2820 Mpi2SCSITaskManagementReply_t *mpi_reply =
2821 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2822
Eric Moore3cb54692010-07-08 14:44:34 -06002823 if (ioc->shost_recovery || ioc->remove_host ||
2824 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302825 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2826 "progress!\n", __func__, ioc->name));
2827 return 1;
2828 }
2829
2830 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2831 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2832 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2833 dewtprintk(ioc, printk("spurious interrupt: "
2834 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2835 le16_to_cpu(mpi_reply->DevHandle), smid));
2836 return 0;
2837 }
2838
2839 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2840 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2841 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2842 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2843 le32_to_cpu(mpi_reply->IOCLogInfo),
2844 le32_to_cpu(mpi_reply->TerminationCount)));
2845
2846 return _scsih_check_for_pending_tm(ioc, smid);
2847}
2848
2849/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302850 * _scsih_tm_tr_complete -
2851 * @ioc: per adapter object
2852 * @smid: system request message index
2853 * @msix_index: MSIX table index supplied by the OS
2854 * @reply: reply message frame(lower 32bit addr)
2855 * Context: interrupt time.
2856 *
2857 * This is the target reset completion routine.
2858 * This code is part of the code to initiate the device removal
2859 * handshake protocal with controller firmware.
2860 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2861 *
2862 * Return 1 meaning mf should be freed from _base_interrupt
2863 * 0 means the mf is freed from this function.
2864 */
2865static u8
2866_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2867 u32 reply)
2868{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302869 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302870 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302871 Mpi2SCSITaskManagementReply_t *mpi_reply =
2872 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2873 Mpi2SasIoUnitControlRequest_t *mpi_request;
2874 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302875
Eric Moore3cb54692010-07-08 14:44:34 -06002876 if (ioc->shost_recovery || ioc->remove_host ||
2877 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302878 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2879 "progress!\n", __func__, ioc->name));
2880 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302881 }
2882
Kashyap, Desai1278b112010-03-09 17:34:13 +05302883 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2884 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2885 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2886 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2887 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2888 le16_to_cpu(mpi_reply->DevHandle), smid));
2889 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302890 }
2891
Kashyap, Desai1278b112010-03-09 17:34:13 +05302892 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2893 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2894 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2895 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2896 le32_to_cpu(mpi_reply->IOCLogInfo),
2897 le32_to_cpu(mpi_reply->TerminationCount)));
2898
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302899 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2900 if (!smid_sas_ctrl) {
2901 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2902 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302903 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302904 }
2905
Kashyap, Desai1278b112010-03-09 17:34:13 +05302906 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2907 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2908 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302909 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2910 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2911 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2912 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302913 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302914 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302915
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302916 return _scsih_check_for_pending_tm(ioc, smid);
2917}
2918
2919/**
2920 * _scsih_check_for_pending_tm - check for pending task management
2921 * @ioc: per adapter object
2922 * @smid: system request message index
2923 *
2924 * This will check delayed target reset list, and feed the
2925 * next reqeust.
2926 *
2927 * Return 1 meaning mf should be freed from _base_interrupt
2928 * 0 means the mf is freed from this function.
2929 */
2930static u8
2931_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2932{
2933 struct _tr_list *delayed_tr;
2934
2935 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2936 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2937 struct _tr_list, list);
2938 mpt2sas_base_free_smid(ioc, smid);
2939 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2940 list_del(&delayed_tr->list);
2941 kfree(delayed_tr);
2942 return 0;
2943 }
2944
Kashyap, Desai1278b112010-03-09 17:34:13 +05302945 if (!list_empty(&ioc->delayed_tr_list)) {
2946 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2947 struct _tr_list, list);
2948 mpt2sas_base_free_smid(ioc, smid);
2949 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2950 list_del(&delayed_tr->list);
2951 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302952 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302953 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302954
Kashyap, Desai1278b112010-03-09 17:34:13 +05302955 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302956}
2957
2958/**
Eric Moore635374e2009-03-09 01:21:12 -06002959 * _scsih_check_topo_delete_events - sanity check on topo events
2960 * @ioc: per adapter object
2961 * @event_data: the event data payload
2962 *
2963 * This routine added to better handle cable breaker.
2964 *
2965 * This handles the case where driver recieves multiple expander
2966 * add and delete events in a single shot. When there is a delete event
2967 * the routine will void any pending add events waiting in the event queue.
2968 *
2969 * Return nothing.
2970 */
2971static void
2972_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2973 Mpi2EventDataSasTopologyChangeList_t *event_data)
2974{
2975 struct fw_event_work *fw_event;
2976 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2977 u16 expander_handle;
2978 struct _sas_node *sas_expander;
2979 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302980 int i, reason_code;
2981 u16 handle;
2982
2983 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302984 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2985 if (!handle)
2986 continue;
2987 reason_code = event_data->PHY[i].PhyStatus &
2988 MPI2_EVENT_SAS_TOPO_RC_MASK;
2989 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2990 _scsih_tm_tr_send(ioc, handle);
2991 }
Eric Moore635374e2009-03-09 01:21:12 -06002992
2993 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2994 if (expander_handle < ioc->sas_hba.num_phys) {
2995 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2996 return;
2997 }
2998
2999 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3000 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3001 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3002 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3003 expander_handle);
3004 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3005 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3006 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3007 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3008
3009 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3010 return;
3011
3012 /* mark ignore flag for pending events */
3013 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3014 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3015 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3016 fw_event->ignore)
3017 continue;
3018 local_event_data = fw_event->event_data;
3019 if (local_event_data->ExpStatus ==
3020 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3021 local_event_data->ExpStatus ==
3022 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3023 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3024 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303025 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003026 "setting ignoring flag\n", ioc->name));
3027 fw_event->ignore = 1;
3028 }
3029 }
3030 }
3031 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3032}
3033
3034/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303035 * _scsih_set_volume_delete_flag - setting volume delete flag
3036 * @ioc: per adapter object
3037 * @handle: device handle
3038 *
3039 * This
3040 * Return nothing.
3041 */
3042static void
3043_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3044{
3045 struct _raid_device *raid_device;
3046 struct MPT2SAS_TARGET *sas_target_priv_data;
3047 unsigned long flags;
3048
3049 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3050 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3051 if (raid_device && raid_device->starget &&
3052 raid_device->starget->hostdata) {
3053 sas_target_priv_data =
3054 raid_device->starget->hostdata;
3055 sas_target_priv_data->deleted = 1;
3056 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3057 "setting delete flag: handle(0x%04x), "
3058 "wwid(0x%016llx)\n", ioc->name, handle,
3059 (unsigned long long) raid_device->wwid));
3060 }
3061 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3062}
3063
3064/**
3065 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3066 * @handle: input handle
3067 * @a: handle for volume a
3068 * @b: handle for volume b
3069 *
3070 * IR firmware only supports two raid volumes. The purpose of this
3071 * routine is to set the volume handle in either a or b. When the given
3072 * input handle is non-zero, or when a and b have not been set before.
3073 */
3074static void
3075_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3076{
3077 if (!handle || handle == *a || handle == *b)
3078 return;
3079 if (!*a)
3080 *a = handle;
3081 else if (!*b)
3082 *b = handle;
3083}
3084
3085/**
3086 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3087 * @ioc: per adapter object
3088 * @event_data: the event data payload
3089 * Context: interrupt time.
3090 *
3091 * This routine will send target reset to volume, followed by target
3092 * resets to the PDs. This is called when a PD has been removed, or
3093 * volume has been deleted or removed. When the target reset is sent
3094 * to volume, the PD target resets need to be queued to start upon
3095 * completion of the volume target reset.
3096 *
3097 * Return nothing.
3098 */
3099static void
3100_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3101 Mpi2EventDataIrConfigChangeList_t *event_data)
3102{
3103 Mpi2EventIrConfigElement_t *element;
3104 int i;
3105 u16 handle, volume_handle, a, b;
3106 struct _tr_list *delayed_tr;
3107
3108 a = 0;
3109 b = 0;
3110
3111 /* Volume Resets for Deleted or Removed */
3112 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3113 for (i = 0; i < event_data->NumElements; i++, element++) {
3114 if (element->ReasonCode ==
3115 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3116 element->ReasonCode ==
3117 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3118 volume_handle = le16_to_cpu(element->VolDevHandle);
3119 _scsih_set_volume_delete_flag(ioc, volume_handle);
3120 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3121 }
3122 }
3123
3124 /* Volume Resets for UNHIDE events */
3125 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3126 for (i = 0; i < event_data->NumElements; i++, element++) {
3127 if (le32_to_cpu(event_data->Flags) &
3128 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3129 continue;
3130 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3131 volume_handle = le16_to_cpu(element->VolDevHandle);
3132 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3133 }
3134 }
3135
3136 if (a)
3137 _scsih_tm_tr_volume_send(ioc, a);
3138 if (b)
3139 _scsih_tm_tr_volume_send(ioc, b);
3140
3141 /* PD target resets */
3142 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3143 for (i = 0; i < event_data->NumElements; i++, element++) {
3144 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3145 continue;
3146 handle = le16_to_cpu(element->PhysDiskDevHandle);
3147 volume_handle = le16_to_cpu(element->VolDevHandle);
3148 clear_bit(handle, ioc->pd_handles);
3149 if (!volume_handle)
3150 _scsih_tm_tr_send(ioc, handle);
3151 else if (volume_handle == a || volume_handle == b) {
3152 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3153 BUG_ON(!delayed_tr);
3154 INIT_LIST_HEAD(&delayed_tr->list);
3155 delayed_tr->handle = handle;
3156 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3157 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3158 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3159 handle));
3160 } else
3161 _scsih_tm_tr_send(ioc, handle);
3162 }
3163}
3164
3165
3166/**
3167 * _scsih_check_volume_delete_events - set delete flag for volumes
3168 * @ioc: per adapter object
3169 * @event_data: the event data payload
3170 * Context: interrupt time.
3171 *
3172 * This will handle the case when the cable connected to entire volume is
3173 * pulled. We will take care of setting the deleted flag so normal IO will
3174 * not be sent.
3175 *
3176 * Return nothing.
3177 */
3178static void
3179_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3180 Mpi2EventDataIrVolume_t *event_data)
3181{
3182 u32 state;
3183
3184 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3185 return;
3186 state = le32_to_cpu(event_data->NewValue);
3187 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3188 MPI2_RAID_VOL_STATE_FAILED)
3189 _scsih_set_volume_delete_flag(ioc,
3190 le16_to_cpu(event_data->VolDevHandle));
3191}
3192
3193/**
Eric Moore635374e2009-03-09 01:21:12 -06003194 * _scsih_flush_running_cmds - completing outstanding commands.
3195 * @ioc: per adapter object
3196 *
3197 * The flushing out of all pending scmd commands following host reset,
3198 * where all IO is dropped to the floor.
3199 *
3200 * Return nothing.
3201 */
3202static void
3203_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3204{
3205 struct scsi_cmnd *scmd;
3206 u16 smid;
3207 u16 count = 0;
3208
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303209 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3210 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003211 if (!scmd)
3212 continue;
3213 count++;
3214 mpt2sas_base_free_smid(ioc, smid);
3215 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003216 if (ioc->pci_error_recovery)
3217 scmd->result = DID_NO_CONNECT << 16;
3218 else
3219 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003220 scmd->scsi_done(scmd);
3221 }
3222 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3223 ioc->name, count));
3224}
3225
3226/**
Eric Moore3c621b32009-05-18 12:59:41 -06003227 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3228 * @scmd: pointer to scsi command object
3229 * @mpi_request: pointer to the SCSI_IO reqest message frame
3230 *
3231 * Supporting protection 1 and 3.
3232 *
3233 * Returns nothing
3234 */
3235static void
3236_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3237{
3238 u16 eedp_flags;
3239 unsigned char prot_op = scsi_get_prot_op(scmd);
3240 unsigned char prot_type = scsi_get_prot_type(scmd);
3241
Eric Moored334aa72010-04-22 10:47:40 -06003242 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003243 return;
3244
3245 if (prot_op == SCSI_PROT_READ_STRIP)
3246 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3247 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3248 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3249 else
3250 return;
3251
Eric Moore3c621b32009-05-18 12:59:41 -06003252 switch (prot_type) {
3253 case SCSI_PROT_DIF_TYPE1:
3254
3255 /*
3256 * enable ref/guard checking
3257 * auto increment ref tag
3258 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303259 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003260 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3261 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3262 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3263 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003264 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003265
Eric Moored334aa72010-04-22 10:47:40 -06003266 case SCSI_PROT_DIF_TYPE2:
3267
3268 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3269 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3270 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003271 break;
3272
3273 case SCSI_PROT_DIF_TYPE3:
3274
3275 /*
3276 * enable guard checking
3277 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303278 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003279 break;
3280 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303281 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3282 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003283}
3284
3285/**
3286 * _scsih_eedp_error_handling - return sense code for EEDP errors
3287 * @scmd: pointer to scsi command object
3288 * @ioc_status: ioc status
3289 *
3290 * Returns nothing
3291 */
3292static void
3293_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3294{
3295 u8 ascq;
3296 u8 sk;
3297 u8 host_byte;
3298
3299 switch (ioc_status) {
3300 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3301 ascq = 0x01;
3302 break;
3303 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3304 ascq = 0x02;
3305 break;
3306 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3307 ascq = 0x03;
3308 break;
3309 default:
3310 ascq = 0x00;
3311 break;
3312 }
3313
3314 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3315 sk = ILLEGAL_REQUEST;
3316 host_byte = DID_ABORT;
3317 } else {
3318 sk = ABORTED_COMMAND;
3319 host_byte = DID_OK;
3320 }
3321
3322 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3323 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3324 SAM_STAT_CHECK_CONDITION;
3325}
3326
3327/**
Eric Moored5d135b2009-05-18 13:02:08 -06003328 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003329 * @scmd: pointer to scsi command object
3330 * @done: function pointer to be invoked on completion
3331 *
3332 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3333 *
3334 * Returns 0 on success. If there's a failure, return either:
3335 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3336 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3337 */
3338static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003339_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003340{
3341 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3342 struct MPT2SAS_DEVICE *sas_device_priv_data;
3343 struct MPT2SAS_TARGET *sas_target_priv_data;
3344 Mpi2SCSIIORequest_t *mpi_request;
3345 u32 mpi_control;
3346 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003347
3348 scmd->scsi_done = done;
3349 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303350 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003351 scmd->result = DID_NO_CONNECT << 16;
3352 scmd->scsi_done(scmd);
3353 return 0;
3354 }
3355
Eric Moore3cb54692010-07-08 14:44:34 -06003356 if (ioc->pci_error_recovery) {
3357 scmd->result = DID_NO_CONNECT << 16;
3358 scmd->scsi_done(scmd);
3359 return 0;
3360 }
3361
Eric Moore635374e2009-03-09 01:21:12 -06003362 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303363 /* invalid device handle */
3364 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003365 scmd->result = DID_NO_CONNECT << 16;
3366 scmd->scsi_done(scmd);
3367 return 0;
3368 }
3369
Kashyap, Desai130b9582010-04-08 17:54:32 +05303370 /* host recovery or link resets sent via IOCTLs */
3371 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003372 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003373 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303374 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3375 return SCSI_MLQUEUE_DEVICE_BUSY;
3376 /* device has been deleted */
3377 else if (sas_target_priv_data->deleted) {
3378 scmd->result = DID_NO_CONNECT << 16;
3379 scmd->scsi_done(scmd);
3380 return 0;
3381 }
Eric Moore635374e2009-03-09 01:21:12 -06003382
3383 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3384 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3385 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3386 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3387 else
3388 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3389
3390 /* set tags */
3391 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3392 if (scmd->device->tagged_supported) {
3393 if (scmd->device->ordered_tags)
3394 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3395 else
3396 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3397 } else
3398/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3399/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3400 */
3401 mpi_control |= (0x500);
3402
3403 } else
3404 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303405 /* Make sure Device is not raid volume */
3406 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003407 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003408 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3409
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303410 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003411 if (!smid) {
3412 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3413 ioc->name, __func__);
3414 goto out;
3415 }
3416 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3417 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003418 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003419 if (scmd->cmd_len == 32)
3420 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003421 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3422 if (sas_device_priv_data->sas_target->flags &
3423 MPT_TARGET_FLAGS_RAID_COMPONENT)
3424 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3425 else
3426 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3427 mpi_request->DevHandle =
3428 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3429 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3430 mpi_request->Control = cpu_to_le32(mpi_control);
3431 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3432 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3433 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3434 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303435 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003436 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3437 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3438 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303439 mpi_request->VF_ID = 0; /* TODO */
3440 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003441 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3442 mpi_request->LUN);
3443 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3444
3445 if (!mpi_request->DataLength) {
3446 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3447 } else {
3448 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3449 mpt2sas_base_free_smid(ioc, smid);
3450 goto out;
3451 }
3452 }
3453
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303454 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3455 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3456 sas_device_priv_data->sas_target->handle);
3457 else
3458 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003459 return 0;
3460
3461 out:
3462 return SCSI_MLQUEUE_HOST_BUSY;
3463}
3464
Jeff Garzikf2812332010-11-16 02:10:29 -05003465static DEF_SCSI_QCMD(_scsih_qcmd)
3466
Eric Moore635374e2009-03-09 01:21:12 -06003467/**
3468 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3469 * @sense_buffer: sense data returned by target
3470 * @data: normalized skey/asc/ascq
3471 *
3472 * Return nothing.
3473 */
3474static void
3475_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3476{
3477 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3478 /* descriptor format */
3479 data->skey = sense_buffer[1] & 0x0F;
3480 data->asc = sense_buffer[2];
3481 data->ascq = sense_buffer[3];
3482 } else {
3483 /* fixed format */
3484 data->skey = sense_buffer[2] & 0x0F;
3485 data->asc = sense_buffer[12];
3486 data->ascq = sense_buffer[13];
3487 }
3488}
3489
3490#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3491/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003492 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003493 * @ioc: per adapter object
3494 * @scmd: pointer to scsi command object
3495 * @mpi_reply: reply mf payload returned from firmware
3496 *
3497 * scsi_status - SCSI Status code returned from target device
3498 * scsi_state - state info associated with SCSI_IO determined by ioc
3499 * ioc_status - ioc supplied status info
3500 *
3501 * Return nothing.
3502 */
3503static void
3504_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3505 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3506{
3507 u32 response_info;
3508 u8 *response_bytes;
3509 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3510 MPI2_IOCSTATUS_MASK;
3511 u8 scsi_state = mpi_reply->SCSIState;
3512 u8 scsi_status = mpi_reply->SCSIStatus;
3513 char *desc_ioc_state = NULL;
3514 char *desc_scsi_status = NULL;
3515 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303516 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303517 struct _sas_device *sas_device = NULL;
3518 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303519 struct scsi_target *starget = scmd->device->sdev_target;
3520 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3521
3522 if (!priv_target)
3523 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303524
3525 if (log_info == 0x31170000)
3526 return;
Eric Moore635374e2009-03-09 01:21:12 -06003527
3528 switch (ioc_status) {
3529 case MPI2_IOCSTATUS_SUCCESS:
3530 desc_ioc_state = "success";
3531 break;
3532 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3533 desc_ioc_state = "invalid function";
3534 break;
3535 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3536 desc_ioc_state = "scsi recovered error";
3537 break;
3538 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3539 desc_ioc_state = "scsi invalid dev handle";
3540 break;
3541 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3542 desc_ioc_state = "scsi device not there";
3543 break;
3544 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3545 desc_ioc_state = "scsi data overrun";
3546 break;
3547 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3548 desc_ioc_state = "scsi data underrun";
3549 break;
3550 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3551 desc_ioc_state = "scsi io data error";
3552 break;
3553 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3554 desc_ioc_state = "scsi protocol error";
3555 break;
3556 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3557 desc_ioc_state = "scsi task terminated";
3558 break;
3559 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3560 desc_ioc_state = "scsi residual mismatch";
3561 break;
3562 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3563 desc_ioc_state = "scsi task mgmt failed";
3564 break;
3565 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3566 desc_ioc_state = "scsi ioc terminated";
3567 break;
3568 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3569 desc_ioc_state = "scsi ext terminated";
3570 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003571 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3572 desc_ioc_state = "eedp guard error";
3573 break;
3574 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3575 desc_ioc_state = "eedp ref tag error";
3576 break;
3577 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3578 desc_ioc_state = "eedp app tag error";
3579 break;
Eric Moore635374e2009-03-09 01:21:12 -06003580 default:
3581 desc_ioc_state = "unknown";
3582 break;
3583 }
3584
3585 switch (scsi_status) {
3586 case MPI2_SCSI_STATUS_GOOD:
3587 desc_scsi_status = "good";
3588 break;
3589 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3590 desc_scsi_status = "check condition";
3591 break;
3592 case MPI2_SCSI_STATUS_CONDITION_MET:
3593 desc_scsi_status = "condition met";
3594 break;
3595 case MPI2_SCSI_STATUS_BUSY:
3596 desc_scsi_status = "busy";
3597 break;
3598 case MPI2_SCSI_STATUS_INTERMEDIATE:
3599 desc_scsi_status = "intermediate";
3600 break;
3601 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3602 desc_scsi_status = "intermediate condmet";
3603 break;
3604 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3605 desc_scsi_status = "reservation conflict";
3606 break;
3607 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3608 desc_scsi_status = "command terminated";
3609 break;
3610 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3611 desc_scsi_status = "task set full";
3612 break;
3613 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3614 desc_scsi_status = "aca active";
3615 break;
3616 case MPI2_SCSI_STATUS_TASK_ABORTED:
3617 desc_scsi_status = "task aborted";
3618 break;
3619 default:
3620 desc_scsi_status = "unknown";
3621 break;
3622 }
3623
3624 desc_scsi_state[0] = '\0';
3625 if (!scsi_state)
3626 desc_scsi_state = " ";
3627 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3628 strcat(desc_scsi_state, "response info ");
3629 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3630 strcat(desc_scsi_state, "state terminated ");
3631 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3632 strcat(desc_scsi_state, "no status ");
3633 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3634 strcat(desc_scsi_state, "autosense failed ");
3635 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3636 strcat(desc_scsi_state, "autosense valid ");
3637
3638 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303639
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303640 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3641 printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
3642 (unsigned long long)priv_target->sas_address);
3643 } else {
3644 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3645 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3646 priv_target->sas_address);
3647 if (sas_device) {
3648 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
3649 "phy(%d)\n", ioc->name, sas_device->sas_address,
3650 sas_device->phy);
3651 printk(MPT2SAS_WARN_FMT
3652 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
3653 ioc->name, sas_device->enclosure_logical_id,
3654 sas_device->slot);
3655 }
3656 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303657 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303658
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303659 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
3660 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3661 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003662 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3663 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3664 scsi_get_resid(scmd));
3665 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3666 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3667 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3668 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3669 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3670 scsi_status, desc_scsi_state, scsi_state);
3671
3672 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3673 struct sense_info data;
3674 _scsih_normalize_sense(scmd->sense_buffer, &data);
3675 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303676 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3677 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06003678 }
3679
3680 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3681 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3682 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303683 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003684 }
3685}
3686#endif
3687
3688/**
3689 * _scsih_smart_predicted_fault - illuminate Fault LED
3690 * @ioc: per adapter object
3691 * @handle: device handle
3692 *
3693 * Return nothing.
3694 */
3695static void
3696_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3697{
3698 Mpi2SepReply_t mpi_reply;
3699 Mpi2SepRequest_t mpi_request;
3700 struct scsi_target *starget;
3701 struct MPT2SAS_TARGET *sas_target_priv_data;
3702 Mpi2EventNotificationReply_t *event_reply;
3703 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3704 struct _sas_device *sas_device;
3705 ssize_t sz;
3706 unsigned long flags;
3707
3708 /* only handle non-raid devices */
3709 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3710 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3711 if (!sas_device) {
3712 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3713 return;
3714 }
3715 starget = sas_device->starget;
3716 sas_target_priv_data = starget->hostdata;
3717
3718 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3719 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3720 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3721 return;
3722 }
3723 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3724 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3725
3726 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3727 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3728 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3729 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3730 mpi_request.SlotStatus =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303731 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
Eric Moore635374e2009-03-09 01:21:12 -06003732 mpi_request.DevHandle = cpu_to_le16(handle);
3733 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3734 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3735 &mpi_request)) != 0) {
3736 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3737 ioc->name, __FILE__, __LINE__, __func__);
3738 return;
3739 }
3740
3741 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3742 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3743 "enclosure_processor: ioc_status (0x%04x), "
3744 "loginfo(0x%08x)\n", ioc->name,
3745 le16_to_cpu(mpi_reply.IOCStatus),
3746 le32_to_cpu(mpi_reply.IOCLogInfo)));
3747 return;
3748 }
3749 }
3750
3751 /* insert into event log */
3752 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3753 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3754 event_reply = kzalloc(sz, GFP_KERNEL);
3755 if (!event_reply) {
3756 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3757 ioc->name, __FILE__, __LINE__, __func__);
3758 return;
3759 }
3760
3761 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3762 event_reply->Event =
3763 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3764 event_reply->MsgLength = sz/4;
3765 event_reply->EventDataLength =
3766 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3767 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3768 event_reply->EventData;
3769 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3770 event_data->ASC = 0x5D;
3771 event_data->DevHandle = cpu_to_le16(handle);
3772 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3773 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3774 kfree(event_reply);
3775}
3776
3777/**
Eric Moored5d135b2009-05-18 13:02:08 -06003778 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003779 * @ioc: per adapter object
3780 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303781 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003782 * @reply: reply message frame(lower 32bit addr)
3783 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303784 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003785 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303786 * Return 1 meaning mf should be freed from _base_interrupt
3787 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003788 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303789static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303790_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003791{
3792 Mpi2SCSIIORequest_t *mpi_request;
3793 Mpi2SCSIIOReply_t *mpi_reply;
3794 struct scsi_cmnd *scmd;
3795 u16 ioc_status;
3796 u32 xfer_cnt;
3797 u8 scsi_state;
3798 u8 scsi_status;
3799 u32 log_info;
3800 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303801 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003802
3803 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303804 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003805 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303806 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003807
3808 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3809
3810 if (mpi_reply == NULL) {
3811 scmd->result = DID_OK << 16;
3812 goto out;
3813 }
3814
3815 sas_device_priv_data = scmd->device->hostdata;
3816 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3817 sas_device_priv_data->sas_target->deleted) {
3818 scmd->result = DID_NO_CONNECT << 16;
3819 goto out;
3820 }
3821
3822 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303823 scsi_state = mpi_reply->SCSIState;
3824 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3825 response_code =
3826 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003827 if (!sas_device_priv_data->tlr_snoop_check) {
3828 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303829 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3830 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303831 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3832 sas_disable_tlr(scmd->device);
3833 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3834 }
Eric Moore635374e2009-03-09 01:21:12 -06003835 }
3836
3837 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3838 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3839 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3840 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3841 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3842 else
3843 log_info = 0;
3844 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003845 scsi_status = mpi_reply->SCSIStatus;
3846
3847 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3848 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3849 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3850 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3851 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3852 }
3853
3854 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3855 struct sense_info data;
3856 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3857 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003858 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003859 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003860 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003861 _scsih_normalize_sense(scmd->sense_buffer, &data);
3862 /* failure prediction threshold exceeded */
3863 if (data.asc == 0x5D)
3864 _scsih_smart_predicted_fault(ioc,
3865 le16_to_cpu(mpi_reply->DevHandle));
3866 }
3867
3868 switch (ioc_status) {
3869 case MPI2_IOCSTATUS_BUSY:
3870 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3871 scmd->result = SAM_STAT_BUSY;
3872 break;
3873
3874 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3875 scmd->result = DID_NO_CONNECT << 16;
3876 break;
3877
3878 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3879 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303880 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3881 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003882 }
Eric Moore635374e2009-03-09 01:21:12 -06003883 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3884 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3885 scmd->result = DID_RESET << 16;
3886 break;
3887
3888 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3889 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3890 scmd->result = DID_SOFT_ERROR << 16;
3891 else
3892 scmd->result = (DID_OK << 16) | scsi_status;
3893 break;
3894
3895 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3896 scmd->result = (DID_OK << 16) | scsi_status;
3897
3898 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3899 break;
3900
3901 if (xfer_cnt < scmd->underflow) {
3902 if (scsi_status == SAM_STAT_BUSY)
3903 scmd->result = SAM_STAT_BUSY;
3904 else
3905 scmd->result = DID_SOFT_ERROR << 16;
3906 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3907 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3908 scmd->result = DID_SOFT_ERROR << 16;
3909 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3910 scmd->result = DID_RESET << 16;
3911 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3912 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3913 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3914 scmd->result = (DRIVER_SENSE << 24) |
3915 SAM_STAT_CHECK_CONDITION;
3916 scmd->sense_buffer[0] = 0x70;
3917 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3918 scmd->sense_buffer[12] = 0x20;
3919 scmd->sense_buffer[13] = 0;
3920 }
3921 break;
3922
3923 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3924 scsi_set_resid(scmd, 0);
3925 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3926 case MPI2_IOCSTATUS_SUCCESS:
3927 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303928 if (response_code ==
3929 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3930 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3931 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003932 scmd->result = DID_SOFT_ERROR << 16;
3933 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3934 scmd->result = DID_RESET << 16;
3935 break;
3936
Eric Moore3c621b32009-05-18 12:59:41 -06003937 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3938 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3939 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3940 _scsih_eedp_error_handling(scmd, ioc_status);
3941 break;
Eric Moore635374e2009-03-09 01:21:12 -06003942 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3943 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3944 case MPI2_IOCSTATUS_INVALID_SGL:
3945 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3946 case MPI2_IOCSTATUS_INVALID_FIELD:
3947 case MPI2_IOCSTATUS_INVALID_STATE:
3948 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3949 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3950 default:
3951 scmd->result = DID_SOFT_ERROR << 16;
3952 break;
3953
3954 }
3955
3956#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3957 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3958 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3959#endif
3960
3961 out:
3962 scsi_dma_unmap(scmd);
3963 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303964 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003965}
3966
3967/**
Eric Moore635374e2009-03-09 01:21:12 -06003968 * _scsih_sas_host_refresh - refreshing sas host object contents
3969 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003970 * Context: user
3971 *
3972 * During port enable, fw will send topology events for every device. Its
3973 * possible that the handles may change from the previous setting, so this
3974 * code keeping handles updating if changed.
3975 *
3976 * Return nothing.
3977 */
3978static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303979_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003980{
3981 u16 sz;
3982 u16 ioc_status;
3983 int i;
3984 Mpi2ConfigReply_t mpi_reply;
3985 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303986 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303987 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06003988
3989 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3990 "updating handles for sas_host(0x%016llx)\n",
3991 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3992
3993 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3994 * sizeof(Mpi2SasIOUnit0PhyData_t));
3995 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3996 if (!sas_iounit_pg0) {
3997 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3998 ioc->name, __FILE__, __LINE__, __func__);
3999 return;
4000 }
Eric Moore635374e2009-03-09 01:21:12 -06004001
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304002 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4003 sas_iounit_pg0, sz)) != 0)
4004 goto out;
4005 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4006 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4007 goto out;
4008 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304009 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304010 if (i == 0)
4011 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4012 PhyData[0].ControllerDevHandle);
4013 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4014 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4015 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304016 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4017 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304018 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304019 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304020 }
Eric Moore635374e2009-03-09 01:21:12 -06004021 out:
4022 kfree(sas_iounit_pg0);
4023}
4024
4025/**
4026 * _scsih_sas_host_add - create sas host object
4027 * @ioc: per adapter object
4028 *
4029 * Creating host side data object, stored in ioc->sas_hba
4030 *
4031 * Return nothing.
4032 */
4033static void
4034_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4035{
4036 int i;
4037 Mpi2ConfigReply_t mpi_reply;
4038 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4039 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4040 Mpi2SasPhyPage0_t phy_pg0;
4041 Mpi2SasDevicePage0_t sas_device_pg0;
4042 Mpi2SasEnclosurePage0_t enclosure_pg0;
4043 u16 ioc_status;
4044 u16 sz;
4045 u16 device_missing_delay;
4046
4047 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4048 if (!ioc->sas_hba.num_phys) {
4049 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4050 ioc->name, __FILE__, __LINE__, __func__);
4051 return;
4052 }
4053
4054 /* sas_iounit page 0 */
4055 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4056 sizeof(Mpi2SasIOUnit0PhyData_t));
4057 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4058 if (!sas_iounit_pg0) {
4059 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4060 ioc->name, __FILE__, __LINE__, __func__);
4061 return;
4062 }
4063 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4064 sas_iounit_pg0, sz))) {
4065 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4066 ioc->name, __FILE__, __LINE__, __func__);
4067 goto out;
4068 }
4069 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4070 MPI2_IOCSTATUS_MASK;
4071 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4072 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4073 ioc->name, __FILE__, __LINE__, __func__);
4074 goto out;
4075 }
4076
4077 /* sas_iounit page 1 */
4078 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4079 sizeof(Mpi2SasIOUnit1PhyData_t));
4080 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4081 if (!sas_iounit_pg1) {
4082 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4083 ioc->name, __FILE__, __LINE__, __func__);
4084 goto out;
4085 }
4086 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4087 sas_iounit_pg1, sz))) {
4088 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4089 ioc->name, __FILE__, __LINE__, __func__);
4090 goto out;
4091 }
4092 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4093 MPI2_IOCSTATUS_MASK;
4094 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4095 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4096 ioc->name, __FILE__, __LINE__, __func__);
4097 goto out;
4098 }
4099
4100 ioc->io_missing_delay =
4101 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4102 device_missing_delay =
4103 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4104 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4105 ioc->device_missing_delay = (device_missing_delay &
4106 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4107 else
4108 ioc->device_missing_delay = device_missing_delay &
4109 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4110
4111 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4112 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4113 sizeof(struct _sas_phy), GFP_KERNEL);
4114 if (!ioc->sas_hba.phy) {
4115 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4116 ioc->name, __FILE__, __LINE__, __func__);
4117 goto out;
4118 }
4119 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4120 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4121 i))) {
4122 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4123 ioc->name, __FILE__, __LINE__, __func__);
4124 goto out;
4125 }
4126 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4127 MPI2_IOCSTATUS_MASK;
4128 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4129 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4130 ioc->name, __FILE__, __LINE__, __func__);
4131 goto out;
4132 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304133
4134 if (i == 0)
4135 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4136 PhyData[0].ControllerDevHandle);
4137 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004138 ioc->sas_hba.phy[i].phy_id = i;
4139 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4140 phy_pg0, ioc->sas_hba.parent_dev);
4141 }
4142 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304143 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004144 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4145 ioc->name, __FILE__, __LINE__, __func__);
4146 goto out;
4147 }
Eric Moore635374e2009-03-09 01:21:12 -06004148 ioc->sas_hba.enclosure_handle =
4149 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4150 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4151 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4152 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4153 (unsigned long long) ioc->sas_hba.sas_address,
4154 ioc->sas_hba.num_phys) ;
4155
4156 if (ioc->sas_hba.enclosure_handle) {
4157 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4158 &enclosure_pg0,
4159 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4160 ioc->sas_hba.enclosure_handle))) {
4161 ioc->sas_hba.enclosure_logical_id =
4162 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4163 }
4164 }
4165
4166 out:
4167 kfree(sas_iounit_pg1);
4168 kfree(sas_iounit_pg0);
4169}
4170
4171/**
4172 * _scsih_expander_add - creating expander object
4173 * @ioc: per adapter object
4174 * @handle: expander handle
4175 *
4176 * Creating expander object, stored in ioc->sas_expander_list.
4177 *
4178 * Return 0 for success, else error.
4179 */
4180static int
4181_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4182{
4183 struct _sas_node *sas_expander;
4184 Mpi2ConfigReply_t mpi_reply;
4185 Mpi2ExpanderPage0_t expander_pg0;
4186 Mpi2ExpanderPage1_t expander_pg1;
4187 Mpi2SasEnclosurePage0_t enclosure_pg0;
4188 u32 ioc_status;
4189 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304190 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004191 int i;
4192 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304193 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004194 int rc = 0;
4195
4196 if (!handle)
4197 return -1;
4198
Eric Moore3cb54692010-07-08 14:44:34 -06004199 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304200 return -1;
4201
Eric Moore635374e2009-03-09 01:21:12 -06004202 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4203 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4204 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4205 ioc->name, __FILE__, __LINE__, __func__);
4206 return -1;
4207 }
4208
4209 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4210 MPI2_IOCSTATUS_MASK;
4211 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4212 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4213 ioc->name, __FILE__, __LINE__, __func__);
4214 return -1;
4215 }
4216
4217 /* handle out of order topology events */
4218 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304219 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4220 != 0) {
4221 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4222 ioc->name, __FILE__, __LINE__, __func__);
4223 return -1;
4224 }
4225 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004226 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304227 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4228 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004229 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4230 if (!sas_expander) {
4231 rc = _scsih_expander_add(ioc, parent_handle);
4232 if (rc != 0)
4233 return rc;
4234 }
4235 }
4236
Eric Moore635374e2009-03-09 01:21:12 -06004237 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304238 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004239 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4240 sas_address);
4241 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4242
4243 if (sas_expander)
4244 return 0;
4245
4246 sas_expander = kzalloc(sizeof(struct _sas_node),
4247 GFP_KERNEL);
4248 if (!sas_expander) {
4249 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4250 ioc->name, __FILE__, __LINE__, __func__);
4251 return -1;
4252 }
4253
4254 sas_expander->handle = handle;
4255 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304256 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004257 sas_expander->sas_address = sas_address;
4258
4259 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4260 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304261 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004262 sas_expander->sas_address, sas_expander->num_phys);
4263
4264 if (!sas_expander->num_phys)
4265 goto out_fail;
4266 sas_expander->phy = kcalloc(sas_expander->num_phys,
4267 sizeof(struct _sas_phy), GFP_KERNEL);
4268 if (!sas_expander->phy) {
4269 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4270 ioc->name, __FILE__, __LINE__, __func__);
4271 rc = -1;
4272 goto out_fail;
4273 }
4274
4275 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4276 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304277 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004278 if (!mpt2sas_port) {
4279 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4280 ioc->name, __FILE__, __LINE__, __func__);
4281 rc = -1;
4282 goto out_fail;
4283 }
4284 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4285
4286 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4287 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4288 &expander_pg1, i, handle))) {
4289 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4290 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304291 rc = -1;
4292 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004293 }
4294 sas_expander->phy[i].handle = handle;
4295 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304296
4297 if ((mpt2sas_transport_add_expander_phy(ioc,
4298 &sas_expander->phy[i], expander_pg1,
4299 sas_expander->parent_dev))) {
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 }
Eric Moore635374e2009-03-09 01:21:12 -06004305 }
4306
4307 if (sas_expander->enclosure_handle) {
4308 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4309 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4310 sas_expander->enclosure_handle))) {
4311 sas_expander->enclosure_logical_id =
4312 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4313 }
4314 }
4315
4316 _scsih_expander_node_add(ioc, sas_expander);
4317 return 0;
4318
4319 out_fail:
4320
Kashyap, Desai20f58952009-08-07 19:34:26 +05304321 if (mpt2sas_port)
4322 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304323 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004324 kfree(sas_expander);
4325 return rc;
4326}
4327
4328/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304329 * _scsih_done - scsih callback handler.
4330 * @ioc: per adapter object
4331 * @smid: system request message index
4332 * @msix_index: MSIX table index supplied by the OS
4333 * @reply: reply message frame(lower 32bit addr)
4334 *
4335 * Callback handler when sending internal generated message frames.
4336 * The callback index passed is `ioc->scsih_cb_idx`
4337 *
4338 * Return 1 meaning mf should be freed from _base_interrupt
4339 * 0 means the mf is freed from this function.
4340 */
4341static u8
4342_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4343{
4344 MPI2DefaultReply_t *mpi_reply;
4345
4346 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4347 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4348 return 1;
4349 if (ioc->scsih_cmds.smid != smid)
4350 return 1;
4351 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4352 if (mpi_reply) {
4353 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4354 mpi_reply->MsgLength*4);
4355 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4356 }
4357 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4358 complete(&ioc->scsih_cmds.done);
4359 return 1;
4360}
4361
4362/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304363 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004364 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304365 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004366 *
4367 * Return nothing.
4368 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304369void
4370mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004371{
4372 struct _sas_node *sas_expander;
4373 unsigned long flags;
4374
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304375 if (ioc->shost_recovery)
4376 return;
4377
Eric Moore635374e2009-03-09 01:21:12 -06004378 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304379 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4380 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304381 if (!sas_expander) {
4382 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4383 return;
4384 }
4385 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004386 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4387 _scsih_expander_node_remove(ioc, sas_expander);
4388}
4389
4390/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304391 * _scsih_check_access_status - check access flags
4392 * @ioc: per adapter object
4393 * @sas_address: sas address
4394 * @handle: sas device handle
4395 * @access_flags: errors returned during discovery of the device
4396 *
4397 * Return 0 for success, else failure
4398 */
4399static u8
4400_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4401 u16 handle, u8 access_status)
4402{
4403 u8 rc = 1;
4404 char *desc = NULL;
4405
4406 switch (access_status) {
4407 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4408 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4409 rc = 0;
4410 break;
4411 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4412 desc = "sata capability failed";
4413 break;
4414 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4415 desc = "sata affiliation conflict";
4416 break;
4417 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4418 desc = "route not addressable";
4419 break;
4420 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4421 desc = "smp error not addressable";
4422 break;
4423 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4424 desc = "device blocked";
4425 break;
4426 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4427 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4428 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4429 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4430 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4431 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4432 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4433 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4434 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4435 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4436 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4437 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4438 desc = "sata initialization failed";
4439 break;
4440 default:
4441 desc = "unknown";
4442 break;
4443 }
4444
4445 if (!rc)
4446 return 0;
4447
4448 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4449 "handle(0x%04x)\n", ioc->name, desc,
4450 (unsigned long long)sas_address, handle);
4451 return rc;
4452}
4453
4454static void
4455_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4456{
4457 Mpi2ConfigReply_t mpi_reply;
4458 Mpi2SasDevicePage0_t sas_device_pg0;
4459 struct _sas_device *sas_device;
4460 u32 ioc_status;
4461 unsigned long flags;
4462 u64 sas_address;
4463 struct scsi_target *starget;
4464 struct MPT2SAS_TARGET *sas_target_priv_data;
4465 u32 device_info;
4466
4467 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4468 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4469 return;
4470
4471 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4472 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4473 return;
4474
4475 /* check if this is end device */
4476 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4477 if (!(_scsih_is_end_device(device_info)))
4478 return;
4479
4480 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4481 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4482 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4483 sas_address);
4484
4485 if (!sas_device) {
4486 printk(MPT2SAS_ERR_FMT "device is not present "
4487 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4488 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4489 return;
4490 }
4491
4492 if (unlikely(sas_device->handle != handle)) {
4493 starget = sas_device->starget;
4494 sas_target_priv_data = starget->hostdata;
4495 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4496 " to (0x%04x)!!!\n", sas_device->handle, handle);
4497 sas_target_priv_data->handle = handle;
4498 sas_device->handle = handle;
4499 }
4500 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4501
4502 /* check if device is present */
4503 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4504 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4505 printk(MPT2SAS_ERR_FMT "device is not present "
4506 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4507 return;
4508 }
4509
4510 /* check if there were any issues with discovery */
4511 if (_scsih_check_access_status(ioc, sas_address, handle,
4512 sas_device_pg0.AccessStatus))
4513 return;
4514 _scsih_ublock_io_device(ioc, handle);
4515
4516}
4517
4518/**
Eric Moore635374e2009-03-09 01:21:12 -06004519 * _scsih_add_device - creating sas device object
4520 * @ioc: per adapter object
4521 * @handle: sas device handle
4522 * @phy_num: phy number end device attached to
4523 * @is_pd: is this hidden raid component
4524 *
4525 * Creating end device object, stored in ioc->sas_device_list.
4526 *
4527 * Returns 0 for success, non-zero for failure.
4528 */
4529static int
4530_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4531{
4532 Mpi2ConfigReply_t mpi_reply;
4533 Mpi2SasDevicePage0_t sas_device_pg0;
4534 Mpi2SasEnclosurePage0_t enclosure_pg0;
4535 struct _sas_device *sas_device;
4536 u32 ioc_status;
4537 __le64 sas_address;
4538 u32 device_info;
4539 unsigned long flags;
4540
4541 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4542 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4543 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4544 ioc->name, __FILE__, __LINE__, __func__);
4545 return -1;
4546 }
4547
4548 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4549 MPI2_IOCSTATUS_MASK;
4550 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4551 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4552 ioc->name, __FILE__, __LINE__, __func__);
4553 return -1;
4554 }
4555
Kashyap, Desaib4344272010-03-17 16:24:14 +05304556 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4557
Eric Moore635374e2009-03-09 01:21:12 -06004558 /* check if device is present */
4559 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4560 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4561 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4562 ioc->name, __FILE__, __LINE__, __func__);
4563 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4564 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4565 return -1;
4566 }
4567
Kashyap, Desaib4344272010-03-17 16:24:14 +05304568 /* check if there were any issues with discovery */
4569 if (_scsih_check_access_status(ioc, sas_address, handle,
4570 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06004571 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06004572
4573 /* check if this is end device */
4574 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4575 if (!(_scsih_is_end_device(device_info))) {
4576 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4577 ioc->name, __FILE__, __LINE__, __func__);
4578 return -1;
4579 }
4580
Eric Moore635374e2009-03-09 01:21:12 -06004581
4582 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4583 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4584 sas_address);
4585 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4586
Kashyap, Desaib4344272010-03-17 16:24:14 +05304587 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004588 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06004589
4590 sas_device = kzalloc(sizeof(struct _sas_device),
4591 GFP_KERNEL);
4592 if (!sas_device) {
4593 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4594 ioc->name, __FILE__, __LINE__, __func__);
4595 return -1;
4596 }
4597
4598 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304599 if (_scsih_get_sas_address(ioc, le16_to_cpu
4600 (sas_device_pg0.ParentDevHandle),
4601 &sas_device->sas_address_parent) != 0)
4602 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4603 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004604 sas_device->enclosure_handle =
4605 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4606 sas_device->slot =
4607 le16_to_cpu(sas_device_pg0.Slot);
4608 sas_device->device_info = device_info;
4609 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304610 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06004611
4612 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304613 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4614 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4615 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004616 sas_device->enclosure_logical_id =
4617 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004618
4619 /* get device name */
4620 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4621
4622 if (ioc->wait_for_port_enable_to_complete)
4623 _scsih_sas_device_init_add(ioc, sas_device);
4624 else
4625 _scsih_sas_device_add(ioc, sas_device);
4626
4627 return 0;
4628}
4629
4630/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05304631 * _scsih_remove_device - removing sas device object
4632 * @ioc: per adapter object
4633 * @sas_device_delete: the sas_device object
4634 *
4635 * Return nothing.
4636 */
4637static void
4638_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4639 struct _sas_device *sas_device)
4640{
4641 struct _sas_device sas_device_backup;
4642 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304643
Kashyap, Desai1278b112010-03-09 17:34:13 +05304644 if (!sas_device)
4645 return;
Eric Moore635374e2009-03-09 01:21:12 -06004646
Kashyap, Desai1278b112010-03-09 17:34:13 +05304647 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06004648 _scsih_sas_device_remove(ioc, sas_device);
4649
Kashyap, Desai1278b112010-03-09 17:34:13 +05304650 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4651 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4652 sas_device_backup.handle, (unsigned long long)
4653 sas_device_backup.sas_address));
4654
4655 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4656 sas_target_priv_data = sas_device_backup.starget->hostdata;
4657 sas_target_priv_data->deleted = 1;
4658 }
4659
Kashyap, Desai1278b112010-03-09 17:34:13 +05304660 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4661
4662 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4663 sas_device_backup.sas_address_parent);
4664
4665 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4666 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4667 (unsigned long long) sas_device_backup.sas_address);
4668
4669 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4670 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4671 sas_device_backup.handle, (unsigned long long)
4672 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004673}
4674
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304675/**
4676 * mpt2sas_device_remove - removing device object
4677 * @ioc: per adapter object
4678 * @sas_address: expander sas_address
4679 *
4680 * Return nothing.
4681 */
4682void
4683mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
4684{
4685 struct _sas_device *sas_device;
4686 unsigned long flags;
4687
4688 if (ioc->shost_recovery)
4689 return;
4690
4691 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4692 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4693 sas_address);
4694 if (!sas_device) {
4695 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4696 return;
4697 }
4698 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4699 _scsih_remove_device(ioc, sas_device);
4700}
4701
Eric Moore635374e2009-03-09 01:21:12 -06004702#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4703/**
4704 * _scsih_sas_topology_change_event_debug - debug for topology event
4705 * @ioc: per adapter object
4706 * @event_data: event data payload
4707 * Context: user.
4708 */
4709static void
4710_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4711 Mpi2EventDataSasTopologyChangeList_t *event_data)
4712{
4713 int i;
4714 u16 handle;
4715 u16 reason_code;
4716 u8 phy_number;
4717 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304718 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004719
4720 switch (event_data->ExpStatus) {
4721 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4722 status_str = "add";
4723 break;
4724 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4725 status_str = "remove";
4726 break;
4727 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304728 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004729 status_str = "responding";
4730 break;
4731 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4732 status_str = "remove delay";
4733 break;
4734 default:
4735 status_str = "unknown status";
4736 break;
4737 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304738 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06004739 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304740 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06004741 "start_phy(%02d), count(%d)\n",
4742 le16_to_cpu(event_data->ExpanderDevHandle),
4743 le16_to_cpu(event_data->EnclosureHandle),
4744 event_data->StartPhyNum, event_data->NumEntries);
4745 for (i = 0; i < event_data->NumEntries; i++) {
4746 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4747 if (!handle)
4748 continue;
4749 phy_number = event_data->StartPhyNum + i;
4750 reason_code = event_data->PHY[i].PhyStatus &
4751 MPI2_EVENT_SAS_TOPO_RC_MASK;
4752 switch (reason_code) {
4753 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304754 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004755 break;
4756 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304757 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004758 break;
4759 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304760 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004761 break;
4762 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304763 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004764 break;
4765 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304766 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004767 break;
4768 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304769 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004770 break;
4771 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304772 link_rate = event_data->PHY[i].LinkRate >> 4;
4773 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304774 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304775 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4776 handle, status_str, link_rate, prev_link_rate);
4777
Eric Moore635374e2009-03-09 01:21:12 -06004778 }
4779}
4780#endif
4781
4782/**
4783 * _scsih_sas_topology_change_event - handle topology changes
4784 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304785 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004786 * Context: user.
4787 *
4788 */
4789static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304790_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004791 struct fw_event_work *fw_event)
4792{
4793 int i;
4794 u16 parent_handle, handle;
4795 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304796 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06004797 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304798 struct _sas_device *sas_device;
4799 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004800 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304801 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304802 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004803
4804#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4805 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4806 _scsih_sas_topology_change_event_debug(ioc, event_data);
4807#endif
4808
Eric Moore3cb54692010-07-08 14:44:34 -06004809 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304810 return;
4811
Eric Moore635374e2009-03-09 01:21:12 -06004812 if (!ioc->sas_hba.num_phys)
4813 _scsih_sas_host_add(ioc);
4814 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304815 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004816
4817 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304818 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06004819 "event\n", ioc->name));
4820 return;
4821 }
4822
4823 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4824
4825 /* handle expander add */
4826 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4827 if (_scsih_expander_add(ioc, parent_handle) != 0)
4828 return;
4829
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304830 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4831 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4832 parent_handle);
4833 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304834 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304835 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304836 max_phys = sas_expander->num_phys;
4837 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304838 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304839 max_phys = ioc->sas_hba.num_phys;
4840 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304841 return;
4842
Eric Moore635374e2009-03-09 01:21:12 -06004843 /* handle siblings events */
4844 for (i = 0; i < event_data->NumEntries; i++) {
4845 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304846 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06004847 "expander event\n", ioc->name));
4848 return;
4849 }
Eric Moore3cb54692010-07-08 14:44:34 -06004850 if (ioc->shost_recovery || ioc->remove_host ||
4851 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304852 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304853 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05304854 if (phy_number >= max_phys)
4855 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304856 reason_code = event_data->PHY[i].PhyStatus &
4857 MPI2_EVENT_SAS_TOPO_RC_MASK;
4858 if ((event_data->PHY[i].PhyStatus &
4859 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4860 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004861 continue;
4862 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4863 if (!handle)
4864 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304865 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304866 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004867 switch (reason_code) {
4868 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304869
4870 if (link_rate == prev_link_rate)
4871 break;
4872
4873 mpt2sas_transport_update_links(ioc, sas_address,
4874 handle, phy_number, link_rate);
4875
Kashyap, Desaib4344272010-03-17 16:24:14 +05304876 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4877 break;
4878
4879 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304880 break;
Eric Moore635374e2009-03-09 01:21:12 -06004881 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304882
4883 mpt2sas_transport_update_links(ioc, sas_address,
4884 handle, phy_number, link_rate);
4885
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304886 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004887 break;
4888 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304889
4890 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4891 sas_device = _scsih_sas_device_find_by_handle(ioc,
4892 handle);
4893 if (!sas_device) {
4894 spin_unlock_irqrestore(&ioc->sas_device_lock,
4895 flags);
4896 break;
4897 }
4898 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4899 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004900 break;
4901 }
4902 }
4903
4904 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304905 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4906 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304907 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004908
4909}
4910
4911#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4912/**
4913 * _scsih_sas_device_status_change_event_debug - debug for device event
4914 * @event_data: event data payload
4915 * Context: user.
4916 *
4917 * Return nothing.
4918 */
4919static void
4920_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4921 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4922{
4923 char *reason_str = NULL;
4924
4925 switch (event_data->ReasonCode) {
4926 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4927 reason_str = "smart data";
4928 break;
4929 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4930 reason_str = "unsupported device discovered";
4931 break;
4932 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4933 reason_str = "internal device reset";
4934 break;
4935 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4936 reason_str = "internal task abort";
4937 break;
4938 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4939 reason_str = "internal task abort set";
4940 break;
4941 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4942 reason_str = "internal clear task set";
4943 break;
4944 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4945 reason_str = "internal query task";
4946 break;
4947 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4948 reason_str = "sata init failure";
4949 break;
4950 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4951 reason_str = "internal device reset complete";
4952 break;
4953 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4954 reason_str = "internal task abort complete";
4955 break;
4956 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4957 reason_str = "internal async notification";
4958 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304959 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4960 reason_str = "expander reduced functionality";
4961 break;
4962 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4963 reason_str = "expander reduced functionality complete";
4964 break;
Eric Moore635374e2009-03-09 01:21:12 -06004965 default:
4966 reason_str = "unknown reason";
4967 break;
4968 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304969 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06004970 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4971 reason_str, le16_to_cpu(event_data->DevHandle),
4972 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4973 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304974 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004975 event_data->ASC, event_data->ASCQ);
4976 printk(KERN_INFO "\n");
4977}
4978#endif
4979
4980/**
4981 * _scsih_sas_device_status_change_event - handle device status change
4982 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304983 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004984 * Context: user.
4985 *
4986 * Return nothing.
4987 */
4988static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304989_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4990 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004991{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304992 struct MPT2SAS_TARGET *target_priv_data;
4993 struct _sas_device *sas_device;
4994 __le64 sas_address;
4995 unsigned long flags;
4996 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4997 fw_event->event_data;
4998
Eric Moore635374e2009-03-09 01:21:12 -06004999#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5000 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305001 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305002 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005003#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305004
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305005 /* In MPI Revision K (0xC), the internal device reset complete was
5006 * implemented, so avoid setting tm_busy flag for older firmware.
5007 */
5008 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5009 return;
5010
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305011 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305012 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305013 event_data->ReasonCode !=
5014 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305015 return;
5016
5017 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5018 sas_address = le64_to_cpu(event_data->SASAddress);
5019 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5020 sas_address);
5021 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5022
5023 if (!sas_device || !sas_device->starget)
5024 return;
5025
5026 target_priv_data = sas_device->starget->hostdata;
5027 if (!target_priv_data)
5028 return;
5029
5030 if (event_data->ReasonCode ==
5031 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5032 target_priv_data->tm_busy = 1;
5033 else
5034 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005035}
5036
5037#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5038/**
5039 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5040 * @ioc: per adapter object
5041 * @event_data: event data payload
5042 * Context: user.
5043 *
5044 * Return nothing.
5045 */
5046static void
5047_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5048 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5049{
5050 char *reason_str = NULL;
5051
5052 switch (event_data->ReasonCode) {
5053 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5054 reason_str = "enclosure add";
5055 break;
5056 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5057 reason_str = "enclosure remove";
5058 break;
5059 default:
5060 reason_str = "unknown reason";
5061 break;
5062 }
5063
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305064 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005065 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5066 " number slots(%d)\n", ioc->name, reason_str,
5067 le16_to_cpu(event_data->EnclosureHandle),
5068 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5069 le16_to_cpu(event_data->StartSlot));
5070}
5071#endif
5072
5073/**
5074 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5075 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305076 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005077 * Context: user.
5078 *
5079 * Return nothing.
5080 */
5081static void
5082_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305083 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005084{
5085#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5086 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5087 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305088 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005089#endif
5090}
5091
5092/**
5093 * _scsih_sas_broadcast_primative_event - handle broadcast events
5094 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305095 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005096 * Context: user.
5097 *
5098 * Return nothing.
5099 */
5100static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305101_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5102 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005103{
5104 struct scsi_cmnd *scmd;
5105 u16 smid, handle;
5106 u32 lun;
5107 struct MPT2SAS_DEVICE *sas_device_priv_data;
5108 u32 termination_count;
5109 u32 query_count;
5110 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305111#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5112 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5113#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05305114 u16 ioc_status;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305115 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
Eric Moore635374e2009-03-09 01:21:12 -06005116 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5117 event_data->PortWidth));
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305118 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005119 __func__));
5120
Eric Moore635374e2009-03-09 01:21:12 -06005121 termination_count = 0;
5122 query_count = 0;
5123 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305124 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06005125 scmd = _scsih_scsi_lookup_get(ioc, smid);
5126 if (!scmd)
5127 continue;
5128 sas_device_priv_data = scmd->device->hostdata;
5129 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5130 continue;
5131 /* skip hidden raid components */
5132 if (sas_device_priv_data->sas_target->flags &
5133 MPT_TARGET_FLAGS_RAID_COMPONENT)
5134 continue;
5135 /* skip volumes */
5136 if (sas_device_priv_data->sas_target->flags &
5137 MPT_TARGET_FLAGS_VOLUME)
5138 continue;
5139
5140 handle = sas_device_priv_data->sas_target->handle;
5141 lun = sas_device_priv_data->lun;
5142 query_count++;
5143
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305144 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5145 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
Eric Moore8901cbb2009-04-21 15:41:32 -06005146 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305147 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5148 & MPI2_IOCSTATUS_MASK;
5149 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06005150 (mpi_reply->ResponseCode ==
5151 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5152 mpi_reply->ResponseCode ==
5153 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
5154 continue;
5155
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305156 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5157 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30, NULL);
Eric Moore635374e2009-03-09 01:21:12 -06005158 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5159 }
Eric Moore635374e2009-03-09 01:21:12 -06005160 ioc->broadcast_aen_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005161
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305162 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005163 "%s - exit, query_count = %d termination_count = %d\n",
5164 ioc->name, __func__, query_count, termination_count));
5165}
5166
5167/**
5168 * _scsih_sas_discovery_event - handle discovery events
5169 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305170 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005171 * Context: user.
5172 *
5173 * Return nothing.
5174 */
5175static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305176_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5177 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005178{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305179 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5180
Eric Moore635374e2009-03-09 01:21:12 -06005181#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5182 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305183 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005184 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5185 "start" : "stop");
5186 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305187 printk("discovery_status(0x%08x)",
5188 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005189 printk("\n");
5190 }
5191#endif
5192
5193 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5194 !ioc->sas_hba.num_phys)
5195 _scsih_sas_host_add(ioc);
5196}
5197
5198/**
5199 * _scsih_reprobe_lun - reprobing lun
5200 * @sdev: scsi device struct
5201 * @no_uld_attach: sdev->no_uld_attach flag setting
5202 *
5203 **/
5204static void
5205_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5206{
5207 int rc;
5208
5209 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5210 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5211 sdev->no_uld_attach ? "hidding" : "exposing");
5212 rc = scsi_device_reprobe(sdev);
5213}
5214
5215/**
5216 * _scsih_reprobe_target - reprobing target
5217 * @starget: scsi target struct
5218 * @no_uld_attach: sdev->no_uld_attach flag setting
5219 *
5220 * Note: no_uld_attach flag determines whether the disk device is attached
5221 * to block layer. A value of `1` means to not attach.
5222 **/
5223static void
5224_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5225{
5226 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5227
5228 if (no_uld_attach)
5229 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5230 else
5231 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5232
5233 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5234 _scsih_reprobe_lun);
5235}
5236/**
5237 * _scsih_sas_volume_add - add new volume
5238 * @ioc: per adapter object
5239 * @element: IR config element data
5240 * Context: user.
5241 *
5242 * Return nothing.
5243 */
5244static void
5245_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5246 Mpi2EventIrConfigElement_t *element)
5247{
5248 struct _raid_device *raid_device;
5249 unsigned long flags;
5250 u64 wwid;
5251 u16 handle = le16_to_cpu(element->VolDevHandle);
5252 int rc;
5253
Eric Moore635374e2009-03-09 01:21:12 -06005254 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5255 if (!wwid) {
5256 printk(MPT2SAS_ERR_FMT
5257 "failure at %s:%d/%s()!\n", ioc->name,
5258 __FILE__, __LINE__, __func__);
5259 return;
5260 }
5261
5262 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5263 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5264 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5265
5266 if (raid_device)
5267 return;
5268
5269 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5270 if (!raid_device) {
5271 printk(MPT2SAS_ERR_FMT
5272 "failure at %s:%d/%s()!\n", ioc->name,
5273 __FILE__, __LINE__, __func__);
5274 return;
5275 }
5276
5277 raid_device->id = ioc->sas_id++;
5278 raid_device->channel = RAID_CHANNEL;
5279 raid_device->handle = handle;
5280 raid_device->wwid = wwid;
5281 _scsih_raid_device_add(ioc, raid_device);
5282 if (!ioc->wait_for_port_enable_to_complete) {
5283 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5284 raid_device->id, 0);
5285 if (rc)
5286 _scsih_raid_device_remove(ioc, raid_device);
5287 } else
5288 _scsih_determine_boot_device(ioc, raid_device, 1);
5289}
5290
5291/**
5292 * _scsih_sas_volume_delete - delete volume
5293 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305294 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005295 * Context: user.
5296 *
5297 * Return nothing.
5298 */
5299static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305300_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005301{
5302 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06005303 unsigned long flags;
5304 struct MPT2SAS_TARGET *sas_target_priv_data;
5305
Eric Moore635374e2009-03-09 01:21:12 -06005306 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5307 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5308 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5309 if (!raid_device)
5310 return;
5311 if (raid_device->starget) {
5312 sas_target_priv_data = raid_device->starget->hostdata;
5313 sas_target_priv_data->deleted = 1;
5314 scsi_remove_target(&raid_device->starget->dev);
5315 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305316 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5317 "(0x%016llx)\n", ioc->name, raid_device->handle,
5318 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06005319 _scsih_raid_device_remove(ioc, raid_device);
5320}
5321
5322/**
5323 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5324 * @ioc: per adapter object
5325 * @element: IR config element data
5326 * Context: user.
5327 *
5328 * Return nothing.
5329 */
5330static void
5331_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5332 Mpi2EventIrConfigElement_t *element)
5333{
5334 struct _sas_device *sas_device;
5335 unsigned long flags;
5336 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5337
5338 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5339 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5340 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5341 if (!sas_device)
5342 return;
5343
5344 /* exposing raid component */
5345 sas_device->volume_handle = 0;
5346 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305347 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005348 _scsih_reprobe_target(sas_device->starget, 0);
5349}
5350
5351/**
5352 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5353 * @ioc: per adapter object
5354 * @element: IR config element data
5355 * Context: user.
5356 *
5357 * Return nothing.
5358 */
5359static void
5360_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5361 Mpi2EventIrConfigElement_t *element)
5362{
5363 struct _sas_device *sas_device;
5364 unsigned long flags;
5365 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5366
5367 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5368 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5369 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5370 if (!sas_device)
5371 return;
5372
5373 /* hiding raid component */
5374 mpt2sas_config_get_volume_handle(ioc, handle,
5375 &sas_device->volume_handle);
5376 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5377 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305378 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005379 _scsih_reprobe_target(sas_device->starget, 1);
5380}
5381
5382/**
5383 * _scsih_sas_pd_delete - delete pd component
5384 * @ioc: per adapter object
5385 * @element: IR config element data
5386 * Context: user.
5387 *
5388 * Return nothing.
5389 */
5390static void
5391_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5392 Mpi2EventIrConfigElement_t *element)
5393{
5394 struct _sas_device *sas_device;
5395 unsigned long flags;
5396 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5397
5398 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5399 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5400 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5401 if (!sas_device)
5402 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305403 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005404}
5405
5406/**
5407 * _scsih_sas_pd_add - remove pd component
5408 * @ioc: per adapter object
5409 * @element: IR config element data
5410 * Context: user.
5411 *
5412 * Return nothing.
5413 */
5414static void
5415_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5416 Mpi2EventIrConfigElement_t *element)
5417{
5418 struct _sas_device *sas_device;
5419 unsigned long flags;
5420 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305421 Mpi2ConfigReply_t mpi_reply;
5422 Mpi2SasDevicePage0_t sas_device_pg0;
5423 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305424 u64 sas_address;
5425 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005426
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305427 set_bit(handle, ioc->pd_handles);
5428
Eric Moore635374e2009-03-09 01:21:12 -06005429 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5430 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5431 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305432 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305433 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305434
5435 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5436 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5437 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5438 ioc->name, __FILE__, __LINE__, __func__);
5439 return;
5440 }
5441
5442 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5443 MPI2_IOCSTATUS_MASK;
5444 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5445 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5446 ioc->name, __FILE__, __LINE__, __func__);
5447 return;
5448 }
5449
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305450 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5451 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5452 mpt2sas_transport_update_links(ioc, sas_address, handle,
5453 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305454
5455 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06005456}
5457
5458#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5459/**
5460 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5461 * @ioc: per adapter object
5462 * @event_data: event data payload
5463 * Context: user.
5464 *
5465 * Return nothing.
5466 */
5467static void
5468_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5469 Mpi2EventDataIrConfigChangeList_t *event_data)
5470{
5471 Mpi2EventIrConfigElement_t *element;
5472 u8 element_type;
5473 int i;
5474 char *reason_str = NULL, *element_str = NULL;
5475
5476 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5477
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305478 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005479 ioc->name, (le32_to_cpu(event_data->Flags) &
5480 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5481 "foreign" : "native", event_data->NumElements);
5482 for (i = 0; i < event_data->NumElements; i++, element++) {
5483 switch (element->ReasonCode) {
5484 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5485 reason_str = "add";
5486 break;
5487 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5488 reason_str = "remove";
5489 break;
5490 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5491 reason_str = "no change";
5492 break;
5493 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5494 reason_str = "hide";
5495 break;
5496 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5497 reason_str = "unhide";
5498 break;
5499 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5500 reason_str = "volume_created";
5501 break;
5502 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5503 reason_str = "volume_deleted";
5504 break;
5505 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5506 reason_str = "pd_created";
5507 break;
5508 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5509 reason_str = "pd_deleted";
5510 break;
5511 default:
5512 reason_str = "unknown reason";
5513 break;
5514 }
5515 element_type = le16_to_cpu(element->ElementFlags) &
5516 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5517 switch (element_type) {
5518 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5519 element_str = "volume";
5520 break;
5521 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5522 element_str = "phys disk";
5523 break;
5524 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5525 element_str = "hot spare";
5526 break;
5527 default:
5528 element_str = "unknown element";
5529 break;
5530 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305531 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005532 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5533 reason_str, le16_to_cpu(element->VolDevHandle),
5534 le16_to_cpu(element->PhysDiskDevHandle),
5535 element->PhysDiskNum);
5536 }
5537}
5538#endif
5539
5540/**
5541 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5542 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305543 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005544 * Context: user.
5545 *
5546 * Return nothing.
5547 */
5548static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305549_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5550 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005551{
5552 Mpi2EventIrConfigElement_t *element;
5553 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305554 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305555 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005556
5557#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5558 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5559 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5560
5561#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305562 foreign_config = (le32_to_cpu(event_data->Flags) &
5563 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005564
5565 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5566 for (i = 0; i < event_data->NumElements; i++, element++) {
5567
5568 switch (element->ReasonCode) {
5569 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5570 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305571 if (!foreign_config)
5572 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005573 break;
5574 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5575 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305576 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305577 _scsih_sas_volume_delete(ioc,
5578 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06005579 break;
5580 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5581 _scsih_sas_pd_hide(ioc, element);
5582 break;
5583 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5584 _scsih_sas_pd_expose(ioc, element);
5585 break;
5586 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5587 _scsih_sas_pd_add(ioc, element);
5588 break;
5589 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5590 _scsih_sas_pd_delete(ioc, element);
5591 break;
5592 }
5593 }
5594}
5595
5596/**
5597 * _scsih_sas_ir_volume_event - IR volume event
5598 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305599 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005600 * Context: user.
5601 *
5602 * Return nothing.
5603 */
5604static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305605_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5606 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005607{
5608 u64 wwid;
5609 unsigned long flags;
5610 struct _raid_device *raid_device;
5611 u16 handle;
5612 u32 state;
5613 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305614 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005615
5616 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5617 return;
5618
5619 handle = le16_to_cpu(event_data->VolDevHandle);
5620 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305621 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005622 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5623 le32_to_cpu(event_data->PreviousValue), state));
5624
Eric Moore635374e2009-03-09 01:21:12 -06005625 switch (state) {
5626 case MPI2_RAID_VOL_STATE_MISSING:
5627 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305628 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005629 break;
5630
5631 case MPI2_RAID_VOL_STATE_ONLINE:
5632 case MPI2_RAID_VOL_STATE_DEGRADED:
5633 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305634
5635 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5636 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5637 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5638
Eric Moore635374e2009-03-09 01:21:12 -06005639 if (raid_device)
5640 break;
5641
5642 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5643 if (!wwid) {
5644 printk(MPT2SAS_ERR_FMT
5645 "failure at %s:%d/%s()!\n", ioc->name,
5646 __FILE__, __LINE__, __func__);
5647 break;
5648 }
5649
5650 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5651 if (!raid_device) {
5652 printk(MPT2SAS_ERR_FMT
5653 "failure at %s:%d/%s()!\n", ioc->name,
5654 __FILE__, __LINE__, __func__);
5655 break;
5656 }
5657
5658 raid_device->id = ioc->sas_id++;
5659 raid_device->channel = RAID_CHANNEL;
5660 raid_device->handle = handle;
5661 raid_device->wwid = wwid;
5662 _scsih_raid_device_add(ioc, raid_device);
5663 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5664 raid_device->id, 0);
5665 if (rc)
5666 _scsih_raid_device_remove(ioc, raid_device);
5667 break;
5668
5669 case MPI2_RAID_VOL_STATE_INITIALIZING:
5670 default:
5671 break;
5672 }
5673}
5674
5675/**
5676 * _scsih_sas_ir_physical_disk_event - PD event
5677 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305678 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005679 * Context: user.
5680 *
5681 * Return nothing.
5682 */
5683static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305684_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5685 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005686{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305687 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005688 u32 state;
5689 struct _sas_device *sas_device;
5690 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305691 Mpi2ConfigReply_t mpi_reply;
5692 Mpi2SasDevicePage0_t sas_device_pg0;
5693 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305694 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305695 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005696
5697 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5698 return;
5699
5700 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5701 state = le32_to_cpu(event_data->NewValue);
5702
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305703 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005704 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5705 le32_to_cpu(event_data->PreviousValue), state));
5706
Eric Moore635374e2009-03-09 01:21:12 -06005707 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005708 case MPI2_RAID_PD_STATE_ONLINE:
5709 case MPI2_RAID_PD_STATE_DEGRADED:
5710 case MPI2_RAID_PD_STATE_REBUILDING:
5711 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305712 case MPI2_RAID_PD_STATE_HOT_SPARE:
5713
5714 set_bit(handle, ioc->pd_handles);
5715
5716 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5717 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5718 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5719
5720 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305721 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305722
5723 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5724 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5725 handle))) {
5726 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5727 ioc->name, __FILE__, __LINE__, __func__);
5728 return;
5729 }
5730
5731 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5732 MPI2_IOCSTATUS_MASK;
5733 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5734 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5735 ioc->name, __FILE__, __LINE__, __func__);
5736 return;
5737 }
5738
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305739 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5740 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5741 mpt2sas_transport_update_links(ioc, sas_address, handle,
5742 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305743
5744 _scsih_add_device(ioc, handle, 0, 1);
5745
Eric Moore635374e2009-03-09 01:21:12 -06005746 break;
5747
Kashyap, Desai62727a72009-08-07 19:35:18 +05305748 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005749 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5750 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06005751 default:
5752 break;
5753 }
5754}
5755
5756#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5757/**
5758 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5759 * @ioc: per adapter object
5760 * @event_data: event data payload
5761 * Context: user.
5762 *
5763 * Return nothing.
5764 */
5765static void
5766_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5767 Mpi2EventDataIrOperationStatus_t *event_data)
5768{
5769 char *reason_str = NULL;
5770
5771 switch (event_data->RAIDOperation) {
5772 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5773 reason_str = "resync";
5774 break;
5775 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5776 reason_str = "online capacity expansion";
5777 break;
5778 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5779 reason_str = "consistency check";
5780 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305781 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5782 reason_str = "background init";
5783 break;
5784 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5785 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005786 break;
5787 }
5788
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305789 if (!reason_str)
5790 return;
5791
Eric Moore635374e2009-03-09 01:21:12 -06005792 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5793 "\thandle(0x%04x), percent complete(%d)\n",
5794 ioc->name, reason_str,
5795 le16_to_cpu(event_data->VolDevHandle),
5796 event_data->PercentComplete);
5797}
5798#endif
5799
5800/**
5801 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5802 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305803 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005804 * Context: user.
5805 *
5806 * Return nothing.
5807 */
5808static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305809_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5810 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005811{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305812 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5813 static struct _raid_device *raid_device;
5814 unsigned long flags;
5815 u16 handle;
5816
Eric Moore635374e2009-03-09 01:21:12 -06005817#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5818 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305819 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305820 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005821#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305822
5823 /* code added for raid transport support */
5824 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5825
5826 handle = le16_to_cpu(event_data->VolDevHandle);
5827
5828 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5829 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5830 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5831
5832 if (!raid_device)
5833 return;
5834
5835 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5836 raid_device->percent_complete =
5837 event_data->PercentComplete;
5838 }
Eric Moore635374e2009-03-09 01:21:12 -06005839}
5840
5841/**
Kashyap, Desai14695852010-03-30 10:52:44 +05305842 * _scsih_prep_device_scan - initialize parameters prior to device scan
5843 * @ioc: per adapter object
5844 *
5845 * Set the deleted flag prior to device scan. If the device is found during
5846 * the scan, then we clear the deleted flag.
5847 */
5848static void
5849_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5850{
5851 struct MPT2SAS_DEVICE *sas_device_priv_data;
5852 struct scsi_device *sdev;
5853
5854 shost_for_each_device(sdev, ioc->shost) {
5855 sas_device_priv_data = sdev->hostdata;
5856 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5857 sas_device_priv_data->sas_target->deleted = 1;
5858 }
5859}
5860
5861/**
Eric Moore635374e2009-03-09 01:21:12 -06005862 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5863 * @ioc: per adapter object
5864 * @sas_address: sas address
5865 * @slot: enclosure slot id
5866 * @handle: device handle
5867 *
5868 * After host reset, find out whether devices are still responding.
5869 * Used in _scsi_remove_unresponsive_sas_devices.
5870 *
5871 * Return nothing.
5872 */
5873static void
5874_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5875 u16 slot, u16 handle)
5876{
5877 struct MPT2SAS_TARGET *sas_target_priv_data;
5878 struct scsi_target *starget;
5879 struct _sas_device *sas_device;
5880 unsigned long flags;
5881
5882 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5883 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5884 if (sas_device->sas_address == sas_address &&
5885 sas_device->slot == slot && sas_device->starget) {
5886 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305887 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05305888 if (starget && starget->hostdata) {
5889 sas_target_priv_data = starget->hostdata;
5890 sas_target_priv_data->tm_busy = 0;
5891 sas_target_priv_data->deleted = 0;
5892 } else
5893 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005894 starget_printk(KERN_INFO, sas_device->starget,
5895 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5896 "logical id(0x%016llx), slot(%d)\n", handle,
5897 (unsigned long long)sas_device->sas_address,
5898 (unsigned long long)
5899 sas_device->enclosure_logical_id,
5900 sas_device->slot);
5901 if (sas_device->handle == handle)
5902 goto out;
5903 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5904 sas_device->handle);
5905 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305906 if (sas_target_priv_data)
5907 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005908 goto out;
5909 }
5910 }
5911 out:
5912 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5913}
5914
5915/**
5916 * _scsih_search_responding_sas_devices -
5917 * @ioc: per adapter object
5918 *
5919 * After host reset, find out whether devices are still responding.
5920 * If not remove.
5921 *
5922 * Return nothing.
5923 */
5924static void
5925_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5926{
5927 Mpi2SasDevicePage0_t sas_device_pg0;
5928 Mpi2ConfigReply_t mpi_reply;
5929 u16 ioc_status;
5930 __le64 sas_address;
5931 u16 handle;
5932 u32 device_info;
5933 u16 slot;
5934
5935 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5936
5937 if (list_empty(&ioc->sas_device_list))
5938 return;
5939
5940 handle = 0xFFFF;
5941 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5942 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5943 handle))) {
5944 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5945 MPI2_IOCSTATUS_MASK;
5946 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5947 break;
5948 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5949 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5950 if (!(_scsih_is_end_device(device_info)))
5951 continue;
5952 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5953 slot = le16_to_cpu(sas_device_pg0.Slot);
5954 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5955 handle);
5956 }
5957}
5958
5959/**
5960 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5961 * @ioc: per adapter object
5962 * @wwid: world wide identifier for raid volume
5963 * @handle: device handle
5964 *
5965 * After host reset, find out whether devices are still responding.
5966 * Used in _scsi_remove_unresponsive_raid_devices.
5967 *
5968 * Return nothing.
5969 */
5970static void
5971_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5972 u16 handle)
5973{
5974 struct MPT2SAS_TARGET *sas_target_priv_data;
5975 struct scsi_target *starget;
5976 struct _raid_device *raid_device;
5977 unsigned long flags;
5978
5979 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5980 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5981 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05305982 starget = raid_device->starget;
5983 if (starget && starget->hostdata) {
5984 sas_target_priv_data = starget->hostdata;
5985 sas_target_priv_data->deleted = 0;
5986 } else
5987 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005988 raid_device->responding = 1;
5989 starget_printk(KERN_INFO, raid_device->starget,
5990 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5991 (unsigned long long)raid_device->wwid);
5992 if (raid_device->handle == handle)
5993 goto out;
5994 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5995 raid_device->handle);
5996 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305997 if (sas_target_priv_data)
5998 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005999 goto out;
6000 }
6001 }
6002 out:
6003 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6004}
6005
6006/**
6007 * _scsih_search_responding_raid_devices -
6008 * @ioc: per adapter object
6009 *
6010 * After host reset, find out whether devices are still responding.
6011 * If not remove.
6012 *
6013 * Return nothing.
6014 */
6015static void
6016_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6017{
6018 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306019 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306020 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006021 Mpi2ConfigReply_t mpi_reply;
6022 u16 ioc_status;
6023 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306024 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006025
6026 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6027
6028 if (list_empty(&ioc->raid_device_list))
6029 return;
6030
6031 handle = 0xFFFF;
6032 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6033 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6034 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6035 MPI2_IOCSTATUS_MASK;
6036 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6037 break;
6038 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306039
6040 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6041 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6042 sizeof(Mpi2RaidVolPage0_t)))
6043 continue;
6044
6045 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6046 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6047 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6048 _scsih_mark_responding_raid_device(ioc,
6049 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006050 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306051
6052 /* refresh the pd_handles */
6053 phys_disk_num = 0xFF;
6054 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6055 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6056 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6057 phys_disk_num))) {
6058 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6059 MPI2_IOCSTATUS_MASK;
6060 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6061 break;
6062 phys_disk_num = pd_pg0.PhysDiskNum;
6063 handle = le16_to_cpu(pd_pg0.DevHandle);
6064 set_bit(handle, ioc->pd_handles);
6065 }
Eric Moore635374e2009-03-09 01:21:12 -06006066}
6067
6068/**
6069 * _scsih_mark_responding_expander - mark a expander as responding
6070 * @ioc: per adapter object
6071 * @sas_address: sas address
6072 * @handle:
6073 *
6074 * After host reset, find out whether devices are still responding.
6075 * Used in _scsi_remove_unresponsive_expanders.
6076 *
6077 * Return nothing.
6078 */
6079static void
6080_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6081 u16 handle)
6082{
6083 struct _sas_node *sas_expander;
6084 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306085 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006086
6087 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6088 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306089 if (sas_expander->sas_address != sas_address)
6090 continue;
6091 sas_expander->responding = 1;
6092 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006093 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306094 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6095 " from(0x%04x) to (0x%04x)!!!\n",
6096 (unsigned long long)sas_expander->sas_address,
6097 sas_expander->handle, handle);
6098 sas_expander->handle = handle;
6099 for (i = 0 ; i < sas_expander->num_phys ; i++)
6100 sas_expander->phy[i].handle = handle;
6101 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006102 }
6103 out:
6104 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6105}
6106
6107/**
6108 * _scsih_search_responding_expanders -
6109 * @ioc: per adapter object
6110 *
6111 * After host reset, find out whether devices are still responding.
6112 * If not remove.
6113 *
6114 * Return nothing.
6115 */
6116static void
6117_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6118{
6119 Mpi2ExpanderPage0_t expander_pg0;
6120 Mpi2ConfigReply_t mpi_reply;
6121 u16 ioc_status;
6122 __le64 sas_address;
6123 u16 handle;
6124
6125 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6126
6127 if (list_empty(&ioc->sas_expander_list))
6128 return;
6129
6130 handle = 0xFFFF;
6131 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6132 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6133
6134 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6135 MPI2_IOCSTATUS_MASK;
6136 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6137 break;
6138
6139 handle = le16_to_cpu(expander_pg0.DevHandle);
6140 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6141 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6142 "sas_addr(0x%016llx)\n", handle,
6143 (unsigned long long)sas_address);
6144 _scsih_mark_responding_expander(ioc, sas_address, handle);
6145 }
6146
6147}
6148
6149/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306150 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006151 * @ioc: per adapter object
6152 *
6153 * Return nothing.
6154 */
6155static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306156_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006157{
6158 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306159 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006160 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006161
Eric Moore635374e2009-03-09 01:21:12 -06006162
6163 list_for_each_entry_safe(sas_device, sas_device_next,
6164 &ioc->sas_device_list, list) {
6165 if (sas_device->responding) {
6166 sas_device->responding = 0;
6167 continue;
6168 }
6169 if (sas_device->starget)
6170 starget_printk(KERN_INFO, sas_device->starget,
6171 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6172 "enclosure logical id(0x%016llx), slot(%d)\n",
6173 sas_device->handle,
6174 (unsigned long long)sas_device->sas_address,
6175 (unsigned long long)
6176 sas_device->enclosure_logical_id,
6177 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306178 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006179 }
6180
6181 list_for_each_entry_safe(raid_device, raid_device_next,
6182 &ioc->raid_device_list, list) {
6183 if (raid_device->responding) {
6184 raid_device->responding = 0;
6185 continue;
6186 }
6187 if (raid_device->starget) {
6188 starget_printk(KERN_INFO, raid_device->starget,
6189 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6190 raid_device->handle,
6191 (unsigned long long)raid_device->wwid);
6192 scsi_remove_target(&raid_device->starget->dev);
6193 }
6194 _scsih_raid_device_remove(ioc, raid_device);
6195 }
6196
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306197 retry_expander_search:
6198 sas_expander = NULL;
6199 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006200 if (sas_expander->responding) {
6201 sas_expander->responding = 0;
6202 continue;
6203 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306204 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306205 goto retry_expander_search;
6206 }
6207}
6208
6209/**
6210 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6211 * @ioc: per adapter object
6212 * @reset_phase: phase
6213 *
6214 * The handler for doing any required cleanup or initialization.
6215 *
6216 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6217 * MPT2_IOC_DONE_RESET
6218 *
6219 * Return nothing.
6220 */
6221void
6222mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6223{
6224 switch (reset_phase) {
6225 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306226 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306227 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306228 break;
6229 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306230 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306231 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306232 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6233 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6234 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6235 complete(&ioc->scsih_cmds.done);
6236 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306237 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6238 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6239 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6240 complete(&ioc->tm_cmds.done);
6241 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306242 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306243 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306244 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306245 break;
6246 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306247 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306248 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306249 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05306250 _scsih_prep_device_scan(ioc);
6251 _scsih_search_responding_sas_devices(ioc);
6252 _scsih_search_responding_raid_devices(ioc);
6253 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306254 break;
Eric Moore635374e2009-03-09 01:21:12 -06006255 }
6256}
6257
6258/**
6259 * _firmware_event_work - delayed task for processing firmware events
6260 * @ioc: per adapter object
6261 * @work: equal to the fw_event_work object
6262 * Context: user.
6263 *
6264 * Return nothing.
6265 */
6266static void
6267_firmware_event_work(struct work_struct *work)
6268{
6269 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306270 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06006271 unsigned long flags;
6272 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6273
Eric Moore635374e2009-03-09 01:21:12 -06006274 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06006275 if (ioc->remove_host || fw_event->cancel_pending_work ||
6276 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06006277 _scsih_fw_event_free(ioc, fw_event);
6278 return;
6279 }
Eric Moore635374e2009-03-09 01:21:12 -06006280
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306281 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6282 _scsih_fw_event_free(ioc, fw_event);
6283 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6284 if (ioc->shost_recovery) {
6285 init_completion(&ioc->shost_recovery_done);
6286 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6287 flags);
6288 wait_for_completion(&ioc->shost_recovery_done);
6289 } else
6290 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6291 flags);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306292 _scsih_remove_unresponding_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006293 return;
6294 }
Eric Moore635374e2009-03-09 01:21:12 -06006295
6296 switch (fw_event->event) {
6297 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306298 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006299 break;
6300 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306301 _scsih_sas_device_status_change_event(ioc,
6302 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006303 break;
6304 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306305 _scsih_sas_discovery_event(ioc,
6306 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006307 break;
6308 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306309 _scsih_sas_broadcast_primative_event(ioc,
6310 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006311 break;
6312 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6313 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306314 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006315 break;
6316 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306317 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006318 break;
6319 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306320 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006321 break;
6322 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306323 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006324 break;
6325 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306326 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006327 break;
Eric Moore635374e2009-03-09 01:21:12 -06006328 }
6329 _scsih_fw_event_free(ioc, fw_event);
6330}
6331
6332/**
6333 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6334 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306335 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06006336 * @reply: reply message frame(lower 32bit addr)
6337 * Context: interrupt.
6338 *
6339 * This function merely adds a new work task into ioc->firmware_event_thread.
6340 * The tasks are worked from _firmware_event_work in user context.
6341 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306342 * Return 1 meaning mf should be freed from _base_interrupt
6343 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006344 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306345u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306346mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6347 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006348{
6349 struct fw_event_work *fw_event;
6350 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006351 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306352 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006353
6354 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06006355 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306356 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006357
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306358 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006359 event = le16_to_cpu(mpi_reply->Event);
6360
6361 switch (event) {
6362 /* handle these */
6363 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6364 {
6365 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6366 (Mpi2EventDataSasBroadcastPrimitive_t *)
6367 mpi_reply->EventData;
6368
6369 if (baen_data->Primitive !=
6370 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6371 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306372 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006373 ioc->broadcast_aen_busy = 1;
6374 break;
6375 }
6376
6377 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6378 _scsih_check_topo_delete_events(ioc,
6379 (Mpi2EventDataSasTopologyChangeList_t *)
6380 mpi_reply->EventData);
6381 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306382 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6383 _scsih_check_ir_config_unhide_events(ioc,
6384 (Mpi2EventDataIrConfigChangeList_t *)
6385 mpi_reply->EventData);
6386 break;
6387 case MPI2_EVENT_IR_VOLUME:
6388 _scsih_check_volume_delete_events(ioc,
6389 (Mpi2EventDataIrVolume_t *)
6390 mpi_reply->EventData);
6391 break;
Eric Moore635374e2009-03-09 01:21:12 -06006392 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6393 case MPI2_EVENT_IR_OPERATION_STATUS:
6394 case MPI2_EVENT_SAS_DISCOVERY:
6395 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06006396 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06006397 break;
6398
6399 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306400 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006401 }
6402
6403 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6404 if (!fw_event) {
6405 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6406 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306407 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006408 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306409 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6410 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06006411 if (!fw_event->event_data) {
6412 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6413 ioc->name, __FILE__, __LINE__, __func__);
6414 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306415 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006416 }
6417
6418 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306419 sz);
Eric Moore635374e2009-03-09 01:21:12 -06006420 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306421 fw_event->VF_ID = mpi_reply->VF_ID;
6422 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06006423 fw_event->event = event;
6424 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306425 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006426}
6427
6428/* shost template */
6429static struct scsi_host_template scsih_driver_template = {
6430 .module = THIS_MODULE,
6431 .name = "Fusion MPT SAS Host",
6432 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06006433 .queuecommand = _scsih_qcmd,
6434 .target_alloc = _scsih_target_alloc,
6435 .slave_alloc = _scsih_slave_alloc,
6436 .slave_configure = _scsih_slave_configure,
6437 .target_destroy = _scsih_target_destroy,
6438 .slave_destroy = _scsih_slave_destroy,
6439 .change_queue_depth = _scsih_change_queue_depth,
6440 .change_queue_type = _scsih_change_queue_type,
6441 .eh_abort_handler = _scsih_abort,
6442 .eh_device_reset_handler = _scsih_dev_reset,
6443 .eh_target_reset_handler = _scsih_target_reset,
6444 .eh_host_reset_handler = _scsih_host_reset,
6445 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06006446 .can_queue = 1,
6447 .this_id = -1,
6448 .sg_tablesize = MPT2SAS_SG_DEPTH,
6449 .max_sectors = 8192,
6450 .cmd_per_lun = 7,
6451 .use_clustering = ENABLE_CLUSTERING,
6452 .shost_attrs = mpt2sas_host_attrs,
6453 .sdev_attrs = mpt2sas_dev_attrs,
6454};
6455
6456/**
6457 * _scsih_expander_node_remove - removing expander device from list.
6458 * @ioc: per adapter object
6459 * @sas_expander: the sas_device object
6460 * Context: Calling function should acquire ioc->sas_node_lock.
6461 *
6462 * Removing object and freeing associated memory from the
6463 * ioc->sas_expander_list.
6464 *
6465 * Return nothing.
6466 */
6467static void
6468_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6469 struct _sas_node *sas_expander)
6470{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306471 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06006472
6473 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306474 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06006475 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306476 if (ioc->shost_recovery)
6477 return;
Eric Moore635374e2009-03-09 01:21:12 -06006478 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306479 SAS_END_DEVICE)
6480 mpt2sas_device_remove(ioc,
6481 mpt2sas_port->remote_identify.sas_address);
6482 else if (mpt2sas_port->remote_identify.device_type ==
6483 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06006484 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306485 SAS_FANOUT_EXPANDER_DEVICE)
6486 mpt2sas_expander_remove(ioc,
6487 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006488 }
6489
6490 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306491 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006492
6493 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6494 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6495 sas_expander->handle, (unsigned long long)
6496 sas_expander->sas_address);
6497
Eric Moore635374e2009-03-09 01:21:12 -06006498 kfree(sas_expander->phy);
6499 kfree(sas_expander);
6500}
6501
6502/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05306503 * _scsih_ir_shutdown - IR shutdown notification
6504 * @ioc: per adapter object
6505 *
6506 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6507 * the host system is shutting down.
6508 *
6509 * Return nothing.
6510 */
6511static void
6512_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6513{
6514 Mpi2RaidActionRequest_t *mpi_request;
6515 Mpi2RaidActionReply_t *mpi_reply;
6516 u16 smid;
6517
6518 /* is IR firmware build loaded ? */
6519 if (!ioc->ir_firmware)
6520 return;
6521
6522 /* are there any volumes ? */
6523 if (list_empty(&ioc->raid_device_list))
6524 return;
6525
6526 mutex_lock(&ioc->scsih_cmds.mutex);
6527
6528 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6529 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6530 ioc->name, __func__);
6531 goto out;
6532 }
6533 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6534
6535 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6536 if (!smid) {
6537 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6538 ioc->name, __func__);
6539 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6540 goto out;
6541 }
6542
6543 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6544 ioc->scsih_cmds.smid = smid;
6545 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6546
6547 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6548 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6549
6550 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6551 init_completion(&ioc->scsih_cmds.done);
6552 mpt2sas_base_put_smid_default(ioc, smid);
6553 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6554
6555 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6556 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6557 ioc->name, __func__);
6558 goto out;
6559 }
6560
6561 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6562 mpi_reply = ioc->scsih_cmds.reply;
6563
6564 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6565 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6566 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6567 le32_to_cpu(mpi_reply->IOCLogInfo));
6568 }
6569
6570 out:
6571 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6572 mutex_unlock(&ioc->scsih_cmds.mutex);
6573}
6574
6575/**
6576 * _scsih_shutdown - routine call during system shutdown
6577 * @pdev: PCI device struct
6578 *
6579 * Return nothing.
6580 */
6581static void
6582_scsih_shutdown(struct pci_dev *pdev)
6583{
6584 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6585 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306586 struct workqueue_struct *wq;
6587 unsigned long flags;
6588
6589 ioc->remove_host = 1;
6590 _scsih_fw_event_cleanup_queue(ioc);
6591
6592 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6593 wq = ioc->firmware_event_thread;
6594 ioc->firmware_event_thread = NULL;
6595 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6596 if (wq)
6597 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306598
6599 _scsih_ir_shutdown(ioc);
6600 mpt2sas_base_detach(ioc);
6601}
6602
6603/**
Eric Moored5d135b2009-05-18 13:02:08 -06006604 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006605 * @pdev: PCI device struct
6606 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306607 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006608 * Return nothing.
6609 */
6610static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006611_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006612{
6613 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6614 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306615 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306616 struct _raid_device *raid_device, *next;
6617 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006618 struct workqueue_struct *wq;
6619 unsigned long flags;
6620
6621 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306622 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006623
6624 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6625 wq = ioc->firmware_event_thread;
6626 ioc->firmware_event_thread = NULL;
6627 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6628 if (wq)
6629 destroy_workqueue(wq);
6630
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306631 /* release all the volumes */
6632 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6633 list) {
6634 if (raid_device->starget) {
6635 sas_target_priv_data =
6636 raid_device->starget->hostdata;
6637 sas_target_priv_data->deleted = 1;
6638 scsi_remove_target(&raid_device->starget->dev);
6639 }
6640 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6641 "(0x%016llx)\n", ioc->name, raid_device->handle,
6642 (unsigned long long) raid_device->wwid);
6643 _scsih_raid_device_remove(ioc, raid_device);
6644 }
6645
Eric Moore635374e2009-03-09 01:21:12 -06006646 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306647 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06006648 &ioc->sas_hba.sas_port_list, port_list) {
6649 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306650 SAS_END_DEVICE)
6651 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06006652 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306653 else if (mpt2sas_port->remote_identify.device_type ==
6654 SAS_EDGE_EXPANDER_DEVICE ||
6655 mpt2sas_port->remote_identify.device_type ==
6656 SAS_FANOUT_EXPANDER_DEVICE)
6657 mpt2sas_expander_remove(ioc,
6658 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006659 }
6660
6661 /* free phys attached to the sas_host */
6662 if (ioc->sas_hba.num_phys) {
6663 kfree(ioc->sas_hba.phy);
6664 ioc->sas_hba.phy = NULL;
6665 ioc->sas_hba.num_phys = 0;
6666 }
6667
6668 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306669 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006670 list_del(&ioc->list);
6671 scsi_remove_host(shost);
6672 scsi_host_put(shost);
6673}
6674
6675/**
6676 * _scsih_probe_boot_devices - reports 1st device
6677 * @ioc: per adapter object
6678 *
6679 * If specified in bios page 2, this routine reports the 1st
6680 * device scsi-ml or sas transport for persistent boot device
6681 * purposes. Please refer to function _scsih_determine_boot_device()
6682 */
6683static void
6684_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6685{
6686 u8 is_raid;
6687 void *device;
6688 struct _sas_device *sas_device;
6689 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306690 u16 handle;
6691 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006692 u64 sas_address;
6693 unsigned long flags;
6694 int rc;
6695
6696 device = NULL;
6697 if (ioc->req_boot_device.device) {
6698 device = ioc->req_boot_device.device;
6699 is_raid = ioc->req_boot_device.is_raid;
6700 } else if (ioc->req_alt_boot_device.device) {
6701 device = ioc->req_alt_boot_device.device;
6702 is_raid = ioc->req_alt_boot_device.is_raid;
6703 } else if (ioc->current_boot_device.device) {
6704 device = ioc->current_boot_device.device;
6705 is_raid = ioc->current_boot_device.is_raid;
6706 }
6707
6708 if (!device)
6709 return;
6710
6711 if (is_raid) {
6712 raid_device = device;
6713 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6714 raid_device->id, 0);
6715 if (rc)
6716 _scsih_raid_device_remove(ioc, raid_device);
6717 } else {
6718 sas_device = device;
6719 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306720 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006721 sas_address = sas_device->sas_address;
6722 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6723 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6724 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6725 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306726 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006727 _scsih_sas_device_remove(ioc, sas_device);
6728 } else if (!sas_device->starget) {
6729 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306730 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006731 _scsih_sas_device_remove(ioc, sas_device);
6732 }
6733 }
6734}
6735
6736/**
6737 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6738 * @ioc: per adapter object
6739 *
6740 * Called during initial loading of the driver.
6741 */
6742static void
6743_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6744{
6745 struct _raid_device *raid_device, *raid_next;
6746 int rc;
6747
6748 list_for_each_entry_safe(raid_device, raid_next,
6749 &ioc->raid_device_list, list) {
6750 if (raid_device->starget)
6751 continue;
6752 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6753 raid_device->id, 0);
6754 if (rc)
6755 _scsih_raid_device_remove(ioc, raid_device);
6756 }
6757}
6758
6759/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306760 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006761 * @ioc: per adapter object
6762 *
6763 * Called during initial loading of the driver.
6764 */
6765static void
6766_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6767{
6768 struct _sas_device *sas_device, *next;
6769 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006770
6771 /* SAS Device List */
6772 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6773 list) {
6774 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6775 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6776 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6777
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306778 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6779 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006780 _scsih_sas_device_remove(ioc, sas_device);
6781 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306782 mpt2sas_transport_port_remove(ioc,
6783 sas_device->sas_address,
6784 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006785 _scsih_sas_device_remove(ioc, sas_device);
6786 }
6787 }
6788}
6789
6790/**
6791 * _scsih_probe_devices - probing for devices
6792 * @ioc: per adapter object
6793 *
6794 * Called during initial loading of the driver.
6795 */
6796static void
6797_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6798{
6799 u16 volume_mapping_flags =
6800 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6801 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6802
6803 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6804 return; /* return when IOC doesn't support initiator mode */
6805
6806 _scsih_probe_boot_devices(ioc);
6807
6808 if (ioc->ir_firmware) {
6809 if ((volume_mapping_flags &
6810 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6811 _scsih_probe_sas(ioc);
6812 _scsih_probe_raid(ioc);
6813 } else {
6814 _scsih_probe_raid(ioc);
6815 _scsih_probe_sas(ioc);
6816 }
6817 } else
6818 _scsih_probe_sas(ioc);
6819}
6820
6821/**
Eric Moored5d135b2009-05-18 13:02:08 -06006822 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006823 * @pdev: PCI device struct
6824 * @id: pci device id
6825 *
6826 * Returns 0 success, anything else error.
6827 */
6828static int
Eric Moored5d135b2009-05-18 13:02:08 -06006829_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006830{
6831 struct MPT2SAS_ADAPTER *ioc;
6832 struct Scsi_Host *shost;
6833
6834 shost = scsi_host_alloc(&scsih_driver_template,
6835 sizeof(struct MPT2SAS_ADAPTER));
6836 if (!shost)
6837 return -ENODEV;
6838
6839 /* init local params */
6840 ioc = shost_priv(shost);
6841 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6842 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006843 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006844 ioc->shost = shost;
6845 ioc->id = mpt_ids++;
6846 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6847 ioc->pdev = pdev;
6848 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6849 ioc->tm_cb_idx = tm_cb_idx;
6850 ioc->ctl_cb_idx = ctl_cb_idx;
6851 ioc->base_cb_idx = base_cb_idx;
6852 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306853 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006854 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306855 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306856 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306857 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006858 ioc->logging_level = logging_level;
6859 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05306860 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06006861 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6862 spin_lock_init(&ioc->scsi_lookup_lock);
6863 spin_lock_init(&ioc->sas_device_lock);
6864 spin_lock_init(&ioc->sas_node_lock);
6865 spin_lock_init(&ioc->fw_event_lock);
6866 spin_lock_init(&ioc->raid_device_lock);
6867
6868 INIT_LIST_HEAD(&ioc->sas_device_list);
6869 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6870 INIT_LIST_HEAD(&ioc->sas_expander_list);
6871 INIT_LIST_HEAD(&ioc->fw_event_list);
6872 INIT_LIST_HEAD(&ioc->raid_device_list);
6873 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306874 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306875 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06006876
6877 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06006878 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06006879 shost->max_lun = max_lun;
6880 shost->transportt = mpt2sas_transport_template;
6881 shost->unique_id = ioc->id;
6882
6883 if ((scsi_add_host(shost, &pdev->dev))) {
6884 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6885 ioc->name, __FILE__, __LINE__, __func__);
6886 list_del(&ioc->list);
6887 goto out_add_shost_fail;
6888 }
6889
Eric Moore3c621b32009-05-18 12:59:41 -06006890 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06006891 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306892 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006893
Eric Moore635374e2009-03-09 01:21:12 -06006894 /* event thread */
6895 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6896 "fw_event%d", ioc->id);
6897 ioc->firmware_event_thread = create_singlethread_workqueue(
6898 ioc->firmware_event_name);
6899 if (!ioc->firmware_event_thread) {
6900 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6901 ioc->name, __FILE__, __LINE__, __func__);
6902 goto out_thread_fail;
6903 }
6904
6905 ioc->wait_for_port_enable_to_complete = 1;
6906 if ((mpt2sas_base_attach(ioc))) {
6907 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6908 ioc->name, __FILE__, __LINE__, __func__);
6909 goto out_attach_fail;
6910 }
6911
6912 ioc->wait_for_port_enable_to_complete = 0;
6913 _scsih_probe_devices(ioc);
6914 return 0;
6915
6916 out_attach_fail:
6917 destroy_workqueue(ioc->firmware_event_thread);
6918 out_thread_fail:
6919 list_del(&ioc->list);
6920 scsi_remove_host(shost);
6921 out_add_shost_fail:
6922 return -ENODEV;
6923}
6924
6925#ifdef CONFIG_PM
6926/**
Eric Moored5d135b2009-05-18 13:02:08 -06006927 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006928 * @pdev: PCI device struct
6929 * @state: PM state change to (usually PCI_D3)
6930 *
6931 * Returns 0 success, anything else error.
6932 */
6933static int
Eric Moored5d135b2009-05-18 13:02:08 -06006934_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006935{
6936 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6937 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6938 u32 device_state;
6939
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306940 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006941 flush_scheduled_work();
6942 scsi_block_requests(shost);
6943 device_state = pci_choose_state(pdev, state);
6944 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6945 "operating state [D%d]\n", ioc->name, pdev,
6946 pci_name(pdev), device_state);
6947
6948 mpt2sas_base_free_resources(ioc);
6949 pci_save_state(pdev);
6950 pci_disable_device(pdev);
6951 pci_set_power_state(pdev, device_state);
6952 return 0;
6953}
6954
6955/**
Eric Moored5d135b2009-05-18 13:02:08 -06006956 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006957 * @pdev: PCI device struct
6958 *
6959 * Returns 0 success, anything else error.
6960 */
6961static int
Eric Moored5d135b2009-05-18 13:02:08 -06006962_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006963{
6964 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6965 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6966 u32 device_state = pdev->current_state;
6967 int r;
6968
6969 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6970 "operating state [D%d]\n", ioc->name, pdev,
6971 pci_name(pdev), device_state);
6972
6973 pci_set_power_state(pdev, PCI_D0);
6974 pci_enable_wake(pdev, PCI_D0, 0);
6975 pci_restore_state(pdev);
6976 ioc->pdev = pdev;
6977 r = mpt2sas_base_map_resources(ioc);
6978 if (r)
6979 return r;
6980
6981 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6982 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306983 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006984 return 0;
6985}
6986#endif /* CONFIG_PM */
6987
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05306988/**
6989 * _scsih_pci_error_detected - Called when a PCI error is detected.
6990 * @pdev: PCI device struct
6991 * @state: PCI channel state
6992 *
6993 * Description: Called when a PCI error is detected.
6994 *
6995 * Return value:
6996 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
6997 */
6998static pci_ers_result_t
6999_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7000{
7001 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7002 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7003
7004 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7005 ioc->name, state);
7006
7007 switch (state) {
7008 case pci_channel_io_normal:
7009 return PCI_ERS_RESULT_CAN_RECOVER;
7010 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06007011 /* Fatal error, prepare for slot reset */
7012 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307013 scsi_block_requests(ioc->shost);
7014 mpt2sas_base_stop_watchdog(ioc);
7015 mpt2sas_base_free_resources(ioc);
7016 return PCI_ERS_RESULT_NEED_RESET;
7017 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06007018 /* Permanent error, prepare for device removal */
7019 ioc->pci_error_recovery = 1;
7020 mpt2sas_base_stop_watchdog(ioc);
7021 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307022 return PCI_ERS_RESULT_DISCONNECT;
7023 }
7024 return PCI_ERS_RESULT_NEED_RESET;
7025}
7026
7027/**
7028 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7029 * @pdev: PCI device struct
7030 *
7031 * Description: This routine is called by the pci error recovery
7032 * code after the PCI slot has been reset, just before we
7033 * should resume normal operations.
7034 */
7035static pci_ers_result_t
7036_scsih_pci_slot_reset(struct pci_dev *pdev)
7037{
7038 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7039 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7040 int rc;
7041
7042 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7043 ioc->name);
7044
Eric Moore3cb54692010-07-08 14:44:34 -06007045 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307046 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06007047 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307048 rc = mpt2sas_base_map_resources(ioc);
7049 if (rc)
7050 return PCI_ERS_RESULT_DISCONNECT;
7051
7052
7053 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7054 FORCE_BIG_HAMMER);
7055
7056 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7057 (rc == 0) ? "success" : "failed");
7058
7059 if (!rc)
7060 return PCI_ERS_RESULT_RECOVERED;
7061 else
7062 return PCI_ERS_RESULT_DISCONNECT;
7063}
7064
7065/**
7066 * _scsih_pci_resume() - resume normal ops after PCI reset
7067 * @pdev: pointer to PCI device
7068 *
7069 * Called when the error recovery driver tells us that its
7070 * OK to resume normal operation. Use completion to allow
7071 * halted scsi ops to resume.
7072 */
7073static void
7074_scsih_pci_resume(struct pci_dev *pdev)
7075{
7076 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7077 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7078
7079 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7080
7081 pci_cleanup_aer_uncorrect_error_status(pdev);
7082 mpt2sas_base_start_watchdog(ioc);
7083 scsi_unblock_requests(ioc->shost);
7084}
7085
7086/**
7087 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7088 * @pdev: pointer to PCI device
7089 */
7090static pci_ers_result_t
7091_scsih_pci_mmio_enabled(struct pci_dev *pdev)
7092{
7093 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7094 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7095
7096 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7097 ioc->name);
7098
7099 /* TODO - dump whatever for debugging purposes */
7100
7101 /* Request a slot reset. */
7102 return PCI_ERS_RESULT_NEED_RESET;
7103}
7104
7105static struct pci_error_handlers _scsih_err_handler = {
7106 .error_detected = _scsih_pci_error_detected,
7107 .mmio_enabled = _scsih_pci_mmio_enabled,
7108 .slot_reset = _scsih_pci_slot_reset,
7109 .resume = _scsih_pci_resume,
7110};
Eric Moore635374e2009-03-09 01:21:12 -06007111
7112static struct pci_driver scsih_driver = {
7113 .name = MPT2SAS_DRIVER_NAME,
7114 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06007115 .probe = _scsih_probe,
7116 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05307117 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307118 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06007119#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06007120 .suspend = _scsih_suspend,
7121 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06007122#endif
7123};
7124
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307125/* raid transport support */
7126static struct raid_function_template mpt2sas_raid_functions = {
7127 .cookie = &scsih_driver_template,
7128 .is_raid = _scsih_is_raid,
7129 .get_resync = _scsih_get_resync,
7130 .get_state = _scsih_get_state,
7131};
Eric Moore635374e2009-03-09 01:21:12 -06007132
7133/**
Eric Moored5d135b2009-05-18 13:02:08 -06007134 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06007135 *
7136 * Returns 0 success, anything else error.
7137 */
7138static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06007139_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06007140{
7141 int error;
7142
7143 mpt_ids = 0;
7144 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7145 MPT2SAS_DRIVER_VERSION);
7146
7147 mpt2sas_transport_template =
7148 sas_attach_transport(&mpt2sas_transport_functions);
7149 if (!mpt2sas_transport_template)
7150 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307151 /* raid transport support */
7152 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7153 if (!mpt2sas_raid_template) {
7154 sas_release_transport(mpt2sas_transport_template);
7155 return -ENODEV;
7156 }
Eric Moore635374e2009-03-09 01:21:12 -06007157
7158 mpt2sas_base_initialize_callback_handler();
7159
7160 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06007161 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06007162
Uwe Kleine-König65155b32010-06-11 12:17:01 +02007163 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06007164 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06007165
7166 /* base internal commands callback handler */
7167 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7168
7169 /* transport internal commands callback handler */
7170 transport_cb_idx = mpt2sas_base_register_callback_handler(
7171 mpt2sas_transport_done);
7172
Kashyap, Desai744090d2009-10-05 15:56:56 +05307173 /* scsih internal commands callback handler */
7174 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7175
Eric Moore635374e2009-03-09 01:21:12 -06007176 /* configuration page API internal commands callback handler */
7177 config_cb_idx = mpt2sas_base_register_callback_handler(
7178 mpt2sas_config_done);
7179
7180 /* ctl module callback handler */
7181 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7182
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307183 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7184 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307185
7186 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7187 _scsih_tm_volume_tr_complete);
7188
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307189 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7190 _scsih_sas_control_complete);
7191
Eric Moore635374e2009-03-09 01:21:12 -06007192 mpt2sas_ctl_init();
7193
7194 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307195 if (error) {
7196 /* raid transport support */
7197 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06007198 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307199 }
Eric Moore635374e2009-03-09 01:21:12 -06007200
7201 return error;
7202}
7203
7204/**
Eric Moored5d135b2009-05-18 13:02:08 -06007205 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06007206 *
7207 * Returns 0 success, anything else error.
7208 */
7209static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06007210_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06007211{
7212 printk(KERN_INFO "mpt2sas version %s unloading\n",
7213 MPT2SAS_DRIVER_VERSION);
7214
7215 pci_unregister_driver(&scsih_driver);
7216
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307217 mpt2sas_ctl_exit();
7218
Eric Moore635374e2009-03-09 01:21:12 -06007219 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7220 mpt2sas_base_release_callback_handler(tm_cb_idx);
7221 mpt2sas_base_release_callback_handler(base_cb_idx);
7222 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307223 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06007224 mpt2sas_base_release_callback_handler(config_cb_idx);
7225 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7226
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307227 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307228 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307229 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7230
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307231 /* raid transport support */
7232 raid_class_release(mpt2sas_raid_template);
7233 sas_release_transport(mpt2sas_transport_template);
7234
Eric Moore635374e2009-03-09 01:21:12 -06007235}
7236
Eric Moored5d135b2009-05-18 13:02:08 -06007237module_init(_scsih_init);
7238module_exit(_scsih_exit);