blob: 854cc91e7aace142dda446cd7d8cffb70247df1d [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/**
Eric Moore635374e2009-03-09 01:21:12 -0600934 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
935 * @ioc: per adapter object
936 * @smid: system request message index
937 *
938 * Returns phys pointer to chain buffer.
939 */
940static dma_addr_t
941_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
942{
943 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
944 ioc->chains_needed_per_io));
945}
946
947/**
948 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
949 * @ioc: per adapter object
950 * @smid: system request message index
951 *
952 * Returns virt pointer to chain buffer.
953 */
954static void *
955_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
956{
957 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
958 ioc->chains_needed_per_io)));
959}
960
961/**
962 * _scsih_build_scatter_gather - main sg creation routine
963 * @ioc: per adapter object
964 * @scmd: scsi command
965 * @smid: system request message index
966 * Context: none.
967 *
968 * The main routine that builds scatter gather table from a given
969 * scsi request sent via the .queuecommand main handler.
970 *
971 * Returns 0 success, anything else error
972 */
973static int
974_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
975 struct scsi_cmnd *scmd, u16 smid)
976{
977 Mpi2SCSIIORequest_t *mpi_request;
978 dma_addr_t chain_dma;
979 struct scatterlist *sg_scmd;
980 void *sg_local, *chain;
981 u32 chain_offset;
982 u32 chain_length;
983 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +0900984 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -0600985 u32 sges_in_segment;
986 u32 sgl_flags;
987 u32 sgl_flags_last_element;
988 u32 sgl_flags_end_buffer;
989
990 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
991
992 /* init scatter gather flags */
993 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
994 if (scmd->sc_data_direction == DMA_TO_DEVICE)
995 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
996 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
997 << MPI2_SGE_FLAGS_SHIFT;
998 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
999 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1000 << MPI2_SGE_FLAGS_SHIFT;
1001 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1002
1003 sg_scmd = scsi_sglist(scmd);
1004 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001005 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001006 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1007 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1008 return -ENOMEM;
1009 }
1010
1011 sg_local = &mpi_request->SGL;
1012 sges_in_segment = ioc->max_sges_in_main_message;
1013 if (sges_left <= sges_in_segment)
1014 goto fill_in_last_segment;
1015
1016 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1017 (sges_in_segment * ioc->sge_size))/4;
1018
1019 /* fill in main message segment when there is a chain following */
1020 while (sges_in_segment) {
1021 if (sges_in_segment == 1)
1022 ioc->base_add_sg_single(sg_local,
1023 sgl_flags_last_element | sg_dma_len(sg_scmd),
1024 sg_dma_address(sg_scmd));
1025 else
1026 ioc->base_add_sg_single(sg_local, sgl_flags |
1027 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1028 sg_scmd = sg_next(sg_scmd);
1029 sg_local += ioc->sge_size;
1030 sges_left--;
1031 sges_in_segment--;
1032 }
1033
1034 /* initializing the chain flags and pointers */
1035 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1036 chain = _scsih_get_chain_buffer(ioc, smid);
1037 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
1038 do {
1039 sges_in_segment = (sges_left <=
1040 ioc->max_sges_in_chain_message) ? sges_left :
1041 ioc->max_sges_in_chain_message;
1042 chain_offset = (sges_left == sges_in_segment) ?
1043 0 : (sges_in_segment * ioc->sge_size)/4;
1044 chain_length = sges_in_segment * ioc->sge_size;
1045 if (chain_offset) {
1046 chain_offset = chain_offset <<
1047 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1048 chain_length += ioc->sge_size;
1049 }
1050 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1051 chain_length, chain_dma);
1052 sg_local = chain;
1053 if (!chain_offset)
1054 goto fill_in_last_segment;
1055
1056 /* fill in chain segments */
1057 while (sges_in_segment) {
1058 if (sges_in_segment == 1)
1059 ioc->base_add_sg_single(sg_local,
1060 sgl_flags_last_element |
1061 sg_dma_len(sg_scmd),
1062 sg_dma_address(sg_scmd));
1063 else
1064 ioc->base_add_sg_single(sg_local, sgl_flags |
1065 sg_dma_len(sg_scmd),
1066 sg_dma_address(sg_scmd));
1067 sg_scmd = sg_next(sg_scmd);
1068 sg_local += ioc->sge_size;
1069 sges_left--;
1070 sges_in_segment--;
1071 }
1072
1073 chain_dma += ioc->request_sz;
1074 chain += ioc->request_sz;
1075 } while (1);
1076
1077
1078 fill_in_last_segment:
1079
1080 /* fill the last segment */
1081 while (sges_left) {
1082 if (sges_left == 1)
1083 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1084 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1085 else
1086 ioc->base_add_sg_single(sg_local, sgl_flags |
1087 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1088 sg_scmd = sg_next(sg_scmd);
1089 sg_local += ioc->sge_size;
1090 sges_left--;
1091 }
1092
1093 return 0;
1094}
1095
1096/**
Eric Moored5d135b2009-05-18 13:02:08 -06001097 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001098 * @sdev: scsi device struct
1099 * @qdepth: requested queue depth
Mike Christiee881a172009-10-15 17:46:39 -07001100 * @reason: calling context
Eric Moore635374e2009-03-09 01:21:12 -06001101 *
1102 * Returns queue depth.
1103 */
1104static int
Mike Christiee881a172009-10-15 17:46:39 -07001105_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
Eric Moore635374e2009-03-09 01:21:12 -06001106{
1107 struct Scsi_Host *shost = sdev->host;
1108 int max_depth;
1109 int tag_type;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301110 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1111 struct MPT2SAS_DEVICE *sas_device_priv_data;
1112 struct MPT2SAS_TARGET *sas_target_priv_data;
1113 struct _sas_device *sas_device;
1114 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001115
Mike Christiee881a172009-10-15 17:46:39 -07001116 if (reason != SCSI_QDEPTH_DEFAULT)
1117 return -EOPNOTSUPP;
1118
Eric Moore635374e2009-03-09 01:21:12 -06001119 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301120
1121 /* limit max device queue for SATA to 32 */
1122 sas_device_priv_data = sdev->hostdata;
1123 if (!sas_device_priv_data)
1124 goto not_sata;
1125 sas_target_priv_data = sas_device_priv_data->sas_target;
1126 if (!sas_target_priv_data)
1127 goto not_sata;
1128 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1129 goto not_sata;
1130 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1131 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1132 sas_device_priv_data->sas_target->sas_address);
1133 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1134 if (sas_device && sas_device->device_info &
1135 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1136 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1137
1138 not_sata:
1139
Eric Moore635374e2009-03-09 01:21:12 -06001140 if (!sdev->tagged_supported)
1141 max_depth = 1;
1142 if (qdepth > max_depth)
1143 qdepth = max_depth;
1144 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1145 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1146
1147 if (sdev->inquiry_len > 7)
1148 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1149 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1150 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1151 sdev->ordered_tags, sdev->scsi_level,
1152 (sdev->inquiry[7] & 2) >> 1);
1153
1154 return sdev->queue_depth;
1155}
1156
1157/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301158 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001159 * @sdev: scsi device struct
1160 * @tag_type: requested tag type
1161 *
1162 * Returns queue tag type.
1163 */
1164static int
Eric Moored5d135b2009-05-18 13:02:08 -06001165_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001166{
1167 if (sdev->tagged_supported) {
1168 scsi_set_tag_type(sdev, tag_type);
1169 if (tag_type)
1170 scsi_activate_tcq(sdev, sdev->queue_depth);
1171 else
1172 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1173 } else
1174 tag_type = 0;
1175
1176 return tag_type;
1177}
1178
1179/**
Eric Moored5d135b2009-05-18 13:02:08 -06001180 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001181 * @starget: scsi target struct
1182 *
1183 * Returns 0 if ok. Any other return is assumed to be an error and
1184 * the device is ignored.
1185 */
1186static int
Eric Moored5d135b2009-05-18 13:02:08 -06001187_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001188{
1189 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1190 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1191 struct MPT2SAS_TARGET *sas_target_priv_data;
1192 struct _sas_device *sas_device;
1193 struct _raid_device *raid_device;
1194 unsigned long flags;
1195 struct sas_rphy *rphy;
1196
1197 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1198 if (!sas_target_priv_data)
1199 return -ENOMEM;
1200
1201 starget->hostdata = sas_target_priv_data;
1202 sas_target_priv_data->starget = starget;
1203 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1204
1205 /* RAID volumes */
1206 if (starget->channel == RAID_CHANNEL) {
1207 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1208 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1209 starget->channel);
1210 if (raid_device) {
1211 sas_target_priv_data->handle = raid_device->handle;
1212 sas_target_priv_data->sas_address = raid_device->wwid;
1213 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1214 raid_device->starget = starget;
1215 }
1216 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1217 return 0;
1218 }
1219
1220 /* sas/sata devices */
1221 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1222 rphy = dev_to_rphy(starget->dev.parent);
1223 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1224 rphy->identify.sas_address);
1225
1226 if (sas_device) {
1227 sas_target_priv_data->handle = sas_device->handle;
1228 sas_target_priv_data->sas_address = sas_device->sas_address;
1229 sas_device->starget = starget;
1230 sas_device->id = starget->id;
1231 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301232 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001233 sas_target_priv_data->flags |=
1234 MPT_TARGET_FLAGS_RAID_COMPONENT;
1235 }
1236 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1237
1238 return 0;
1239}
1240
1241/**
Eric Moored5d135b2009-05-18 13:02:08 -06001242 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001243 * @starget: scsi target struct
1244 *
1245 * Returns nothing.
1246 */
1247static void
Eric Moored5d135b2009-05-18 13:02:08 -06001248_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001249{
1250 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1251 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1252 struct MPT2SAS_TARGET *sas_target_priv_data;
1253 struct _sas_device *sas_device;
1254 struct _raid_device *raid_device;
1255 unsigned long flags;
1256 struct sas_rphy *rphy;
1257
1258 sas_target_priv_data = starget->hostdata;
1259 if (!sas_target_priv_data)
1260 return;
1261
1262 if (starget->channel == RAID_CHANNEL) {
1263 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1264 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1265 starget->channel);
1266 if (raid_device) {
1267 raid_device->starget = NULL;
1268 raid_device->sdev = NULL;
1269 }
1270 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1271 goto out;
1272 }
1273
1274 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1275 rphy = dev_to_rphy(starget->dev.parent);
1276 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1277 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001278 if (sas_device && (sas_device->starget == starget) &&
1279 (sas_device->id == starget->id) &&
1280 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001281 sas_device->starget = NULL;
1282
1283 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1284
1285 out:
1286 kfree(sas_target_priv_data);
1287 starget->hostdata = NULL;
1288}
1289
1290/**
Eric Moored5d135b2009-05-18 13:02:08 -06001291 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001292 * @sdev: scsi device struct
1293 *
1294 * Returns 0 if ok. Any other return is assumed to be an error and
1295 * the device is ignored.
1296 */
1297static int
Eric Moored5d135b2009-05-18 13:02:08 -06001298_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001299{
1300 struct Scsi_Host *shost;
1301 struct MPT2SAS_ADAPTER *ioc;
1302 struct MPT2SAS_TARGET *sas_target_priv_data;
1303 struct MPT2SAS_DEVICE *sas_device_priv_data;
1304 struct scsi_target *starget;
1305 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001306 unsigned long flags;
1307
1308 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1309 if (!sas_device_priv_data)
1310 return -ENOMEM;
1311
1312 sas_device_priv_data->lun = sdev->lun;
1313 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1314
1315 starget = scsi_target(sdev);
1316 sas_target_priv_data = starget->hostdata;
1317 sas_target_priv_data->num_luns++;
1318 sas_device_priv_data->sas_target = sas_target_priv_data;
1319 sdev->hostdata = sas_device_priv_data;
1320 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1321 sdev->no_uld_attach = 1;
1322
1323 shost = dev_to_shost(&starget->dev);
1324 ioc = shost_priv(shost);
1325 if (starget->channel == RAID_CHANNEL) {
1326 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1327 raid_device = _scsih_raid_device_find_by_id(ioc,
1328 starget->id, starget->channel);
1329 if (raid_device)
1330 raid_device->sdev = sdev; /* raid is single lun */
1331 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001332 }
1333
Eric Moore635374e2009-03-09 01:21:12 -06001334 return 0;
1335}
1336
1337/**
Eric Moored5d135b2009-05-18 13:02:08 -06001338 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001339 * @sdev: scsi device struct
1340 *
1341 * Returns nothing.
1342 */
1343static void
Eric Moored5d135b2009-05-18 13:02:08 -06001344_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001345{
1346 struct MPT2SAS_TARGET *sas_target_priv_data;
1347 struct scsi_target *starget;
1348
1349 if (!sdev->hostdata)
1350 return;
1351
1352 starget = scsi_target(sdev);
1353 sas_target_priv_data = starget->hostdata;
1354 sas_target_priv_data->num_luns--;
1355 kfree(sdev->hostdata);
1356 sdev->hostdata = NULL;
1357}
1358
1359/**
Eric Moored5d135b2009-05-18 13:02:08 -06001360 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001361 * @ioc: per adapter object
1362 * @sas_device: the sas_device object
1363 * @sdev: scsi device struct
1364 */
1365static void
Eric Moored5d135b2009-05-18 13:02:08 -06001366_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001367 struct _sas_device *sas_device, struct scsi_device *sdev)
1368{
1369 Mpi2ConfigReply_t mpi_reply;
1370 Mpi2SasDevicePage0_t sas_device_pg0;
1371 u32 ioc_status;
1372 u16 flags;
1373 u32 device_info;
1374
1375 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1376 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1377 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1378 ioc->name, __FILE__, __LINE__, __func__);
1379 return;
1380 }
1381
1382 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1383 MPI2_IOCSTATUS_MASK;
1384 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1385 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1386 ioc->name, __FILE__, __LINE__, __func__);
1387 return;
1388 }
1389
1390 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301391 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001392
1393 sdev_printk(KERN_INFO, sdev,
1394 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1395 "sw_preserve(%s)\n",
1396 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1397 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1398 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1399 "n",
1400 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1401 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1402 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1403}
1404
1405/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301406 * _scsih_is_raid - return boolean indicating device is raid volume
1407 * @dev the device struct object
1408 */
1409static int
1410_scsih_is_raid(struct device *dev)
1411{
1412 struct scsi_device *sdev = to_scsi_device(dev);
1413
1414 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1415}
1416
1417/**
1418 * _scsih_get_resync - get raid volume resync percent complete
1419 * @dev the device struct object
1420 */
1421static void
1422_scsih_get_resync(struct device *dev)
1423{
1424 struct scsi_device *sdev = to_scsi_device(dev);
1425 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1426 static struct _raid_device *raid_device;
1427 unsigned long flags;
1428 Mpi2RaidVolPage0_t vol_pg0;
1429 Mpi2ConfigReply_t mpi_reply;
1430 u32 volume_status_flags;
1431 u8 percent_complete = 0;
1432
1433 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1434 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1435 sdev->channel);
1436 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1437
1438 if (!raid_device)
1439 goto out;
1440
1441 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1442 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1443 sizeof(Mpi2RaidVolPage0_t))) {
1444 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1445 ioc->name, __FILE__, __LINE__, __func__);
1446 goto out;
1447 }
1448
1449 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1450 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1451 percent_complete = raid_device->percent_complete;
1452 out:
1453 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1454}
1455
1456/**
1457 * _scsih_get_state - get raid volume level
1458 * @dev the device struct object
1459 */
1460static void
1461_scsih_get_state(struct device *dev)
1462{
1463 struct scsi_device *sdev = to_scsi_device(dev);
1464 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1465 static struct _raid_device *raid_device;
1466 unsigned long flags;
1467 Mpi2RaidVolPage0_t vol_pg0;
1468 Mpi2ConfigReply_t mpi_reply;
1469 u32 volstate;
1470 enum raid_state state = RAID_STATE_UNKNOWN;
1471
1472 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1473 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1474 sdev->channel);
1475 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1476
1477 if (!raid_device)
1478 goto out;
1479
1480 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1481 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1482 sizeof(Mpi2RaidVolPage0_t))) {
1483 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1484 ioc->name, __FILE__, __LINE__, __func__);
1485 goto out;
1486 }
1487
1488 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1489 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1490 state = RAID_STATE_RESYNCING;
1491 goto out;
1492 }
1493
1494 switch (vol_pg0.VolumeState) {
1495 case MPI2_RAID_VOL_STATE_OPTIMAL:
1496 case MPI2_RAID_VOL_STATE_ONLINE:
1497 state = RAID_STATE_ACTIVE;
1498 break;
1499 case MPI2_RAID_VOL_STATE_DEGRADED:
1500 state = RAID_STATE_DEGRADED;
1501 break;
1502 case MPI2_RAID_VOL_STATE_FAILED:
1503 case MPI2_RAID_VOL_STATE_MISSING:
1504 state = RAID_STATE_OFFLINE;
1505 break;
1506 }
1507 out:
1508 raid_set_state(mpt2sas_raid_template, dev, state);
1509}
1510
1511/**
1512 * _scsih_set_level - set raid level
1513 * @sdev: scsi device struct
1514 * @raid_device: raid_device object
1515 */
1516static void
1517_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1518{
1519 enum raid_level level = RAID_LEVEL_UNKNOWN;
1520
1521 switch (raid_device->volume_type) {
1522 case MPI2_RAID_VOL_TYPE_RAID0:
1523 level = RAID_LEVEL_0;
1524 break;
1525 case MPI2_RAID_VOL_TYPE_RAID10:
1526 level = RAID_LEVEL_10;
1527 break;
1528 case MPI2_RAID_VOL_TYPE_RAID1E:
1529 level = RAID_LEVEL_1E;
1530 break;
1531 case MPI2_RAID_VOL_TYPE_RAID1:
1532 level = RAID_LEVEL_1;
1533 break;
1534 }
1535
1536 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1537}
1538
1539/**
Eric Moore635374e2009-03-09 01:21:12 -06001540 * _scsih_get_volume_capabilities - volume capabilities
1541 * @ioc: per adapter object
1542 * @sas_device: the raid_device object
1543 */
1544static void
1545_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1546 struct _raid_device *raid_device)
1547{
1548 Mpi2RaidVolPage0_t *vol_pg0;
1549 Mpi2RaidPhysDiskPage0_t pd_pg0;
1550 Mpi2SasDevicePage0_t sas_device_pg0;
1551 Mpi2ConfigReply_t mpi_reply;
1552 u16 sz;
1553 u8 num_pds;
1554
1555 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1556 &num_pds)) || !num_pds) {
1557 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1558 ioc->name, __FILE__, __LINE__, __func__);
1559 return;
1560 }
1561
1562 raid_device->num_pds = num_pds;
1563 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1564 sizeof(Mpi2RaidVol0PhysDisk_t));
1565 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1566 if (!vol_pg0) {
1567 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1568 ioc->name, __FILE__, __LINE__, __func__);
1569 return;
1570 }
1571
1572 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1573 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1574 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1575 ioc->name, __FILE__, __LINE__, __func__);
1576 kfree(vol_pg0);
1577 return;
1578 }
1579
1580 raid_device->volume_type = vol_pg0->VolumeType;
1581
1582 /* figure out what the underlying devices are by
1583 * obtaining the device_info bits for the 1st device
1584 */
1585 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1586 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1587 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1588 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1589 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1590 le16_to_cpu(pd_pg0.DevHandle)))) {
1591 raid_device->device_info =
1592 le32_to_cpu(sas_device_pg0.DeviceInfo);
1593 }
1594 }
1595
1596 kfree(vol_pg0);
1597}
1598
1599/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301600 * _scsih_enable_tlr - setting TLR flags
1601 * @ioc: per adapter object
1602 * @sdev: scsi device struct
1603 *
1604 * Enabling Transaction Layer Retries for tape devices when
1605 * vpd page 0x90 is present
1606 *
1607 */
1608static void
1609_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1610{
1611 /* only for TAPE */
1612 if (sdev->type != TYPE_TAPE)
1613 return;
1614
1615 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1616 return;
1617
1618 sas_enable_tlr(sdev);
1619 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1620 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1621 return;
1622
1623}
1624
1625/**
Eric Moored5d135b2009-05-18 13:02:08 -06001626 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001627 * @sdev: scsi device struct
1628 *
1629 * Returns 0 if ok. Any other return is assumed to be an error and
1630 * the device is ignored.
1631 */
1632static int
Eric Moored5d135b2009-05-18 13:02:08 -06001633_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001634{
1635 struct Scsi_Host *shost = sdev->host;
1636 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1637 struct MPT2SAS_DEVICE *sas_device_priv_data;
1638 struct MPT2SAS_TARGET *sas_target_priv_data;
1639 struct _sas_device *sas_device;
1640 struct _raid_device *raid_device;
1641 unsigned long flags;
1642 int qdepth;
1643 u8 ssp_target = 0;
1644 char *ds = "";
1645 char *r_level = "";
1646
1647 qdepth = 1;
1648 sas_device_priv_data = sdev->hostdata;
1649 sas_device_priv_data->configured_lun = 1;
1650 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1651 sas_target_priv_data = sas_device_priv_data->sas_target;
1652
1653 /* raid volume handling */
1654 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1655
1656 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1657 raid_device = _scsih_raid_device_find_by_handle(ioc,
1658 sas_target_priv_data->handle);
1659 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1660 if (!raid_device) {
1661 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1662 ioc->name, __FILE__, __LINE__, __func__);
1663 return 0;
1664 }
1665
1666 _scsih_get_volume_capabilities(ioc, raid_device);
1667
1668 /* RAID Queue Depth Support
1669 * IS volume = underlying qdepth of drive type, either
1670 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1671 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1672 */
1673 if (raid_device->device_info &
1674 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1675 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1676 ds = "SSP";
1677 } else {
1678 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1679 if (raid_device->device_info &
1680 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1681 ds = "SATA";
1682 else
1683 ds = "STP";
1684 }
1685
1686 switch (raid_device->volume_type) {
1687 case MPI2_RAID_VOL_TYPE_RAID0:
1688 r_level = "RAID0";
1689 break;
1690 case MPI2_RAID_VOL_TYPE_RAID1E:
1691 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301692 if (ioc->manu_pg10.OEMIdentifier &&
1693 (ioc->manu_pg10.GenericFlags0 &
1694 MFG10_GF0_R10_DISPLAY) &&
1695 !(raid_device->num_pds % 2))
1696 r_level = "RAID10";
1697 else
1698 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001699 break;
1700 case MPI2_RAID_VOL_TYPE_RAID1:
1701 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1702 r_level = "RAID1";
1703 break;
1704 case MPI2_RAID_VOL_TYPE_RAID10:
1705 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1706 r_level = "RAID10";
1707 break;
1708 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1709 default:
1710 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1711 r_level = "RAIDX";
1712 break;
1713 }
1714
1715 sdev_printk(KERN_INFO, sdev, "%s: "
1716 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1717 r_level, raid_device->handle,
1718 (unsigned long long)raid_device->wwid,
1719 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001720 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301721 /* raid transport support */
1722 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001723 return 0;
1724 }
1725
1726 /* non-raid handling */
1727 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1728 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1729 sas_device_priv_data->sas_target->sas_address);
1730 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1731 if (sas_device) {
1732 if (sas_target_priv_data->flags &
1733 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1734 mpt2sas_config_get_volume_handle(ioc,
1735 sas_device->handle, &sas_device->volume_handle);
1736 mpt2sas_config_get_volume_wwid(ioc,
1737 sas_device->volume_handle,
1738 &sas_device->volume_wwid);
1739 }
1740 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1741 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1742 ssp_target = 1;
1743 ds = "SSP";
1744 } else {
1745 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1746 if (sas_device->device_info &
1747 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1748 ds = "STP";
1749 else if (sas_device->device_info &
1750 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1751 ds = "SATA";
1752 }
1753
1754 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05301755 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06001756 ds, sas_device->handle,
1757 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05301758 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06001759 (unsigned long long)sas_device->device_name);
1760 sdev_printk(KERN_INFO, sdev, "%s: "
1761 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1762 (unsigned long long) sas_device->enclosure_logical_id,
1763 sas_device->slot);
1764
1765 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06001766 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06001767 }
1768
Mike Christiee881a172009-10-15 17:46:39 -07001769 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06001770
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301771 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06001772 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301773 _scsih_enable_tlr(ioc, sdev);
1774 }
Eric Moore635374e2009-03-09 01:21:12 -06001775 return 0;
1776}
1777
1778/**
Eric Moored5d135b2009-05-18 13:02:08 -06001779 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06001780 * @sdev: scsi device struct
1781 * @bdev: pointer to block device context
1782 * @capacity: device size (in 512 byte sectors)
1783 * @params: three element array to place output:
1784 * params[0] number of heads (max 255)
1785 * params[1] number of sectors (max 63)
1786 * params[2] number of cylinders
1787 *
1788 * Return nothing.
1789 */
1790static int
Eric Moored5d135b2009-05-18 13:02:08 -06001791_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06001792 sector_t capacity, int params[])
1793{
1794 int heads;
1795 int sectors;
1796 sector_t cylinders;
1797 ulong dummy;
1798
1799 heads = 64;
1800 sectors = 32;
1801
1802 dummy = heads * sectors;
1803 cylinders = capacity;
1804 sector_div(cylinders, dummy);
1805
1806 /*
1807 * Handle extended translation size for logical drives
1808 * > 1Gb
1809 */
1810 if ((ulong)capacity >= 0x200000) {
1811 heads = 255;
1812 sectors = 63;
1813 dummy = heads * sectors;
1814 cylinders = capacity;
1815 sector_div(cylinders, dummy);
1816 }
1817
1818 /* return result */
1819 params[0] = heads;
1820 params[1] = sectors;
1821 params[2] = cylinders;
1822
1823 return 0;
1824}
1825
1826/**
1827 * _scsih_response_code - translation of device response code
1828 * @ioc: per adapter object
1829 * @response_code: response code returned by the device
1830 *
1831 * Return nothing.
1832 */
1833static void
1834_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1835{
1836 char *desc;
1837
1838 switch (response_code) {
1839 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1840 desc = "task management request completed";
1841 break;
1842 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1843 desc = "invalid frame";
1844 break;
1845 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1846 desc = "task management request not supported";
1847 break;
1848 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1849 desc = "task management request failed";
1850 break;
1851 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1852 desc = "task management request succeeded";
1853 break;
1854 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1855 desc = "invalid lun";
1856 break;
1857 case 0xA:
1858 desc = "overlapped tag attempted";
1859 break;
1860 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1861 desc = "task queued, however not sent to target";
1862 break;
1863 default:
1864 desc = "unknown";
1865 break;
1866 }
1867 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1868 ioc->name, response_code, desc);
1869}
1870
1871/**
Eric Moored5d135b2009-05-18 13:02:08 -06001872 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06001873 * @ioc: per adapter object
1874 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301875 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001876 * @reply: reply message frame(lower 32bit addr)
1877 * Context: none.
1878 *
1879 * The callback handler when using scsih_issue_tm.
1880 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301881 * Return 1 meaning mf should be freed from _base_interrupt
1882 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06001883 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301884static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301885_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06001886{
1887 MPI2DefaultReply_t *mpi_reply;
1888
1889 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301890 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001891 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301892 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001893 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1894 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1895 if (mpi_reply) {
1896 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1897 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1898 }
1899 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1900 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05301901 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001902}
1903
1904/**
1905 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1906 * @ioc: per adapter object
1907 * @handle: device handle
1908 *
1909 * During taskmangement request, we need to freeze the device queue.
1910 */
1911void
1912mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1913{
1914 struct MPT2SAS_DEVICE *sas_device_priv_data;
1915 struct scsi_device *sdev;
1916 u8 skip = 0;
1917
1918 shost_for_each_device(sdev, ioc->shost) {
1919 if (skip)
1920 continue;
1921 sas_device_priv_data = sdev->hostdata;
1922 if (!sas_device_priv_data)
1923 continue;
1924 if (sas_device_priv_data->sas_target->handle == handle) {
1925 sas_device_priv_data->sas_target->tm_busy = 1;
1926 skip = 1;
1927 ioc->ignore_loginfos = 1;
1928 }
1929 }
1930}
1931
1932/**
1933 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1934 * @ioc: per adapter object
1935 * @handle: device handle
1936 *
1937 * During taskmangement request, we need to freeze the device queue.
1938 */
1939void
1940mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1941{
1942 struct MPT2SAS_DEVICE *sas_device_priv_data;
1943 struct scsi_device *sdev;
1944 u8 skip = 0;
1945
1946 shost_for_each_device(sdev, ioc->shost) {
1947 if (skip)
1948 continue;
1949 sas_device_priv_data = sdev->hostdata;
1950 if (!sas_device_priv_data)
1951 continue;
1952 if (sas_device_priv_data->sas_target->handle == handle) {
1953 sas_device_priv_data->sas_target->tm_busy = 0;
1954 skip = 1;
1955 ioc->ignore_loginfos = 0;
1956 }
1957 }
1958}
1959
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301960
Eric Moore635374e2009-03-09 01:21:12 -06001961/**
1962 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1963 * @ioc: per adapter struct
1964 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301965 * @channel: the channel assigned by the OS
1966 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06001967 * @lun: lun number
1968 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1969 * @smid_task: smid assigned to the task
1970 * @timeout: timeout in seconds
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301971 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06001972 *
1973 * A generic API for sending task management requests to firmware.
1974 *
Eric Moore635374e2009-03-09 01:21:12 -06001975 * The callback index is set inside `ioc->tm_cb_idx`.
1976 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301977 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06001978 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301979int
1980mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
1981 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
1982 struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06001983{
1984 Mpi2SCSITaskManagementRequest_t *mpi_request;
1985 Mpi2SCSITaskManagementReply_t *mpi_reply;
1986 u16 smid = 0;
1987 u32 ioc_state;
1988 unsigned long timeleft;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301989 struct scsi_cmnd *scmd_lookup;
1990 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001991
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301992 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301993 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
1994 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
1995 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05301996 rc = FAILED;
1997 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05301998 }
1999
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05302000 if (ioc->shost_recovery || ioc->remove_host) {
Eric Moore635374e2009-03-09 01:21:12 -06002001 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2002 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302003 rc = FAILED;
2004 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002005 }
Eric Moore635374e2009-03-09 01:21:12 -06002006
2007 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2008 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302009 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002010 "active!\n", ioc->name));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302011 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2012 FORCE_BIG_HAMMER);
2013 rc = SUCCESS;
2014 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002015 }
2016
2017 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2018 mpt2sas_base_fault_info(ioc, ioc_state &
2019 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302020 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2021 FORCE_BIG_HAMMER);
2022 rc = SUCCESS;
2023 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002024 }
2025
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302026 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002027 if (!smid) {
2028 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2029 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302030 rc = FAILED;
2031 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002032 }
2033
2034 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302035 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2036 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002037 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2038 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2039 ioc->tm_cmds.smid = smid;
2040 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2041 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2042 mpi_request->DevHandle = cpu_to_le16(handle);
2043 mpi_request->TaskType = type;
2044 mpi_request->TaskMID = cpu_to_le16(smid_task);
2045 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2046 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302047 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302048 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002049 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002050 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2051 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2052 ioc->name, __func__);
2053 _debug_dump_mf(mpi_request,
2054 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302055 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2056 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2057 FORCE_BIG_HAMMER);
2058 rc = SUCCESS;
2059 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2060 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2061 goto err_out;
2062 }
Eric Moore635374e2009-03-09 01:21:12 -06002063 }
2064
2065 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2066 mpi_reply = ioc->tm_cmds.reply;
2067 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2068 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2069 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2070 le32_to_cpu(mpi_reply->IOCLogInfo),
2071 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302072 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002073 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302074 if (mpi_reply->IOCStatus)
2075 _debug_dump_mf(mpi_request,
2076 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2077 }
Eric Moore635374e2009-03-09 01:21:12 -06002078 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302079
2080 /* sanity check:
2081 * Check to see the commands were terminated.
2082 * This is only needed for eh callbacks, hence the scmd check.
2083 */
2084 rc = FAILED;
2085 if (scmd == NULL)
2086 goto bypass_sanity_checks;
2087 switch (type) {
2088 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2089 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
2090 if (scmd_lookup && (scmd_lookup->serial_number ==
2091 scmd->serial_number))
2092 rc = FAILED;
2093 else
2094 rc = SUCCESS;
2095 break;
2096
2097 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2098 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2099 rc = FAILED;
2100 else
2101 rc = SUCCESS;
2102 break;
2103
2104 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2105 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2106 rc = FAILED;
2107 else
2108 rc = SUCCESS;
2109 break;
2110 }
2111
2112 bypass_sanity_checks:
2113
2114 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2115 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2116 mutex_unlock(&ioc->tm_cmds.mutex);
2117
2118 return rc;
2119
2120 err_out:
2121 mutex_unlock(&ioc->tm_cmds.mutex);
2122 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002123}
2124
2125/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302126 * _scsih_tm_display_info - displays info about the device
2127 * @ioc: per adapter struct
2128 * @scmd: pointer to scsi command object
2129 *
2130 * Called by task management callback handlers.
2131 */
2132static void
2133_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2134{
2135 struct scsi_target *starget = scmd->device->sdev_target;
2136 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2137 struct _sas_device *sas_device = NULL;
2138 unsigned long flags;
2139
2140 if (!priv_target)
2141 return;
2142
2143 scsi_print_command(scmd);
2144 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2145 starget_printk(KERN_INFO, starget, "volume handle(0x%04x), "
2146 "volume wwid(0x%016llx)\n",
2147 priv_target->handle,
2148 (unsigned long long)priv_target->sas_address);
2149 } else {
2150 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2151 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2152 priv_target->sas_address);
2153 if (sas_device) {
2154 if (priv_target->flags &
2155 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2156 starget_printk(KERN_INFO, starget,
2157 "volume handle(0x%04x), "
2158 "volume wwid(0x%016llx)\n",
2159 sas_device->volume_handle,
2160 (unsigned long long)sas_device->volume_wwid);
2161 }
2162 starget_printk(KERN_INFO, starget,
2163 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2164 sas_device->handle,
2165 (unsigned long long)sas_device->sas_address,
2166 sas_device->phy);
2167 starget_printk(KERN_INFO, starget,
2168 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2169 (unsigned long long)sas_device->enclosure_logical_id,
2170 sas_device->slot);
2171 }
2172 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2173 }
2174}
2175
2176/**
Eric Moored5d135b2009-05-18 13:02:08 -06002177 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302178 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002179 *
2180 * Returns SUCCESS if command aborted else FAILED
2181 */
2182static int
Eric Moored5d135b2009-05-18 13:02:08 -06002183_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002184{
2185 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2186 struct MPT2SAS_DEVICE *sas_device_priv_data;
2187 u16 smid;
2188 u16 handle;
2189 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002190
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302191 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2192 "scmd(%p)\n", scmd);
2193 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002194
2195 sas_device_priv_data = scmd->device->hostdata;
2196 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302197 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2198 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002199 scmd->result = DID_NO_CONNECT << 16;
2200 scmd->scsi_done(scmd);
2201 r = SUCCESS;
2202 goto out;
2203 }
2204
2205 /* search for the command */
2206 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2207 if (!smid) {
2208 scmd->result = DID_RESET << 16;
2209 r = SUCCESS;
2210 goto out;
2211 }
2212
2213 /* for hidden raid components and volumes this is not supported */
2214 if (sas_device_priv_data->sas_target->flags &
2215 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2216 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2217 scmd->result = DID_RESET << 16;
2218 r = FAILED;
2219 goto out;
2220 }
2221
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302222 mpt2sas_halt_firmware(ioc);
2223
Eric Moore635374e2009-03-09 01:21:12 -06002224 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302225 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2226 scmd->device->id, scmd->device->lun,
2227 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002228
2229 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302230 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2231 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002232 return r;
2233}
2234
Eric Moore635374e2009-03-09 01:21:12 -06002235/**
Eric Moored5d135b2009-05-18 13:02:08 -06002236 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302237 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002238 *
2239 * Returns SUCCESS if command aborted else FAILED
2240 */
2241static int
Eric Moored5d135b2009-05-18 13:02:08 -06002242_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002243{
2244 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2245 struct MPT2SAS_DEVICE *sas_device_priv_data;
2246 struct _sas_device *sas_device;
2247 unsigned long flags;
2248 u16 handle;
2249 int r;
2250
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302251 struct scsi_target *starget = scmd->device->sdev_target;
2252
2253 starget_printk(KERN_INFO, starget, "attempting target reset! "
2254 "scmd(%p)\n", scmd);
2255 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002256
2257 sas_device_priv_data = scmd->device->hostdata;
2258 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302259 starget_printk(KERN_INFO, starget, "target been deleted! "
2260 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002261 scmd->result = DID_NO_CONNECT << 16;
2262 scmd->scsi_done(scmd);
2263 r = SUCCESS;
2264 goto out;
2265 }
2266
2267 /* for hidden raid components obtain the volume_handle */
2268 handle = 0;
2269 if (sas_device_priv_data->sas_target->flags &
2270 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2271 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2272 sas_device = _scsih_sas_device_find_by_handle(ioc,
2273 sas_device_priv_data->sas_target->handle);
2274 if (sas_device)
2275 handle = sas_device->volume_handle;
2276 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2277 } else
2278 handle = sas_device_priv_data->sas_target->handle;
2279
2280 if (!handle) {
2281 scmd->result = DID_RESET << 16;
2282 r = FAILED;
2283 goto out;
2284 }
2285
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302286 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2287 scmd->device->id, scmd->device->lun,
2288 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002289
2290 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302291 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2292 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002293 return r;
2294}
2295
2296/**
Eric Moored5d135b2009-05-18 13:02:08 -06002297 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302298 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002299 *
2300 * Returns SUCCESS if command aborted else FAILED
2301 */
2302static int
Eric Moored5d135b2009-05-18 13:02:08 -06002303_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002304{
2305 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2306 struct MPT2SAS_DEVICE *sas_device_priv_data;
2307 struct _sas_device *sas_device;
2308 unsigned long flags;
2309 u16 handle;
2310 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302311 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002312
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302313 starget_printk(KERN_INFO, starget, "attempting target reset! "
2314 "scmd(%p)\n", scmd);
2315 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002316
2317 sas_device_priv_data = scmd->device->hostdata;
2318 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302319 starget_printk(KERN_INFO, starget, "target been deleted! "
2320 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002321 scmd->result = DID_NO_CONNECT << 16;
2322 scmd->scsi_done(scmd);
2323 r = SUCCESS;
2324 goto out;
2325 }
2326
2327 /* for hidden raid components obtain the volume_handle */
2328 handle = 0;
2329 if (sas_device_priv_data->sas_target->flags &
2330 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2331 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2332 sas_device = _scsih_sas_device_find_by_handle(ioc,
2333 sas_device_priv_data->sas_target->handle);
2334 if (sas_device)
2335 handle = sas_device->volume_handle;
2336 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2337 } else
2338 handle = sas_device_priv_data->sas_target->handle;
2339
2340 if (!handle) {
2341 scmd->result = DID_RESET << 16;
2342 r = FAILED;
2343 goto out;
2344 }
2345
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302346 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2347 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2348 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002349
2350 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302351 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2352 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002353 return r;
2354}
2355
2356/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302357 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302358 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002359 *
2360 * Returns SUCCESS if command aborted else FAILED
2361 */
2362static int
Eric Moored5d135b2009-05-18 13:02:08 -06002363_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002364{
2365 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2366 int r, retval;
2367
2368 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2369 ioc->name, scmd);
2370 scsi_print_command(scmd);
2371
2372 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2373 FORCE_BIG_HAMMER);
2374 r = (retval < 0) ? FAILED : SUCCESS;
2375 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2376 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2377
2378 return r;
2379}
2380
2381/**
2382 * _scsih_fw_event_add - insert and queue up fw_event
2383 * @ioc: per adapter object
2384 * @fw_event: object describing the event
2385 * Context: This function will acquire ioc->fw_event_lock.
2386 *
2387 * This adds the firmware event object into link list, then queues it up to
2388 * be processed from user context.
2389 *
2390 * Return nothing.
2391 */
2392static void
2393_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2394{
2395 unsigned long flags;
2396
2397 if (ioc->firmware_event_thread == NULL)
2398 return;
2399
2400 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2401 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302402 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2403 queue_delayed_work(ioc->firmware_event_thread,
2404 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002405 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2406}
2407
2408/**
2409 * _scsih_fw_event_free - delete fw_event
2410 * @ioc: per adapter object
2411 * @fw_event: object describing the event
2412 * Context: This function will acquire ioc->fw_event_lock.
2413 *
2414 * This removes firmware event object from link list, frees associated memory.
2415 *
2416 * Return nothing.
2417 */
2418static void
2419_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2420 *fw_event)
2421{
2422 unsigned long flags;
2423
2424 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2425 list_del(&fw_event->list);
2426 kfree(fw_event->event_data);
2427 kfree(fw_event);
2428 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2429}
2430
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302431
Eric Moore635374e2009-03-09 01:21:12 -06002432/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302433 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002434 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002435 *
2436 * Return nothing.
2437 */
2438static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302439_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002440{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302441 struct fw_event_work *fw_event;
2442
2443 if (ioc->wait_for_port_enable_to_complete)
2444 return;
2445 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2446 if (!fw_event)
2447 return;
2448 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2449 fw_event->ioc = ioc;
2450 _scsih_fw_event_add(ioc, fw_event);
2451}
2452
2453/**
2454 * _scsih_fw_event_cleanup_queue - cleanup event queue
2455 * @ioc: per adapter object
2456 *
2457 * Walk the firmware event queue, either killing timers, or waiting
2458 * for outstanding events to complete
2459 *
2460 * Return nothing.
2461 */
2462static void
2463_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2464{
2465 struct fw_event_work *fw_event, *next;
2466
2467 if (list_empty(&ioc->fw_event_list) ||
2468 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002469 return;
2470
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302471 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2472 if (cancel_delayed_work(&fw_event->delayed_work)) {
2473 _scsih_fw_event_free(ioc, fw_event);
2474 continue;
2475 }
2476 fw_event->cancel_pending_work = 1;
2477 }
Eric Moore635374e2009-03-09 01:21:12 -06002478}
2479
Eric Moore635374e2009-03-09 01:21:12 -06002480/**
2481 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2482 * @ioc: per adapter object
2483 * @handle: device handle
2484 *
2485 * During device pull we need to appropiately set the sdev state.
2486 */
2487static void
2488_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2489{
2490 struct MPT2SAS_DEVICE *sas_device_priv_data;
2491 struct scsi_device *sdev;
2492
2493 shost_for_each_device(sdev, ioc->shost) {
2494 sas_device_priv_data = sdev->hostdata;
2495 if (!sas_device_priv_data)
2496 continue;
2497 if (!sas_device_priv_data->block)
2498 continue;
2499 if (sas_device_priv_data->sas_target->handle == handle) {
2500 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2501 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2502 "handle(0x%04x)\n", ioc->name, handle));
2503 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302504 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002505 }
2506 }
2507}
2508
2509/**
2510 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2511 * @ioc: per adapter object
2512 * @handle: device handle
2513 *
2514 * During device pull we need to appropiately set the sdev state.
2515 */
2516static void
2517_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2518{
2519 struct MPT2SAS_DEVICE *sas_device_priv_data;
2520 struct scsi_device *sdev;
2521
2522 shost_for_each_device(sdev, ioc->shost) {
2523 sas_device_priv_data = sdev->hostdata;
2524 if (!sas_device_priv_data)
2525 continue;
2526 if (sas_device_priv_data->block)
2527 continue;
2528 if (sas_device_priv_data->sas_target->handle == handle) {
2529 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2530 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2531 "handle(0x%04x)\n", ioc->name, handle));
2532 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302533 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002534 }
2535 }
2536}
2537
2538/**
2539 * _scsih_block_io_to_children_attached_to_ex
2540 * @ioc: per adapter object
2541 * @sas_expander: the sas_device object
2542 *
2543 * This routine set sdev state to SDEV_BLOCK for all devices
2544 * attached to this expander. This function called when expander is
2545 * pulled.
2546 */
2547static void
2548_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2549 struct _sas_node *sas_expander)
2550{
2551 struct _sas_port *mpt2sas_port;
2552 struct _sas_device *sas_device;
2553 struct _sas_node *expander_sibling;
2554 unsigned long flags;
2555
2556 if (!sas_expander)
2557 return;
2558
2559 list_for_each_entry(mpt2sas_port,
2560 &sas_expander->sas_port_list, port_list) {
2561 if (mpt2sas_port->remote_identify.device_type ==
2562 SAS_END_DEVICE) {
2563 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2564 sas_device =
2565 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2566 mpt2sas_port->remote_identify.sas_address);
2567 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2568 if (!sas_device)
2569 continue;
2570 _scsih_block_io_device(ioc, sas_device->handle);
2571 }
2572 }
2573
2574 list_for_each_entry(mpt2sas_port,
2575 &sas_expander->sas_port_list, port_list) {
2576
2577 if (mpt2sas_port->remote_identify.device_type ==
2578 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2579 mpt2sas_port->remote_identify.device_type ==
2580 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2581
2582 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2583 expander_sibling =
2584 mpt2sas_scsih_expander_find_by_sas_address(
2585 ioc, mpt2sas_port->remote_identify.sas_address);
2586 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2587 _scsih_block_io_to_children_attached_to_ex(ioc,
2588 expander_sibling);
2589 }
2590 }
2591}
2592
2593/**
2594 * _scsih_block_io_to_children_attached_directly
2595 * @ioc: per adapter object
2596 * @event_data: topology change event data
2597 *
2598 * This routine set sdev state to SDEV_BLOCK for all devices
2599 * direct attached during device pull.
2600 */
2601static void
2602_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2603 Mpi2EventDataSasTopologyChangeList_t *event_data)
2604{
2605 int i;
2606 u16 handle;
2607 u16 reason_code;
2608 u8 phy_number;
2609
2610 for (i = 0; i < event_data->NumEntries; i++) {
2611 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2612 if (!handle)
2613 continue;
2614 phy_number = event_data->StartPhyNum + i;
2615 reason_code = event_data->PHY[i].PhyStatus &
2616 MPI2_EVENT_SAS_TOPO_RC_MASK;
2617 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2618 _scsih_block_io_device(ioc, handle);
2619 }
2620}
2621
2622/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302623 * _scsih_tm_tr_send - send task management request
2624 * @ioc: per adapter object
2625 * @handle: device handle
2626 * Context: interrupt time.
2627 *
2628 * This code is to initiate the device removal handshake protocal
2629 * with controller firmware. This function will issue target reset
2630 * using high priority request queue. It will send a sas iounit
2631 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2632 *
2633 * This is designed to send muliple task management request at the same
2634 * time to the fifo. If the fifo is full, we will append the request,
2635 * and process it in a future completion.
2636 */
2637static void
2638_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2639{
2640 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302641 u16 smid;
2642 struct _sas_device *sas_device;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302643 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302644 unsigned long flags;
2645 struct _tr_list *delayed_tr;
2646
Kashyap, Desai1278b112010-03-09 17:34:13 +05302647 if (ioc->shost_recovery || ioc->remove_host) {
2648 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2649 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302650 return;
2651 }
2652
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302653 /* if PD, then return */
2654 if (test_bit(handle, ioc->pd_handles))
2655 return;
2656
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302657 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2658 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302659 if (sas_device && sas_device->starget &&
2660 sas_device->starget->hostdata) {
2661 sas_target_priv_data = sas_device->starget->hostdata;
2662 sas_target_priv_data->deleted = 1;
2663 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2664 "setting delete flag: handle(0x%04x), "
2665 "sas_addr(0x%016llx)\n", ioc->name, handle,
2666 (unsigned long long) sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05302667 }
2668 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302669
2670 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2671 if (!smid) {
2672 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2673 if (!delayed_tr)
2674 return;
2675 INIT_LIST_HEAD(&delayed_tr->list);
2676 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302677 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2678 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2679 "DELAYED:tr:handle(0x%04x), (open)\n",
2680 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302681 return;
2682 }
2683
Kashyap, Desai1278b112010-03-09 17:34:13 +05302684 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2685 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2686 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302687 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2688 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2689 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2690 mpi_request->DevHandle = cpu_to_le16(handle);
2691 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302692 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2693}
2694
2695
2696
2697/**
2698 * _scsih_sas_control_complete - completion routine
2699 * @ioc: per adapter object
2700 * @smid: system request message index
2701 * @msix_index: MSIX table index supplied by the OS
2702 * @reply: reply message frame(lower 32bit addr)
2703 * Context: interrupt time.
2704 *
2705 * This is the sas iounit controll completion routine.
2706 * This code is part of the code to initiate the device removal
2707 * handshake protocal with controller firmware.
2708 *
2709 * Return 1 meaning mf should be freed from _base_interrupt
2710 * 0 means the mf is freed from this function.
2711 */
2712static u8
2713_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2714 u8 msix_index, u32 reply)
2715{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302716 Mpi2SasIoUnitControlReply_t *mpi_reply =
2717 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2718
Kashyap, Desai1278b112010-03-09 17:34:13 +05302719 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2720 "sc_complete:handle(0x%04x), (open) "
2721 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2722 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2723 le16_to_cpu(mpi_reply->IOCStatus),
2724 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302725 return 1;
2726}
2727
2728/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302729 * _scsih_tm_tr_volume_send - send target reset request for volumes
2730 * @ioc: per adapter object
2731 * @handle: device handle
2732 * Context: interrupt time.
2733 *
2734 * This is designed to send muliple task management request at the same
2735 * time to the fifo. If the fifo is full, we will append the request,
2736 * and process it in a future completion.
2737 */
2738static void
2739_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2740{
2741 Mpi2SCSITaskManagementRequest_t *mpi_request;
2742 u16 smid;
2743 struct _tr_list *delayed_tr;
2744
2745 if (ioc->shost_recovery || ioc->remove_host) {
2746 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2747 "progress!\n", __func__, ioc->name));
2748 return;
2749 }
2750
2751 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2752 if (!smid) {
2753 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2754 if (!delayed_tr)
2755 return;
2756 INIT_LIST_HEAD(&delayed_tr->list);
2757 delayed_tr->handle = handle;
2758 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2759 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2760 "DELAYED:tr:handle(0x%04x), (open)\n",
2761 ioc->name, handle));
2762 return;
2763 }
2764
2765 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2766 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2767 ioc->tm_tr_volume_cb_idx));
2768 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2769 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2770 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2771 mpi_request->DevHandle = cpu_to_le16(handle);
2772 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2773 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2774}
2775
2776/**
2777 * _scsih_tm_volume_tr_complete - target reset completion
2778 * @ioc: per adapter object
2779 * @smid: system request message index
2780 * @msix_index: MSIX table index supplied by the OS
2781 * @reply: reply message frame(lower 32bit addr)
2782 * Context: interrupt time.
2783 *
2784 * Return 1 meaning mf should be freed from _base_interrupt
2785 * 0 means the mf is freed from this function.
2786 */
2787static u8
2788_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2789 u8 msix_index, u32 reply)
2790{
2791 u16 handle;
2792 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2793 Mpi2SCSITaskManagementReply_t *mpi_reply =
2794 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2795
2796 if (ioc->shost_recovery || ioc->remove_host) {
2797 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2798 "progress!\n", __func__, ioc->name));
2799 return 1;
2800 }
2801
2802 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2803 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2804 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2805 dewtprintk(ioc, printk("spurious interrupt: "
2806 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2807 le16_to_cpu(mpi_reply->DevHandle), smid));
2808 return 0;
2809 }
2810
2811 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2812 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2813 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2814 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2815 le32_to_cpu(mpi_reply->IOCLogInfo),
2816 le32_to_cpu(mpi_reply->TerminationCount)));
2817
2818 return _scsih_check_for_pending_tm(ioc, smid);
2819}
2820
2821/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302822 * _scsih_tm_tr_complete -
2823 * @ioc: per adapter object
2824 * @smid: system request message index
2825 * @msix_index: MSIX table index supplied by the OS
2826 * @reply: reply message frame(lower 32bit addr)
2827 * Context: interrupt time.
2828 *
2829 * This is the target reset completion routine.
2830 * This code is part of the code to initiate the device removal
2831 * handshake protocal with controller firmware.
2832 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2833 *
2834 * Return 1 meaning mf should be freed from _base_interrupt
2835 * 0 means the mf is freed from this function.
2836 */
2837static u8
2838_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2839 u32 reply)
2840{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302841 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302842 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302843 Mpi2SCSITaskManagementReply_t *mpi_reply =
2844 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2845 Mpi2SasIoUnitControlRequest_t *mpi_request;
2846 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302847
Kashyap, Desai1278b112010-03-09 17:34:13 +05302848 if (ioc->shost_recovery || ioc->remove_host) {
2849 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2850 "progress!\n", __func__, ioc->name));
2851 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302852 }
2853
Kashyap, Desai1278b112010-03-09 17:34:13 +05302854 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2855 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2856 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2857 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2858 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2859 le16_to_cpu(mpi_reply->DevHandle), smid));
2860 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302861 }
2862
Kashyap, Desai1278b112010-03-09 17:34:13 +05302863 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2864 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2865 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2866 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2867 le32_to_cpu(mpi_reply->IOCLogInfo),
2868 le32_to_cpu(mpi_reply->TerminationCount)));
2869
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302870 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2871 if (!smid_sas_ctrl) {
2872 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2873 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302874 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302875 }
2876
Kashyap, Desai1278b112010-03-09 17:34:13 +05302877 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2878 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2879 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302880 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2881 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2882 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2883 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302884 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302885 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302886
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302887 return _scsih_check_for_pending_tm(ioc, smid);
2888}
2889
2890/**
2891 * _scsih_check_for_pending_tm - check for pending task management
2892 * @ioc: per adapter object
2893 * @smid: system request message index
2894 *
2895 * This will check delayed target reset list, and feed the
2896 * next reqeust.
2897 *
2898 * Return 1 meaning mf should be freed from _base_interrupt
2899 * 0 means the mf is freed from this function.
2900 */
2901static u8
2902_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2903{
2904 struct _tr_list *delayed_tr;
2905
2906 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2907 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2908 struct _tr_list, list);
2909 mpt2sas_base_free_smid(ioc, smid);
2910 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2911 list_del(&delayed_tr->list);
2912 kfree(delayed_tr);
2913 return 0;
2914 }
2915
Kashyap, Desai1278b112010-03-09 17:34:13 +05302916 if (!list_empty(&ioc->delayed_tr_list)) {
2917 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2918 struct _tr_list, list);
2919 mpt2sas_base_free_smid(ioc, smid);
2920 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2921 list_del(&delayed_tr->list);
2922 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302923 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302924 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302925
Kashyap, Desai1278b112010-03-09 17:34:13 +05302926 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302927}
2928
2929/**
Eric Moore635374e2009-03-09 01:21:12 -06002930 * _scsih_check_topo_delete_events - sanity check on topo events
2931 * @ioc: per adapter object
2932 * @event_data: the event data payload
2933 *
2934 * This routine added to better handle cable breaker.
2935 *
2936 * This handles the case where driver recieves multiple expander
2937 * add and delete events in a single shot. When there is a delete event
2938 * the routine will void any pending add events waiting in the event queue.
2939 *
2940 * Return nothing.
2941 */
2942static void
2943_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2944 Mpi2EventDataSasTopologyChangeList_t *event_data)
2945{
2946 struct fw_event_work *fw_event;
2947 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2948 u16 expander_handle;
2949 struct _sas_node *sas_expander;
2950 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302951 int i, reason_code;
2952 u16 handle;
2953
2954 for (i = 0 ; i < event_data->NumEntries; i++) {
2955 if (event_data->PHY[i].PhyStatus &
2956 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2957 continue;
2958 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2959 if (!handle)
2960 continue;
2961 reason_code = event_data->PHY[i].PhyStatus &
2962 MPI2_EVENT_SAS_TOPO_RC_MASK;
2963 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2964 _scsih_tm_tr_send(ioc, handle);
2965 }
Eric Moore635374e2009-03-09 01:21:12 -06002966
2967 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2968 if (expander_handle < ioc->sas_hba.num_phys) {
2969 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2970 return;
2971 }
2972
2973 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2974 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2975 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2976 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2977 expander_handle);
2978 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2979 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2980 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2981 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2982
2983 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2984 return;
2985
2986 /* mark ignore flag for pending events */
2987 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2988 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2989 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2990 fw_event->ignore)
2991 continue;
2992 local_event_data = fw_event->event_data;
2993 if (local_event_data->ExpStatus ==
2994 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2995 local_event_data->ExpStatus ==
2996 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2997 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2998 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302999 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003000 "setting ignoring flag\n", ioc->name));
3001 fw_event->ignore = 1;
3002 }
3003 }
3004 }
3005 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3006}
3007
3008/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303009 * _scsih_set_volume_delete_flag - setting volume delete flag
3010 * @ioc: per adapter object
3011 * @handle: device handle
3012 *
3013 * This
3014 * Return nothing.
3015 */
3016static void
3017_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3018{
3019 struct _raid_device *raid_device;
3020 struct MPT2SAS_TARGET *sas_target_priv_data;
3021 unsigned long flags;
3022
3023 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3024 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3025 if (raid_device && raid_device->starget &&
3026 raid_device->starget->hostdata) {
3027 sas_target_priv_data =
3028 raid_device->starget->hostdata;
3029 sas_target_priv_data->deleted = 1;
3030 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3031 "setting delete flag: handle(0x%04x), "
3032 "wwid(0x%016llx)\n", ioc->name, handle,
3033 (unsigned long long) raid_device->wwid));
3034 }
3035 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3036}
3037
3038/**
3039 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3040 * @handle: input handle
3041 * @a: handle for volume a
3042 * @b: handle for volume b
3043 *
3044 * IR firmware only supports two raid volumes. The purpose of this
3045 * routine is to set the volume handle in either a or b. When the given
3046 * input handle is non-zero, or when a and b have not been set before.
3047 */
3048static void
3049_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3050{
3051 if (!handle || handle == *a || handle == *b)
3052 return;
3053 if (!*a)
3054 *a = handle;
3055 else if (!*b)
3056 *b = handle;
3057}
3058
3059/**
3060 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3061 * @ioc: per adapter object
3062 * @event_data: the event data payload
3063 * Context: interrupt time.
3064 *
3065 * This routine will send target reset to volume, followed by target
3066 * resets to the PDs. This is called when a PD has been removed, or
3067 * volume has been deleted or removed. When the target reset is sent
3068 * to volume, the PD target resets need to be queued to start upon
3069 * completion of the volume target reset.
3070 *
3071 * Return nothing.
3072 */
3073static void
3074_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3075 Mpi2EventDataIrConfigChangeList_t *event_data)
3076{
3077 Mpi2EventIrConfigElement_t *element;
3078 int i;
3079 u16 handle, volume_handle, a, b;
3080 struct _tr_list *delayed_tr;
3081
3082 a = 0;
3083 b = 0;
3084
3085 /* Volume Resets for Deleted or Removed */
3086 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3087 for (i = 0; i < event_data->NumElements; i++, element++) {
3088 if (element->ReasonCode ==
3089 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3090 element->ReasonCode ==
3091 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3092 volume_handle = le16_to_cpu(element->VolDevHandle);
3093 _scsih_set_volume_delete_flag(ioc, volume_handle);
3094 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3095 }
3096 }
3097
3098 /* Volume Resets for UNHIDE events */
3099 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3100 for (i = 0; i < event_data->NumElements; i++, element++) {
3101 if (le32_to_cpu(event_data->Flags) &
3102 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3103 continue;
3104 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3105 volume_handle = le16_to_cpu(element->VolDevHandle);
3106 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3107 }
3108 }
3109
3110 if (a)
3111 _scsih_tm_tr_volume_send(ioc, a);
3112 if (b)
3113 _scsih_tm_tr_volume_send(ioc, b);
3114
3115 /* PD target resets */
3116 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3117 for (i = 0; i < event_data->NumElements; i++, element++) {
3118 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3119 continue;
3120 handle = le16_to_cpu(element->PhysDiskDevHandle);
3121 volume_handle = le16_to_cpu(element->VolDevHandle);
3122 clear_bit(handle, ioc->pd_handles);
3123 if (!volume_handle)
3124 _scsih_tm_tr_send(ioc, handle);
3125 else if (volume_handle == a || volume_handle == b) {
3126 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3127 BUG_ON(!delayed_tr);
3128 INIT_LIST_HEAD(&delayed_tr->list);
3129 delayed_tr->handle = handle;
3130 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3131 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3132 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3133 handle));
3134 } else
3135 _scsih_tm_tr_send(ioc, handle);
3136 }
3137}
3138
3139
3140/**
3141 * _scsih_check_volume_delete_events - set delete flag for volumes
3142 * @ioc: per adapter object
3143 * @event_data: the event data payload
3144 * Context: interrupt time.
3145 *
3146 * This will handle the case when the cable connected to entire volume is
3147 * pulled. We will take care of setting the deleted flag so normal IO will
3148 * not be sent.
3149 *
3150 * Return nothing.
3151 */
3152static void
3153_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3154 Mpi2EventDataIrVolume_t *event_data)
3155{
3156 u32 state;
3157
3158 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3159 return;
3160 state = le32_to_cpu(event_data->NewValue);
3161 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3162 MPI2_RAID_VOL_STATE_FAILED)
3163 _scsih_set_volume_delete_flag(ioc,
3164 le16_to_cpu(event_data->VolDevHandle));
3165}
3166
3167/**
Eric Moore635374e2009-03-09 01:21:12 -06003168 * _scsih_flush_running_cmds - completing outstanding commands.
3169 * @ioc: per adapter object
3170 *
3171 * The flushing out of all pending scmd commands following host reset,
3172 * where all IO is dropped to the floor.
3173 *
3174 * Return nothing.
3175 */
3176static void
3177_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3178{
3179 struct scsi_cmnd *scmd;
3180 u16 smid;
3181 u16 count = 0;
3182
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303183 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3184 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003185 if (!scmd)
3186 continue;
3187 count++;
3188 mpt2sas_base_free_smid(ioc, smid);
3189 scsi_dma_unmap(scmd);
3190 scmd->result = DID_RESET << 16;
3191 scmd->scsi_done(scmd);
3192 }
3193 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3194 ioc->name, count));
3195}
3196
3197/**
Eric Moore3c621b32009-05-18 12:59:41 -06003198 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3199 * @scmd: pointer to scsi command object
3200 * @mpi_request: pointer to the SCSI_IO reqest message frame
3201 *
3202 * Supporting protection 1 and 3.
3203 *
3204 * Returns nothing
3205 */
3206static void
3207_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3208{
3209 u16 eedp_flags;
3210 unsigned char prot_op = scsi_get_prot_op(scmd);
3211 unsigned char prot_type = scsi_get_prot_type(scmd);
3212
Eric Moored334aa72010-04-22 10:47:40 -06003213 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003214 return;
3215
3216 if (prot_op == SCSI_PROT_READ_STRIP)
3217 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3218 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3219 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3220 else
3221 return;
3222
Eric Moore3c621b32009-05-18 12:59:41 -06003223 switch (prot_type) {
3224 case SCSI_PROT_DIF_TYPE1:
3225
3226 /*
3227 * enable ref/guard checking
3228 * auto increment ref tag
3229 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303230 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003231 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3232 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3233 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3234 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003235 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003236
Eric Moored334aa72010-04-22 10:47:40 -06003237 case SCSI_PROT_DIF_TYPE2:
3238
3239 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3240 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3241 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003242 break;
3243
3244 case SCSI_PROT_DIF_TYPE3:
3245
3246 /*
3247 * enable guard checking
3248 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303249 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003250 break;
3251 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303252 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3253 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003254}
3255
3256/**
3257 * _scsih_eedp_error_handling - return sense code for EEDP errors
3258 * @scmd: pointer to scsi command object
3259 * @ioc_status: ioc status
3260 *
3261 * Returns nothing
3262 */
3263static void
3264_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3265{
3266 u8 ascq;
3267 u8 sk;
3268 u8 host_byte;
3269
3270 switch (ioc_status) {
3271 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3272 ascq = 0x01;
3273 break;
3274 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3275 ascq = 0x02;
3276 break;
3277 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3278 ascq = 0x03;
3279 break;
3280 default:
3281 ascq = 0x00;
3282 break;
3283 }
3284
3285 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3286 sk = ILLEGAL_REQUEST;
3287 host_byte = DID_ABORT;
3288 } else {
3289 sk = ABORTED_COMMAND;
3290 host_byte = DID_OK;
3291 }
3292
3293 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3294 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3295 SAM_STAT_CHECK_CONDITION;
3296}
3297
3298/**
Eric Moored5d135b2009-05-18 13:02:08 -06003299 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003300 * @scmd: pointer to scsi command object
3301 * @done: function pointer to be invoked on completion
3302 *
3303 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3304 *
3305 * Returns 0 on success. If there's a failure, return either:
3306 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3307 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3308 */
3309static int
Eric Moored5d135b2009-05-18 13:02:08 -06003310_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003311{
3312 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3313 struct MPT2SAS_DEVICE *sas_device_priv_data;
3314 struct MPT2SAS_TARGET *sas_target_priv_data;
3315 Mpi2SCSIIORequest_t *mpi_request;
3316 u32 mpi_control;
3317 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003318
3319 scmd->scsi_done = done;
3320 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303321 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003322 scmd->result = DID_NO_CONNECT << 16;
3323 scmd->scsi_done(scmd);
3324 return 0;
3325 }
3326
3327 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303328 /* invalid device handle */
3329 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003330 scmd->result = DID_NO_CONNECT << 16;
3331 scmd->scsi_done(scmd);
3332 return 0;
3333 }
3334
Kashyap, Desai130b9582010-04-08 17:54:32 +05303335 /* host recovery or link resets sent via IOCTLs */
3336 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003337 return SCSI_MLQUEUE_HOST_BUSY;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303338 /* device busy with task managment */
3339 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3340 return SCSI_MLQUEUE_DEVICE_BUSY;
3341 /* device has been deleted */
3342 else if (sas_target_priv_data->deleted) {
3343 scmd->result = DID_NO_CONNECT << 16;
3344 scmd->scsi_done(scmd);
3345 return 0;
3346 }
Eric Moore635374e2009-03-09 01:21:12 -06003347
3348 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3349 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3350 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3351 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3352 else
3353 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3354
3355 /* set tags */
3356 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3357 if (scmd->device->tagged_supported) {
3358 if (scmd->device->ordered_tags)
3359 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3360 else
3361 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3362 } else
3363/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3364/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3365 */
3366 mpi_control |= (0x500);
3367
3368 } else
3369 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303370 /* Make sure Device is not raid volume */
3371 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003372 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003373 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3374
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303375 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003376 if (!smid) {
3377 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3378 ioc->name, __func__);
3379 goto out;
3380 }
3381 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3382 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003383 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003384 if (scmd->cmd_len == 32)
3385 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003386 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3387 if (sas_device_priv_data->sas_target->flags &
3388 MPT_TARGET_FLAGS_RAID_COMPONENT)
3389 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3390 else
3391 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3392 mpi_request->DevHandle =
3393 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3394 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3395 mpi_request->Control = cpu_to_le32(mpi_control);
3396 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3397 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3398 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3399 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303400 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003401 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3402 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3403 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303404 mpi_request->VF_ID = 0; /* TODO */
3405 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003406 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3407 mpi_request->LUN);
3408 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3409
3410 if (!mpi_request->DataLength) {
3411 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3412 } else {
3413 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3414 mpt2sas_base_free_smid(ioc, smid);
3415 goto out;
3416 }
3417 }
3418
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303419 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3420 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3421 sas_device_priv_data->sas_target->handle);
3422 else
3423 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003424 return 0;
3425
3426 out:
3427 return SCSI_MLQUEUE_HOST_BUSY;
3428}
3429
3430/**
3431 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3432 * @sense_buffer: sense data returned by target
3433 * @data: normalized skey/asc/ascq
3434 *
3435 * Return nothing.
3436 */
3437static void
3438_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3439{
3440 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3441 /* descriptor format */
3442 data->skey = sense_buffer[1] & 0x0F;
3443 data->asc = sense_buffer[2];
3444 data->ascq = sense_buffer[3];
3445 } else {
3446 /* fixed format */
3447 data->skey = sense_buffer[2] & 0x0F;
3448 data->asc = sense_buffer[12];
3449 data->ascq = sense_buffer[13];
3450 }
3451}
3452
3453#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3454/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003455 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003456 * @ioc: per adapter object
3457 * @scmd: pointer to scsi command object
3458 * @mpi_reply: reply mf payload returned from firmware
3459 *
3460 * scsi_status - SCSI Status code returned from target device
3461 * scsi_state - state info associated with SCSI_IO determined by ioc
3462 * ioc_status - ioc supplied status info
3463 *
3464 * Return nothing.
3465 */
3466static void
3467_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3468 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3469{
3470 u32 response_info;
3471 u8 *response_bytes;
3472 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3473 MPI2_IOCSTATUS_MASK;
3474 u8 scsi_state = mpi_reply->SCSIState;
3475 u8 scsi_status = mpi_reply->SCSIStatus;
3476 char *desc_ioc_state = NULL;
3477 char *desc_scsi_status = NULL;
3478 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303479 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303480 struct _sas_device *sas_device = NULL;
3481 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303482 struct scsi_target *starget = scmd->device->sdev_target;
3483 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
3484
3485 if (!priv_target)
3486 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303487
3488 if (log_info == 0x31170000)
3489 return;
Eric Moore635374e2009-03-09 01:21:12 -06003490
3491 switch (ioc_status) {
3492 case MPI2_IOCSTATUS_SUCCESS:
3493 desc_ioc_state = "success";
3494 break;
3495 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3496 desc_ioc_state = "invalid function";
3497 break;
3498 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3499 desc_ioc_state = "scsi recovered error";
3500 break;
3501 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3502 desc_ioc_state = "scsi invalid dev handle";
3503 break;
3504 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3505 desc_ioc_state = "scsi device not there";
3506 break;
3507 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3508 desc_ioc_state = "scsi data overrun";
3509 break;
3510 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3511 desc_ioc_state = "scsi data underrun";
3512 break;
3513 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3514 desc_ioc_state = "scsi io data error";
3515 break;
3516 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3517 desc_ioc_state = "scsi protocol error";
3518 break;
3519 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3520 desc_ioc_state = "scsi task terminated";
3521 break;
3522 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3523 desc_ioc_state = "scsi residual mismatch";
3524 break;
3525 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3526 desc_ioc_state = "scsi task mgmt failed";
3527 break;
3528 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3529 desc_ioc_state = "scsi ioc terminated";
3530 break;
3531 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3532 desc_ioc_state = "scsi ext terminated";
3533 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003534 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3535 desc_ioc_state = "eedp guard error";
3536 break;
3537 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3538 desc_ioc_state = "eedp ref tag error";
3539 break;
3540 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3541 desc_ioc_state = "eedp app tag error";
3542 break;
Eric Moore635374e2009-03-09 01:21:12 -06003543 default:
3544 desc_ioc_state = "unknown";
3545 break;
3546 }
3547
3548 switch (scsi_status) {
3549 case MPI2_SCSI_STATUS_GOOD:
3550 desc_scsi_status = "good";
3551 break;
3552 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3553 desc_scsi_status = "check condition";
3554 break;
3555 case MPI2_SCSI_STATUS_CONDITION_MET:
3556 desc_scsi_status = "condition met";
3557 break;
3558 case MPI2_SCSI_STATUS_BUSY:
3559 desc_scsi_status = "busy";
3560 break;
3561 case MPI2_SCSI_STATUS_INTERMEDIATE:
3562 desc_scsi_status = "intermediate";
3563 break;
3564 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3565 desc_scsi_status = "intermediate condmet";
3566 break;
3567 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3568 desc_scsi_status = "reservation conflict";
3569 break;
3570 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3571 desc_scsi_status = "command terminated";
3572 break;
3573 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3574 desc_scsi_status = "task set full";
3575 break;
3576 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3577 desc_scsi_status = "aca active";
3578 break;
3579 case MPI2_SCSI_STATUS_TASK_ABORTED:
3580 desc_scsi_status = "task aborted";
3581 break;
3582 default:
3583 desc_scsi_status = "unknown";
3584 break;
3585 }
3586
3587 desc_scsi_state[0] = '\0';
3588 if (!scsi_state)
3589 desc_scsi_state = " ";
3590 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3591 strcat(desc_scsi_state, "response info ");
3592 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3593 strcat(desc_scsi_state, "state terminated ");
3594 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3595 strcat(desc_scsi_state, "no status ");
3596 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3597 strcat(desc_scsi_state, "autosense failed ");
3598 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3599 strcat(desc_scsi_state, "autosense valid ");
3600
3601 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303602
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303603 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3604 printk(MPT2SAS_WARN_FMT "\tvolume wwid(0x%016llx)\n", ioc->name,
3605 (unsigned long long)priv_target->sas_address);
3606 } else {
3607 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3608 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3609 priv_target->sas_address);
3610 if (sas_device) {
3611 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
3612 "phy(%d)\n", ioc->name, sas_device->sas_address,
3613 sas_device->phy);
3614 printk(MPT2SAS_WARN_FMT
3615 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
3616 ioc->name, sas_device->enclosure_logical_id,
3617 sas_device->slot);
3618 }
3619 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303620 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303621
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303622 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
3623 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3624 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003625 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3626 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3627 scsi_get_resid(scmd));
3628 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3629 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3630 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3631 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3632 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3633 scsi_status, desc_scsi_state, scsi_state);
3634
3635 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3636 struct sense_info data;
3637 _scsih_normalize_sense(scmd->sense_buffer, &data);
3638 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303639 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3640 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06003641 }
3642
3643 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3644 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3645 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303646 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003647 }
3648}
3649#endif
3650
3651/**
3652 * _scsih_smart_predicted_fault - illuminate Fault LED
3653 * @ioc: per adapter object
3654 * @handle: device handle
3655 *
3656 * Return nothing.
3657 */
3658static void
3659_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3660{
3661 Mpi2SepReply_t mpi_reply;
3662 Mpi2SepRequest_t mpi_request;
3663 struct scsi_target *starget;
3664 struct MPT2SAS_TARGET *sas_target_priv_data;
3665 Mpi2EventNotificationReply_t *event_reply;
3666 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3667 struct _sas_device *sas_device;
3668 ssize_t sz;
3669 unsigned long flags;
3670
3671 /* only handle non-raid devices */
3672 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3673 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3674 if (!sas_device) {
3675 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3676 return;
3677 }
3678 starget = sas_device->starget;
3679 sas_target_priv_data = starget->hostdata;
3680
3681 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3682 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3683 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3684 return;
3685 }
3686 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3687 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3688
3689 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3690 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3691 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3692 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3693 mpi_request.SlotStatus =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303694 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
Eric Moore635374e2009-03-09 01:21:12 -06003695 mpi_request.DevHandle = cpu_to_le16(handle);
3696 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3697 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3698 &mpi_request)) != 0) {
3699 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3700 ioc->name, __FILE__, __LINE__, __func__);
3701 return;
3702 }
3703
3704 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3705 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3706 "enclosure_processor: ioc_status (0x%04x), "
3707 "loginfo(0x%08x)\n", ioc->name,
3708 le16_to_cpu(mpi_reply.IOCStatus),
3709 le32_to_cpu(mpi_reply.IOCLogInfo)));
3710 return;
3711 }
3712 }
3713
3714 /* insert into event log */
3715 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3716 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3717 event_reply = kzalloc(sz, GFP_KERNEL);
3718 if (!event_reply) {
3719 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3720 ioc->name, __FILE__, __LINE__, __func__);
3721 return;
3722 }
3723
3724 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3725 event_reply->Event =
3726 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3727 event_reply->MsgLength = sz/4;
3728 event_reply->EventDataLength =
3729 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3730 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3731 event_reply->EventData;
3732 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3733 event_data->ASC = 0x5D;
3734 event_data->DevHandle = cpu_to_le16(handle);
3735 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3736 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3737 kfree(event_reply);
3738}
3739
3740/**
Eric Moored5d135b2009-05-18 13:02:08 -06003741 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003742 * @ioc: per adapter object
3743 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303744 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003745 * @reply: reply message frame(lower 32bit addr)
3746 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303747 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003748 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303749 * Return 1 meaning mf should be freed from _base_interrupt
3750 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003751 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303752static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303753_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003754{
3755 Mpi2SCSIIORequest_t *mpi_request;
3756 Mpi2SCSIIOReply_t *mpi_reply;
3757 struct scsi_cmnd *scmd;
3758 u16 ioc_status;
3759 u32 xfer_cnt;
3760 u8 scsi_state;
3761 u8 scsi_status;
3762 u32 log_info;
3763 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303764 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003765
3766 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303767 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003768 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303769 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003770
3771 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3772
3773 if (mpi_reply == NULL) {
3774 scmd->result = DID_OK << 16;
3775 goto out;
3776 }
3777
3778 sas_device_priv_data = scmd->device->hostdata;
3779 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3780 sas_device_priv_data->sas_target->deleted) {
3781 scmd->result = DID_NO_CONNECT << 16;
3782 goto out;
3783 }
3784
3785 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303786 scsi_state = mpi_reply->SCSIState;
3787 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3788 response_code =
3789 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003790 if (!sas_device_priv_data->tlr_snoop_check) {
3791 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303792 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3793 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303794 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3795 sas_disable_tlr(scmd->device);
3796 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3797 }
Eric Moore635374e2009-03-09 01:21:12 -06003798 }
3799
3800 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3801 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3802 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3803 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3804 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3805 else
3806 log_info = 0;
3807 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003808 scsi_status = mpi_reply->SCSIStatus;
3809
3810 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3811 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3812 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3813 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3814 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3815 }
3816
3817 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3818 struct sense_info data;
3819 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3820 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003821 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003822 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003823 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003824 _scsih_normalize_sense(scmd->sense_buffer, &data);
3825 /* failure prediction threshold exceeded */
3826 if (data.asc == 0x5D)
3827 _scsih_smart_predicted_fault(ioc,
3828 le16_to_cpu(mpi_reply->DevHandle));
3829 }
3830
3831 switch (ioc_status) {
3832 case MPI2_IOCSTATUS_BUSY:
3833 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3834 scmd->result = SAM_STAT_BUSY;
3835 break;
3836
3837 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3838 scmd->result = DID_NO_CONNECT << 16;
3839 break;
3840
3841 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3842 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303843 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3844 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003845 }
Eric Moore635374e2009-03-09 01:21:12 -06003846 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3847 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3848 scmd->result = DID_RESET << 16;
3849 break;
3850
3851 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3852 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3853 scmd->result = DID_SOFT_ERROR << 16;
3854 else
3855 scmd->result = (DID_OK << 16) | scsi_status;
3856 break;
3857
3858 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3859 scmd->result = (DID_OK << 16) | scsi_status;
3860
3861 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3862 break;
3863
3864 if (xfer_cnt < scmd->underflow) {
3865 if (scsi_status == SAM_STAT_BUSY)
3866 scmd->result = SAM_STAT_BUSY;
3867 else
3868 scmd->result = DID_SOFT_ERROR << 16;
3869 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3870 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3871 scmd->result = DID_SOFT_ERROR << 16;
3872 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3873 scmd->result = DID_RESET << 16;
3874 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3875 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3876 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3877 scmd->result = (DRIVER_SENSE << 24) |
3878 SAM_STAT_CHECK_CONDITION;
3879 scmd->sense_buffer[0] = 0x70;
3880 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3881 scmd->sense_buffer[12] = 0x20;
3882 scmd->sense_buffer[13] = 0;
3883 }
3884 break;
3885
3886 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3887 scsi_set_resid(scmd, 0);
3888 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3889 case MPI2_IOCSTATUS_SUCCESS:
3890 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303891 if (response_code ==
3892 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3893 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3894 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003895 scmd->result = DID_SOFT_ERROR << 16;
3896 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3897 scmd->result = DID_RESET << 16;
3898 break;
3899
Eric Moore3c621b32009-05-18 12:59:41 -06003900 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3901 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3902 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3903 _scsih_eedp_error_handling(scmd, ioc_status);
3904 break;
Eric Moore635374e2009-03-09 01:21:12 -06003905 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3906 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3907 case MPI2_IOCSTATUS_INVALID_SGL:
3908 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3909 case MPI2_IOCSTATUS_INVALID_FIELD:
3910 case MPI2_IOCSTATUS_INVALID_STATE:
3911 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3912 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3913 default:
3914 scmd->result = DID_SOFT_ERROR << 16;
3915 break;
3916
3917 }
3918
3919#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3920 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3921 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3922#endif
3923
3924 out:
3925 scsi_dma_unmap(scmd);
3926 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303927 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003928}
3929
3930/**
Eric Moore635374e2009-03-09 01:21:12 -06003931 * _scsih_sas_host_refresh - refreshing sas host object contents
3932 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003933 * Context: user
3934 *
3935 * During port enable, fw will send topology events for every device. Its
3936 * possible that the handles may change from the previous setting, so this
3937 * code keeping handles updating if changed.
3938 *
3939 * Return nothing.
3940 */
3941static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303942_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003943{
3944 u16 sz;
3945 u16 ioc_status;
3946 int i;
3947 Mpi2ConfigReply_t mpi_reply;
3948 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303949 u16 attached_handle;
Eric Moore635374e2009-03-09 01:21:12 -06003950
3951 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3952 "updating handles for sas_host(0x%016llx)\n",
3953 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3954
3955 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3956 * sizeof(Mpi2SasIOUnit0PhyData_t));
3957 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3958 if (!sas_iounit_pg0) {
3959 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3960 ioc->name, __FILE__, __LINE__, __func__);
3961 return;
3962 }
Eric Moore635374e2009-03-09 01:21:12 -06003963
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303964 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3965 sas_iounit_pg0, sz)) != 0)
3966 goto out;
3967 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3968 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3969 goto out;
3970 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3971 if (i == 0)
3972 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3973 PhyData[0].ControllerDevHandle);
3974 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3975 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3976 AttachedDevHandle);
3977 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3978 attached_handle, i, sas_iounit_pg0->PhyData[i].
3979 NegotiatedLinkRate >> 4);
3980 }
Eric Moore635374e2009-03-09 01:21:12 -06003981 out:
3982 kfree(sas_iounit_pg0);
3983}
3984
3985/**
3986 * _scsih_sas_host_add - create sas host object
3987 * @ioc: per adapter object
3988 *
3989 * Creating host side data object, stored in ioc->sas_hba
3990 *
3991 * Return nothing.
3992 */
3993static void
3994_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3995{
3996 int i;
3997 Mpi2ConfigReply_t mpi_reply;
3998 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3999 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4000 Mpi2SasPhyPage0_t phy_pg0;
4001 Mpi2SasDevicePage0_t sas_device_pg0;
4002 Mpi2SasEnclosurePage0_t enclosure_pg0;
4003 u16 ioc_status;
4004 u16 sz;
4005 u16 device_missing_delay;
4006
4007 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4008 if (!ioc->sas_hba.num_phys) {
4009 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4010 ioc->name, __FILE__, __LINE__, __func__);
4011 return;
4012 }
4013
4014 /* sas_iounit page 0 */
4015 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4016 sizeof(Mpi2SasIOUnit0PhyData_t));
4017 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4018 if (!sas_iounit_pg0) {
4019 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4020 ioc->name, __FILE__, __LINE__, __func__);
4021 return;
4022 }
4023 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4024 sas_iounit_pg0, sz))) {
4025 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4026 ioc->name, __FILE__, __LINE__, __func__);
4027 goto out;
4028 }
4029 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4030 MPI2_IOCSTATUS_MASK;
4031 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4032 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4033 ioc->name, __FILE__, __LINE__, __func__);
4034 goto out;
4035 }
4036
4037 /* sas_iounit page 1 */
4038 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4039 sizeof(Mpi2SasIOUnit1PhyData_t));
4040 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4041 if (!sas_iounit_pg1) {
4042 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4043 ioc->name, __FILE__, __LINE__, __func__);
4044 goto out;
4045 }
4046 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4047 sas_iounit_pg1, sz))) {
4048 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4049 ioc->name, __FILE__, __LINE__, __func__);
4050 goto out;
4051 }
4052 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4053 MPI2_IOCSTATUS_MASK;
4054 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4055 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4056 ioc->name, __FILE__, __LINE__, __func__);
4057 goto out;
4058 }
4059
4060 ioc->io_missing_delay =
4061 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4062 device_missing_delay =
4063 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4064 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4065 ioc->device_missing_delay = (device_missing_delay &
4066 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4067 else
4068 ioc->device_missing_delay = device_missing_delay &
4069 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4070
4071 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4072 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4073 sizeof(struct _sas_phy), GFP_KERNEL);
4074 if (!ioc->sas_hba.phy) {
4075 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4076 ioc->name, __FILE__, __LINE__, __func__);
4077 goto out;
4078 }
4079 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4080 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4081 i))) {
4082 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4083 ioc->name, __FILE__, __LINE__, __func__);
4084 goto out;
4085 }
4086 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4087 MPI2_IOCSTATUS_MASK;
4088 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4089 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4090 ioc->name, __FILE__, __LINE__, __func__);
4091 goto out;
4092 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304093
4094 if (i == 0)
4095 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4096 PhyData[0].ControllerDevHandle);
4097 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004098 ioc->sas_hba.phy[i].phy_id = i;
4099 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4100 phy_pg0, ioc->sas_hba.parent_dev);
4101 }
4102 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304103 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004104 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4105 ioc->name, __FILE__, __LINE__, __func__);
4106 goto out;
4107 }
Eric Moore635374e2009-03-09 01:21:12 -06004108 ioc->sas_hba.enclosure_handle =
4109 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4110 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4111 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4112 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4113 (unsigned long long) ioc->sas_hba.sas_address,
4114 ioc->sas_hba.num_phys) ;
4115
4116 if (ioc->sas_hba.enclosure_handle) {
4117 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4118 &enclosure_pg0,
4119 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4120 ioc->sas_hba.enclosure_handle))) {
4121 ioc->sas_hba.enclosure_logical_id =
4122 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4123 }
4124 }
4125
4126 out:
4127 kfree(sas_iounit_pg1);
4128 kfree(sas_iounit_pg0);
4129}
4130
4131/**
4132 * _scsih_expander_add - creating expander object
4133 * @ioc: per adapter object
4134 * @handle: expander handle
4135 *
4136 * Creating expander object, stored in ioc->sas_expander_list.
4137 *
4138 * Return 0 for success, else error.
4139 */
4140static int
4141_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4142{
4143 struct _sas_node *sas_expander;
4144 Mpi2ConfigReply_t mpi_reply;
4145 Mpi2ExpanderPage0_t expander_pg0;
4146 Mpi2ExpanderPage1_t expander_pg1;
4147 Mpi2SasEnclosurePage0_t enclosure_pg0;
4148 u32 ioc_status;
4149 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304150 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004151 int i;
4152 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304153 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004154 int rc = 0;
4155
4156 if (!handle)
4157 return -1;
4158
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304159 if (ioc->shost_recovery)
4160 return -1;
4161
Eric Moore635374e2009-03-09 01:21:12 -06004162 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4163 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4164 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4165 ioc->name, __FILE__, __LINE__, __func__);
4166 return -1;
4167 }
4168
4169 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4170 MPI2_IOCSTATUS_MASK;
4171 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4172 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4173 ioc->name, __FILE__, __LINE__, __func__);
4174 return -1;
4175 }
4176
4177 /* handle out of order topology events */
4178 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304179 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4180 != 0) {
4181 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4182 ioc->name, __FILE__, __LINE__, __func__);
4183 return -1;
4184 }
4185 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004186 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304187 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4188 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004189 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4190 if (!sas_expander) {
4191 rc = _scsih_expander_add(ioc, parent_handle);
4192 if (rc != 0)
4193 return rc;
4194 }
4195 }
4196
Eric Moore635374e2009-03-09 01:21:12 -06004197 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304198 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004199 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4200 sas_address);
4201 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4202
4203 if (sas_expander)
4204 return 0;
4205
4206 sas_expander = kzalloc(sizeof(struct _sas_node),
4207 GFP_KERNEL);
4208 if (!sas_expander) {
4209 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4210 ioc->name, __FILE__, __LINE__, __func__);
4211 return -1;
4212 }
4213
4214 sas_expander->handle = handle;
4215 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304216 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004217 sas_expander->sas_address = sas_address;
4218
4219 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4220 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304221 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004222 sas_expander->sas_address, sas_expander->num_phys);
4223
4224 if (!sas_expander->num_phys)
4225 goto out_fail;
4226 sas_expander->phy = kcalloc(sas_expander->num_phys,
4227 sizeof(struct _sas_phy), GFP_KERNEL);
4228 if (!sas_expander->phy) {
4229 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4230 ioc->name, __FILE__, __LINE__, __func__);
4231 rc = -1;
4232 goto out_fail;
4233 }
4234
4235 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4236 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304237 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004238 if (!mpt2sas_port) {
4239 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4240 ioc->name, __FILE__, __LINE__, __func__);
4241 rc = -1;
4242 goto out_fail;
4243 }
4244 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4245
4246 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4247 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4248 &expander_pg1, i, handle))) {
4249 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4250 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304251 rc = -1;
4252 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004253 }
4254 sas_expander->phy[i].handle = handle;
4255 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304256
4257 if ((mpt2sas_transport_add_expander_phy(ioc,
4258 &sas_expander->phy[i], expander_pg1,
4259 sas_expander->parent_dev))) {
4260 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4261 ioc->name, __FILE__, __LINE__, __func__);
4262 rc = -1;
4263 goto out_fail;
4264 }
Eric Moore635374e2009-03-09 01:21:12 -06004265 }
4266
4267 if (sas_expander->enclosure_handle) {
4268 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4269 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4270 sas_expander->enclosure_handle))) {
4271 sas_expander->enclosure_logical_id =
4272 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4273 }
4274 }
4275
4276 _scsih_expander_node_add(ioc, sas_expander);
4277 return 0;
4278
4279 out_fail:
4280
Kashyap, Desai20f58952009-08-07 19:34:26 +05304281 if (mpt2sas_port)
4282 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304283 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004284 kfree(sas_expander);
4285 return rc;
4286}
4287
4288/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304289 * _scsih_done - scsih callback handler.
4290 * @ioc: per adapter object
4291 * @smid: system request message index
4292 * @msix_index: MSIX table index supplied by the OS
4293 * @reply: reply message frame(lower 32bit addr)
4294 *
4295 * Callback handler when sending internal generated message frames.
4296 * The callback index passed is `ioc->scsih_cb_idx`
4297 *
4298 * Return 1 meaning mf should be freed from _base_interrupt
4299 * 0 means the mf is freed from this function.
4300 */
4301static u8
4302_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4303{
4304 MPI2DefaultReply_t *mpi_reply;
4305
4306 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4307 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4308 return 1;
4309 if (ioc->scsih_cmds.smid != smid)
4310 return 1;
4311 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4312 if (mpi_reply) {
4313 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4314 mpi_reply->MsgLength*4);
4315 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4316 }
4317 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4318 complete(&ioc->scsih_cmds.done);
4319 return 1;
4320}
4321
4322/**
Eric Moore635374e2009-03-09 01:21:12 -06004323 * _scsih_expander_remove - removing expander object
4324 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304325 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004326 *
4327 * Return nothing.
4328 */
4329static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304330_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004331{
4332 struct _sas_node *sas_expander;
4333 unsigned long flags;
4334
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304335 if (ioc->shost_recovery)
4336 return;
4337
Eric Moore635374e2009-03-09 01:21:12 -06004338 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304339 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4340 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004341 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4342 _scsih_expander_node_remove(ioc, sas_expander);
4343}
4344
4345/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304346 * _scsih_check_access_status - check access flags
4347 * @ioc: per adapter object
4348 * @sas_address: sas address
4349 * @handle: sas device handle
4350 * @access_flags: errors returned during discovery of the device
4351 *
4352 * Return 0 for success, else failure
4353 */
4354static u8
4355_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4356 u16 handle, u8 access_status)
4357{
4358 u8 rc = 1;
4359 char *desc = NULL;
4360
4361 switch (access_status) {
4362 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4363 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4364 rc = 0;
4365 break;
4366 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4367 desc = "sata capability failed";
4368 break;
4369 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4370 desc = "sata affiliation conflict";
4371 break;
4372 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4373 desc = "route not addressable";
4374 break;
4375 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4376 desc = "smp error not addressable";
4377 break;
4378 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4379 desc = "device blocked";
4380 break;
4381 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4382 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4383 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4384 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4385 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4386 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4387 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4388 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4389 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4390 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4391 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4392 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4393 desc = "sata initialization failed";
4394 break;
4395 default:
4396 desc = "unknown";
4397 break;
4398 }
4399
4400 if (!rc)
4401 return 0;
4402
4403 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4404 "handle(0x%04x)\n", ioc->name, desc,
4405 (unsigned long long)sas_address, handle);
4406 return rc;
4407}
4408
4409static void
4410_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4411{
4412 Mpi2ConfigReply_t mpi_reply;
4413 Mpi2SasDevicePage0_t sas_device_pg0;
4414 struct _sas_device *sas_device;
4415 u32 ioc_status;
4416 unsigned long flags;
4417 u64 sas_address;
4418 struct scsi_target *starget;
4419 struct MPT2SAS_TARGET *sas_target_priv_data;
4420 u32 device_info;
4421
4422 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4423 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4424 return;
4425
4426 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4427 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4428 return;
4429
4430 /* check if this is end device */
4431 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4432 if (!(_scsih_is_end_device(device_info)))
4433 return;
4434
4435 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4436 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4437 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4438 sas_address);
4439
4440 if (!sas_device) {
4441 printk(MPT2SAS_ERR_FMT "device is not present "
4442 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4443 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4444 return;
4445 }
4446
4447 if (unlikely(sas_device->handle != handle)) {
4448 starget = sas_device->starget;
4449 sas_target_priv_data = starget->hostdata;
4450 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4451 " to (0x%04x)!!!\n", sas_device->handle, handle);
4452 sas_target_priv_data->handle = handle;
4453 sas_device->handle = handle;
4454 }
4455 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4456
4457 /* check if device is present */
4458 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4459 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4460 printk(MPT2SAS_ERR_FMT "device is not present "
4461 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4462 return;
4463 }
4464
4465 /* check if there were any issues with discovery */
4466 if (_scsih_check_access_status(ioc, sas_address, handle,
4467 sas_device_pg0.AccessStatus))
4468 return;
4469 _scsih_ublock_io_device(ioc, handle);
4470
4471}
4472
4473/**
Eric Moore635374e2009-03-09 01:21:12 -06004474 * _scsih_add_device - creating sas device object
4475 * @ioc: per adapter object
4476 * @handle: sas device handle
4477 * @phy_num: phy number end device attached to
4478 * @is_pd: is this hidden raid component
4479 *
4480 * Creating end device object, stored in ioc->sas_device_list.
4481 *
4482 * Returns 0 for success, non-zero for failure.
4483 */
4484static int
4485_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4486{
4487 Mpi2ConfigReply_t mpi_reply;
4488 Mpi2SasDevicePage0_t sas_device_pg0;
4489 Mpi2SasEnclosurePage0_t enclosure_pg0;
4490 struct _sas_device *sas_device;
4491 u32 ioc_status;
4492 __le64 sas_address;
4493 u32 device_info;
4494 unsigned long flags;
4495
4496 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4497 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4498 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4499 ioc->name, __FILE__, __LINE__, __func__);
4500 return -1;
4501 }
4502
4503 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4504 MPI2_IOCSTATUS_MASK;
4505 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4506 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4507 ioc->name, __FILE__, __LINE__, __func__);
4508 return -1;
4509 }
4510
Kashyap, Desaib4344272010-03-17 16:24:14 +05304511 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4512
Eric Moore635374e2009-03-09 01:21:12 -06004513 /* check if device is present */
4514 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4515 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4516 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4517 ioc->name, __FILE__, __LINE__, __func__);
4518 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4519 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4520 return -1;
4521 }
4522
Kashyap, Desaib4344272010-03-17 16:24:14 +05304523 /* check if there were any issues with discovery */
4524 if (_scsih_check_access_status(ioc, sas_address, handle,
4525 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06004526 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06004527
4528 /* check if this is end device */
4529 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4530 if (!(_scsih_is_end_device(device_info))) {
4531 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4532 ioc->name, __FILE__, __LINE__, __func__);
4533 return -1;
4534 }
4535
Eric Moore635374e2009-03-09 01:21:12 -06004536
4537 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4538 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4539 sas_address);
4540 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4541
Kashyap, Desaib4344272010-03-17 16:24:14 +05304542 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004543 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06004544
4545 sas_device = kzalloc(sizeof(struct _sas_device),
4546 GFP_KERNEL);
4547 if (!sas_device) {
4548 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4549 ioc->name, __FILE__, __LINE__, __func__);
4550 return -1;
4551 }
4552
4553 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304554 if (_scsih_get_sas_address(ioc, le16_to_cpu
4555 (sas_device_pg0.ParentDevHandle),
4556 &sas_device->sas_address_parent) != 0)
4557 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4558 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004559 sas_device->enclosure_handle =
4560 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4561 sas_device->slot =
4562 le16_to_cpu(sas_device_pg0.Slot);
4563 sas_device->device_info = device_info;
4564 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304565 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06004566
4567 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304568 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4569 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4570 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004571 sas_device->enclosure_logical_id =
4572 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004573
4574 /* get device name */
4575 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4576
4577 if (ioc->wait_for_port_enable_to_complete)
4578 _scsih_sas_device_init_add(ioc, sas_device);
4579 else
4580 _scsih_sas_device_add(ioc, sas_device);
4581
4582 return 0;
4583}
4584
4585/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05304586 * _scsih_remove_device - removing sas device object
4587 * @ioc: per adapter object
4588 * @sas_device_delete: the sas_device object
4589 *
4590 * Return nothing.
4591 */
4592static void
4593_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4594 struct _sas_device *sas_device)
4595{
4596 struct _sas_device sas_device_backup;
4597 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304598
Kashyap, Desai1278b112010-03-09 17:34:13 +05304599 if (!sas_device)
4600 return;
Eric Moore635374e2009-03-09 01:21:12 -06004601
Kashyap, Desai1278b112010-03-09 17:34:13 +05304602 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06004603 _scsih_sas_device_remove(ioc, sas_device);
4604
Kashyap, Desai1278b112010-03-09 17:34:13 +05304605 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4606 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4607 sas_device_backup.handle, (unsigned long long)
4608 sas_device_backup.sas_address));
4609
4610 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4611 sas_target_priv_data = sas_device_backup.starget->hostdata;
4612 sas_target_priv_data->deleted = 1;
4613 }
4614
Kashyap, Desai1278b112010-03-09 17:34:13 +05304615 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4616
4617 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4618 sas_device_backup.sas_address_parent);
4619
4620 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4621 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4622 (unsigned long long) sas_device_backup.sas_address);
4623
4624 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4625 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4626 sas_device_backup.handle, (unsigned long long)
4627 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004628}
4629
4630#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4631/**
4632 * _scsih_sas_topology_change_event_debug - debug for topology event
4633 * @ioc: per adapter object
4634 * @event_data: event data payload
4635 * Context: user.
4636 */
4637static void
4638_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4639 Mpi2EventDataSasTopologyChangeList_t *event_data)
4640{
4641 int i;
4642 u16 handle;
4643 u16 reason_code;
4644 u8 phy_number;
4645 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304646 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004647
4648 switch (event_data->ExpStatus) {
4649 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4650 status_str = "add";
4651 break;
4652 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4653 status_str = "remove";
4654 break;
4655 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304656 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004657 status_str = "responding";
4658 break;
4659 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4660 status_str = "remove delay";
4661 break;
4662 default:
4663 status_str = "unknown status";
4664 break;
4665 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304666 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06004667 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304668 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06004669 "start_phy(%02d), count(%d)\n",
4670 le16_to_cpu(event_data->ExpanderDevHandle),
4671 le16_to_cpu(event_data->EnclosureHandle),
4672 event_data->StartPhyNum, event_data->NumEntries);
4673 for (i = 0; i < event_data->NumEntries; i++) {
4674 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4675 if (!handle)
4676 continue;
4677 phy_number = event_data->StartPhyNum + i;
4678 reason_code = event_data->PHY[i].PhyStatus &
4679 MPI2_EVENT_SAS_TOPO_RC_MASK;
4680 switch (reason_code) {
4681 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304682 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004683 break;
4684 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304685 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004686 break;
4687 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304688 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004689 break;
4690 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304691 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004692 break;
4693 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304694 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004695 break;
4696 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304697 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004698 break;
4699 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304700 link_rate = event_data->PHY[i].LinkRate >> 4;
4701 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304702 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304703 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4704 handle, status_str, link_rate, prev_link_rate);
4705
Eric Moore635374e2009-03-09 01:21:12 -06004706 }
4707}
4708#endif
4709
4710/**
4711 * _scsih_sas_topology_change_event - handle topology changes
4712 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304713 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004714 * Context: user.
4715 *
4716 */
4717static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304718_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004719 struct fw_event_work *fw_event)
4720{
4721 int i;
4722 u16 parent_handle, handle;
4723 u16 reason_code;
4724 u8 phy_number;
4725 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304726 struct _sas_device *sas_device;
4727 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004728 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304729 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304730 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004731
4732#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4733 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4734 _scsih_sas_topology_change_event_debug(ioc, event_data);
4735#endif
4736
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304737 if (ioc->shost_recovery || ioc->remove_host)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304738 return;
4739
Eric Moore635374e2009-03-09 01:21:12 -06004740 if (!ioc->sas_hba.num_phys)
4741 _scsih_sas_host_add(ioc);
4742 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304743 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004744
4745 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304746 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06004747 "event\n", ioc->name));
4748 return;
4749 }
4750
4751 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4752
4753 /* handle expander add */
4754 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4755 if (_scsih_expander_add(ioc, parent_handle) != 0)
4756 return;
4757
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304758 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4759 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4760 parent_handle);
4761 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4762 if (sas_expander)
4763 sas_address = sas_expander->sas_address;
4764 else if (parent_handle < ioc->sas_hba.num_phys)
4765 sas_address = ioc->sas_hba.sas_address;
4766 else
4767 return;
4768
Eric Moore635374e2009-03-09 01:21:12 -06004769 /* handle siblings events */
4770 for (i = 0; i < event_data->NumEntries; i++) {
4771 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304772 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06004773 "expander event\n", ioc->name));
4774 return;
4775 }
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304776 if (ioc->shost_recovery || ioc->remove_host)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304777 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304778 phy_number = event_data->StartPhyNum + i;
4779 reason_code = event_data->PHY[i].PhyStatus &
4780 MPI2_EVENT_SAS_TOPO_RC_MASK;
4781 if ((event_data->PHY[i].PhyStatus &
4782 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4783 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004784 continue;
4785 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4786 if (!handle)
4787 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304788 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304789 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004790 switch (reason_code) {
4791 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304792
4793 if (link_rate == prev_link_rate)
4794 break;
4795
4796 mpt2sas_transport_update_links(ioc, sas_address,
4797 handle, phy_number, link_rate);
4798
Kashyap, Desaib4344272010-03-17 16:24:14 +05304799 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4800 break;
4801
4802 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304803 break;
Eric Moore635374e2009-03-09 01:21:12 -06004804 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304805
4806 mpt2sas_transport_update_links(ioc, sas_address,
4807 handle, phy_number, link_rate);
4808
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304809 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004810 break;
4811 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304812
4813 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4814 sas_device = _scsih_sas_device_find_by_handle(ioc,
4815 handle);
4816 if (!sas_device) {
4817 spin_unlock_irqrestore(&ioc->sas_device_lock,
4818 flags);
4819 break;
4820 }
4821 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4822 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004823 break;
4824 }
4825 }
4826
4827 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304828 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4829 sas_expander)
4830 _scsih_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004831
4832}
4833
4834#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4835/**
4836 * _scsih_sas_device_status_change_event_debug - debug for device event
4837 * @event_data: event data payload
4838 * Context: user.
4839 *
4840 * Return nothing.
4841 */
4842static void
4843_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4844 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4845{
4846 char *reason_str = NULL;
4847
4848 switch (event_data->ReasonCode) {
4849 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4850 reason_str = "smart data";
4851 break;
4852 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4853 reason_str = "unsupported device discovered";
4854 break;
4855 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4856 reason_str = "internal device reset";
4857 break;
4858 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4859 reason_str = "internal task abort";
4860 break;
4861 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4862 reason_str = "internal task abort set";
4863 break;
4864 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4865 reason_str = "internal clear task set";
4866 break;
4867 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4868 reason_str = "internal query task";
4869 break;
4870 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4871 reason_str = "sata init failure";
4872 break;
4873 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4874 reason_str = "internal device reset complete";
4875 break;
4876 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4877 reason_str = "internal task abort complete";
4878 break;
4879 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4880 reason_str = "internal async notification";
4881 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304882 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4883 reason_str = "expander reduced functionality";
4884 break;
4885 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4886 reason_str = "expander reduced functionality complete";
4887 break;
Eric Moore635374e2009-03-09 01:21:12 -06004888 default:
4889 reason_str = "unknown reason";
4890 break;
4891 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304892 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06004893 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4894 reason_str, le16_to_cpu(event_data->DevHandle),
4895 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4896 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304897 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004898 event_data->ASC, event_data->ASCQ);
4899 printk(KERN_INFO "\n");
4900}
4901#endif
4902
4903/**
4904 * _scsih_sas_device_status_change_event - handle device status change
4905 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304906 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004907 * Context: user.
4908 *
4909 * Return nothing.
4910 */
4911static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304912_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4913 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004914{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304915 struct MPT2SAS_TARGET *target_priv_data;
4916 struct _sas_device *sas_device;
4917 __le64 sas_address;
4918 unsigned long flags;
4919 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4920 fw_event->event_data;
4921
Eric Moore635374e2009-03-09 01:21:12 -06004922#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4923 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304924 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304925 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004926#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304927
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05304928 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304929 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05304930 event_data->ReasonCode !=
4931 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304932 return;
4933
4934 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4935 sas_address = le64_to_cpu(event_data->SASAddress);
4936 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4937 sas_address);
4938 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4939
4940 if (!sas_device || !sas_device->starget)
4941 return;
4942
4943 target_priv_data = sas_device->starget->hostdata;
4944 if (!target_priv_data)
4945 return;
4946
4947 if (event_data->ReasonCode ==
4948 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4949 target_priv_data->tm_busy = 1;
4950 else
4951 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004952}
4953
4954#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4955/**
4956 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4957 * @ioc: per adapter object
4958 * @event_data: event data payload
4959 * Context: user.
4960 *
4961 * Return nothing.
4962 */
4963static void
4964_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4965 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4966{
4967 char *reason_str = NULL;
4968
4969 switch (event_data->ReasonCode) {
4970 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4971 reason_str = "enclosure add";
4972 break;
4973 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4974 reason_str = "enclosure remove";
4975 break;
4976 default:
4977 reason_str = "unknown reason";
4978 break;
4979 }
4980
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304981 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06004982 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4983 " number slots(%d)\n", ioc->name, reason_str,
4984 le16_to_cpu(event_data->EnclosureHandle),
4985 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4986 le16_to_cpu(event_data->StartSlot));
4987}
4988#endif
4989
4990/**
4991 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4992 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304993 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004994 * Context: user.
4995 *
4996 * Return nothing.
4997 */
4998static void
4999_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305000 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005001{
5002#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5003 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5004 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305005 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005006#endif
5007}
5008
5009/**
5010 * _scsih_sas_broadcast_primative_event - handle broadcast events
5011 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305012 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005013 * Context: user.
5014 *
5015 * Return nothing.
5016 */
5017static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305018_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5019 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005020{
5021 struct scsi_cmnd *scmd;
5022 u16 smid, handle;
5023 u32 lun;
5024 struct MPT2SAS_DEVICE *sas_device_priv_data;
5025 u32 termination_count;
5026 u32 query_count;
5027 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305028#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5029 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5030#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05305031 u16 ioc_status;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305032 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
Eric Moore635374e2009-03-09 01:21:12 -06005033 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5034 event_data->PortWidth));
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305035 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005036 __func__));
5037
Eric Moore635374e2009-03-09 01:21:12 -06005038 termination_count = 0;
5039 query_count = 0;
5040 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305041 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06005042 scmd = _scsih_scsi_lookup_get(ioc, smid);
5043 if (!scmd)
5044 continue;
5045 sas_device_priv_data = scmd->device->hostdata;
5046 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5047 continue;
5048 /* skip hidden raid components */
5049 if (sas_device_priv_data->sas_target->flags &
5050 MPT_TARGET_FLAGS_RAID_COMPONENT)
5051 continue;
5052 /* skip volumes */
5053 if (sas_device_priv_data->sas_target->flags &
5054 MPT_TARGET_FLAGS_VOLUME)
5055 continue;
5056
5057 handle = sas_device_priv_data->sas_target->handle;
5058 lun = sas_device_priv_data->lun;
5059 query_count++;
5060
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305061 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5062 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
Eric Moore8901cbb2009-04-21 15:41:32 -06005063 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305064 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5065 & MPI2_IOCSTATUS_MASK;
5066 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06005067 (mpi_reply->ResponseCode ==
5068 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5069 mpi_reply->ResponseCode ==
5070 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
5071 continue;
5072
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305073 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5074 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30, NULL);
Eric Moore635374e2009-03-09 01:21:12 -06005075 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5076 }
Eric Moore635374e2009-03-09 01:21:12 -06005077 ioc->broadcast_aen_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005078
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305079 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005080 "%s - exit, query_count = %d termination_count = %d\n",
5081 ioc->name, __func__, query_count, termination_count));
5082}
5083
5084/**
5085 * _scsih_sas_discovery_event - handle discovery events
5086 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305087 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005088 * Context: user.
5089 *
5090 * Return nothing.
5091 */
5092static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305093_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5094 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005095{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305096 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5097
Eric Moore635374e2009-03-09 01:21:12 -06005098#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5099 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305100 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005101 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5102 "start" : "stop");
5103 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305104 printk("discovery_status(0x%08x)",
5105 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005106 printk("\n");
5107 }
5108#endif
5109
5110 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5111 !ioc->sas_hba.num_phys)
5112 _scsih_sas_host_add(ioc);
5113}
5114
5115/**
5116 * _scsih_reprobe_lun - reprobing lun
5117 * @sdev: scsi device struct
5118 * @no_uld_attach: sdev->no_uld_attach flag setting
5119 *
5120 **/
5121static void
5122_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5123{
5124 int rc;
5125
5126 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5127 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5128 sdev->no_uld_attach ? "hidding" : "exposing");
5129 rc = scsi_device_reprobe(sdev);
5130}
5131
5132/**
5133 * _scsih_reprobe_target - reprobing target
5134 * @starget: scsi target struct
5135 * @no_uld_attach: sdev->no_uld_attach flag setting
5136 *
5137 * Note: no_uld_attach flag determines whether the disk device is attached
5138 * to block layer. A value of `1` means to not attach.
5139 **/
5140static void
5141_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5142{
5143 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5144
5145 if (no_uld_attach)
5146 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5147 else
5148 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5149
5150 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5151 _scsih_reprobe_lun);
5152}
5153/**
5154 * _scsih_sas_volume_add - add new volume
5155 * @ioc: per adapter object
5156 * @element: IR config element data
5157 * Context: user.
5158 *
5159 * Return nothing.
5160 */
5161static void
5162_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5163 Mpi2EventIrConfigElement_t *element)
5164{
5165 struct _raid_device *raid_device;
5166 unsigned long flags;
5167 u64 wwid;
5168 u16 handle = le16_to_cpu(element->VolDevHandle);
5169 int rc;
5170
Eric Moore635374e2009-03-09 01:21:12 -06005171 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5172 if (!wwid) {
5173 printk(MPT2SAS_ERR_FMT
5174 "failure at %s:%d/%s()!\n", ioc->name,
5175 __FILE__, __LINE__, __func__);
5176 return;
5177 }
5178
5179 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5180 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5181 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5182
5183 if (raid_device)
5184 return;
5185
5186 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5187 if (!raid_device) {
5188 printk(MPT2SAS_ERR_FMT
5189 "failure at %s:%d/%s()!\n", ioc->name,
5190 __FILE__, __LINE__, __func__);
5191 return;
5192 }
5193
5194 raid_device->id = ioc->sas_id++;
5195 raid_device->channel = RAID_CHANNEL;
5196 raid_device->handle = handle;
5197 raid_device->wwid = wwid;
5198 _scsih_raid_device_add(ioc, raid_device);
5199 if (!ioc->wait_for_port_enable_to_complete) {
5200 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5201 raid_device->id, 0);
5202 if (rc)
5203 _scsih_raid_device_remove(ioc, raid_device);
5204 } else
5205 _scsih_determine_boot_device(ioc, raid_device, 1);
5206}
5207
5208/**
5209 * _scsih_sas_volume_delete - delete volume
5210 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305211 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005212 * Context: user.
5213 *
5214 * Return nothing.
5215 */
5216static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305217_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005218{
5219 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06005220 unsigned long flags;
5221 struct MPT2SAS_TARGET *sas_target_priv_data;
5222
Eric Moore635374e2009-03-09 01:21:12 -06005223 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5224 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5225 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5226 if (!raid_device)
5227 return;
5228 if (raid_device->starget) {
5229 sas_target_priv_data = raid_device->starget->hostdata;
5230 sas_target_priv_data->deleted = 1;
5231 scsi_remove_target(&raid_device->starget->dev);
5232 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305233 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5234 "(0x%016llx)\n", ioc->name, raid_device->handle,
5235 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06005236 _scsih_raid_device_remove(ioc, raid_device);
5237}
5238
5239/**
5240 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5241 * @ioc: per adapter object
5242 * @element: IR config element data
5243 * Context: user.
5244 *
5245 * Return nothing.
5246 */
5247static void
5248_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5249 Mpi2EventIrConfigElement_t *element)
5250{
5251 struct _sas_device *sas_device;
5252 unsigned long flags;
5253 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5254
5255 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5256 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5257 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5258 if (!sas_device)
5259 return;
5260
5261 /* exposing raid component */
5262 sas_device->volume_handle = 0;
5263 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305264 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005265 _scsih_reprobe_target(sas_device->starget, 0);
5266}
5267
5268/**
5269 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5270 * @ioc: per adapter object
5271 * @element: IR config element data
5272 * Context: user.
5273 *
5274 * Return nothing.
5275 */
5276static void
5277_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5278 Mpi2EventIrConfigElement_t *element)
5279{
5280 struct _sas_device *sas_device;
5281 unsigned long flags;
5282 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5283
5284 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5285 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5286 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5287 if (!sas_device)
5288 return;
5289
5290 /* hiding raid component */
5291 mpt2sas_config_get_volume_handle(ioc, handle,
5292 &sas_device->volume_handle);
5293 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5294 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305295 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005296 _scsih_reprobe_target(sas_device->starget, 1);
5297}
5298
5299/**
5300 * _scsih_sas_pd_delete - delete pd component
5301 * @ioc: per adapter object
5302 * @element: IR config element data
5303 * Context: user.
5304 *
5305 * Return nothing.
5306 */
5307static void
5308_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5309 Mpi2EventIrConfigElement_t *element)
5310{
5311 struct _sas_device *sas_device;
5312 unsigned long flags;
5313 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5314
5315 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5316 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5317 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5318 if (!sas_device)
5319 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305320 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005321}
5322
5323/**
5324 * _scsih_sas_pd_add - remove pd component
5325 * @ioc: per adapter object
5326 * @element: IR config element data
5327 * Context: user.
5328 *
5329 * Return nothing.
5330 */
5331static void
5332_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5333 Mpi2EventIrConfigElement_t *element)
5334{
5335 struct _sas_device *sas_device;
5336 unsigned long flags;
5337 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305338 Mpi2ConfigReply_t mpi_reply;
5339 Mpi2SasDevicePage0_t sas_device_pg0;
5340 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305341 u64 sas_address;
5342 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005343
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305344 set_bit(handle, ioc->pd_handles);
5345
Eric Moore635374e2009-03-09 01:21:12 -06005346 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5347 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5348 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305349 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305350 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305351
5352 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5353 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5354 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5355 ioc->name, __FILE__, __LINE__, __func__);
5356 return;
5357 }
5358
5359 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5360 MPI2_IOCSTATUS_MASK;
5361 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5362 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5363 ioc->name, __FILE__, __LINE__, __func__);
5364 return;
5365 }
5366
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305367 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5368 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5369 mpt2sas_transport_update_links(ioc, sas_address, handle,
5370 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305371
5372 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06005373}
5374
5375#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5376/**
5377 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5378 * @ioc: per adapter object
5379 * @event_data: event data payload
5380 * Context: user.
5381 *
5382 * Return nothing.
5383 */
5384static void
5385_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5386 Mpi2EventDataIrConfigChangeList_t *event_data)
5387{
5388 Mpi2EventIrConfigElement_t *element;
5389 u8 element_type;
5390 int i;
5391 char *reason_str = NULL, *element_str = NULL;
5392
5393 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5394
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305395 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005396 ioc->name, (le32_to_cpu(event_data->Flags) &
5397 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5398 "foreign" : "native", event_data->NumElements);
5399 for (i = 0; i < event_data->NumElements; i++, element++) {
5400 switch (element->ReasonCode) {
5401 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5402 reason_str = "add";
5403 break;
5404 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5405 reason_str = "remove";
5406 break;
5407 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5408 reason_str = "no change";
5409 break;
5410 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5411 reason_str = "hide";
5412 break;
5413 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5414 reason_str = "unhide";
5415 break;
5416 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5417 reason_str = "volume_created";
5418 break;
5419 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5420 reason_str = "volume_deleted";
5421 break;
5422 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5423 reason_str = "pd_created";
5424 break;
5425 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5426 reason_str = "pd_deleted";
5427 break;
5428 default:
5429 reason_str = "unknown reason";
5430 break;
5431 }
5432 element_type = le16_to_cpu(element->ElementFlags) &
5433 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5434 switch (element_type) {
5435 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5436 element_str = "volume";
5437 break;
5438 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5439 element_str = "phys disk";
5440 break;
5441 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5442 element_str = "hot spare";
5443 break;
5444 default:
5445 element_str = "unknown element";
5446 break;
5447 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305448 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005449 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5450 reason_str, le16_to_cpu(element->VolDevHandle),
5451 le16_to_cpu(element->PhysDiskDevHandle),
5452 element->PhysDiskNum);
5453 }
5454}
5455#endif
5456
5457/**
5458 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5459 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305460 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005461 * Context: user.
5462 *
5463 * Return nothing.
5464 */
5465static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305466_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5467 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005468{
5469 Mpi2EventIrConfigElement_t *element;
5470 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305471 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305472 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005473
5474#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5475 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5476 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5477
5478#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305479 foreign_config = (le32_to_cpu(event_data->Flags) &
5480 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005481
5482 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5483 for (i = 0; i < event_data->NumElements; i++, element++) {
5484
5485 switch (element->ReasonCode) {
5486 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5487 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305488 if (!foreign_config)
5489 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005490 break;
5491 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5492 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305493 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305494 _scsih_sas_volume_delete(ioc,
5495 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06005496 break;
5497 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5498 _scsih_sas_pd_hide(ioc, element);
5499 break;
5500 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5501 _scsih_sas_pd_expose(ioc, element);
5502 break;
5503 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5504 _scsih_sas_pd_add(ioc, element);
5505 break;
5506 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5507 _scsih_sas_pd_delete(ioc, element);
5508 break;
5509 }
5510 }
5511}
5512
5513/**
5514 * _scsih_sas_ir_volume_event - IR volume event
5515 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305516 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005517 * Context: user.
5518 *
5519 * Return nothing.
5520 */
5521static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305522_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5523 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005524{
5525 u64 wwid;
5526 unsigned long flags;
5527 struct _raid_device *raid_device;
5528 u16 handle;
5529 u32 state;
5530 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305531 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005532
5533 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5534 return;
5535
5536 handle = le16_to_cpu(event_data->VolDevHandle);
5537 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305538 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005539 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5540 le32_to_cpu(event_data->PreviousValue), state));
5541
Eric Moore635374e2009-03-09 01:21:12 -06005542 switch (state) {
5543 case MPI2_RAID_VOL_STATE_MISSING:
5544 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305545 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005546 break;
5547
5548 case MPI2_RAID_VOL_STATE_ONLINE:
5549 case MPI2_RAID_VOL_STATE_DEGRADED:
5550 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305551
5552 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5553 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5554 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5555
Eric Moore635374e2009-03-09 01:21:12 -06005556 if (raid_device)
5557 break;
5558
5559 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5560 if (!wwid) {
5561 printk(MPT2SAS_ERR_FMT
5562 "failure at %s:%d/%s()!\n", ioc->name,
5563 __FILE__, __LINE__, __func__);
5564 break;
5565 }
5566
5567 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5568 if (!raid_device) {
5569 printk(MPT2SAS_ERR_FMT
5570 "failure at %s:%d/%s()!\n", ioc->name,
5571 __FILE__, __LINE__, __func__);
5572 break;
5573 }
5574
5575 raid_device->id = ioc->sas_id++;
5576 raid_device->channel = RAID_CHANNEL;
5577 raid_device->handle = handle;
5578 raid_device->wwid = wwid;
5579 _scsih_raid_device_add(ioc, raid_device);
5580 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5581 raid_device->id, 0);
5582 if (rc)
5583 _scsih_raid_device_remove(ioc, raid_device);
5584 break;
5585
5586 case MPI2_RAID_VOL_STATE_INITIALIZING:
5587 default:
5588 break;
5589 }
5590}
5591
5592/**
5593 * _scsih_sas_ir_physical_disk_event - PD event
5594 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305595 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005596 * Context: user.
5597 *
5598 * Return nothing.
5599 */
5600static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305601_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5602 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005603{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305604 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005605 u32 state;
5606 struct _sas_device *sas_device;
5607 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305608 Mpi2ConfigReply_t mpi_reply;
5609 Mpi2SasDevicePage0_t sas_device_pg0;
5610 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305611 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305612 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005613
5614 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5615 return;
5616
5617 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5618 state = le32_to_cpu(event_data->NewValue);
5619
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305620 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005621 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5622 le32_to_cpu(event_data->PreviousValue), state));
5623
Eric Moore635374e2009-03-09 01:21:12 -06005624 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005625 case MPI2_RAID_PD_STATE_ONLINE:
5626 case MPI2_RAID_PD_STATE_DEGRADED:
5627 case MPI2_RAID_PD_STATE_REBUILDING:
5628 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305629 case MPI2_RAID_PD_STATE_HOT_SPARE:
5630
5631 set_bit(handle, ioc->pd_handles);
5632
5633 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5634 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5635 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5636
5637 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305638 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305639
5640 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5641 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5642 handle))) {
5643 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5644 ioc->name, __FILE__, __LINE__, __func__);
5645 return;
5646 }
5647
5648 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5649 MPI2_IOCSTATUS_MASK;
5650 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5651 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5652 ioc->name, __FILE__, __LINE__, __func__);
5653 return;
5654 }
5655
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305656 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5657 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5658 mpt2sas_transport_update_links(ioc, sas_address, handle,
5659 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305660
5661 _scsih_add_device(ioc, handle, 0, 1);
5662
Eric Moore635374e2009-03-09 01:21:12 -06005663 break;
5664
Kashyap, Desai62727a72009-08-07 19:35:18 +05305665 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005666 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5667 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06005668 default:
5669 break;
5670 }
5671}
5672
5673#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5674/**
5675 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5676 * @ioc: per adapter object
5677 * @event_data: event data payload
5678 * Context: user.
5679 *
5680 * Return nothing.
5681 */
5682static void
5683_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5684 Mpi2EventDataIrOperationStatus_t *event_data)
5685{
5686 char *reason_str = NULL;
5687
5688 switch (event_data->RAIDOperation) {
5689 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5690 reason_str = "resync";
5691 break;
5692 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5693 reason_str = "online capacity expansion";
5694 break;
5695 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5696 reason_str = "consistency check";
5697 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305698 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5699 reason_str = "background init";
5700 break;
5701 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5702 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005703 break;
5704 }
5705
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305706 if (!reason_str)
5707 return;
5708
Eric Moore635374e2009-03-09 01:21:12 -06005709 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5710 "\thandle(0x%04x), percent complete(%d)\n",
5711 ioc->name, reason_str,
5712 le16_to_cpu(event_data->VolDevHandle),
5713 event_data->PercentComplete);
5714}
5715#endif
5716
5717/**
5718 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5719 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305720 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005721 * Context: user.
5722 *
5723 * Return nothing.
5724 */
5725static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305726_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5727 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005728{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305729 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5730 static struct _raid_device *raid_device;
5731 unsigned long flags;
5732 u16 handle;
5733
Eric Moore635374e2009-03-09 01:21:12 -06005734#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5735 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305736 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305737 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005738#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305739
5740 /* code added for raid transport support */
5741 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5742
5743 handle = le16_to_cpu(event_data->VolDevHandle);
5744
5745 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5746 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5747 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5748
5749 if (!raid_device)
5750 return;
5751
5752 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5753 raid_device->percent_complete =
5754 event_data->PercentComplete;
5755 }
Eric Moore635374e2009-03-09 01:21:12 -06005756}
5757
5758/**
5759 * _scsih_task_set_full - handle task set full
5760 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305761 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005762 * Context: user.
5763 *
5764 * Throttle back qdepth.
5765 */
5766static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305767_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5768 *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005769{
5770 unsigned long flags;
5771 struct _sas_device *sas_device;
5772 static struct _raid_device *raid_device;
5773 struct scsi_device *sdev;
5774 int depth;
5775 u16 current_depth;
5776 u16 handle;
5777 int id, channel;
5778 u64 sas_address;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305779 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005780
5781 current_depth = le16_to_cpu(event_data->CurrentDepth);
5782 handle = le16_to_cpu(event_data->DevHandle);
5783 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5784 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5785 if (!sas_device) {
5786 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5787 return;
5788 }
5789 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5790 id = sas_device->id;
5791 channel = sas_device->channel;
5792 sas_address = sas_device->sas_address;
5793
5794 /* if hidden raid component, then change to volume characteristics */
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305795 if (test_bit(handle, ioc->pd_handles) && sas_device->volume_handle) {
Eric Moore635374e2009-03-09 01:21:12 -06005796 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5797 raid_device = _scsih_raid_device_find_by_handle(
5798 ioc, sas_device->volume_handle);
5799 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5800 if (raid_device) {
5801 id = raid_device->id;
5802 channel = raid_device->channel;
5803 handle = raid_device->handle;
5804 sas_address = raid_device->wwid;
5805 }
5806 }
5807
5808 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305809 starget_printk(KERN_INFO, sas_device->starget, "task set "
Eric Moore635374e2009-03-09 01:21:12 -06005810 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5811 handle, (unsigned long long)sas_address, current_depth);
5812
5813 shost_for_each_device(sdev, ioc->shost) {
5814 if (sdev->id == id && sdev->channel == channel) {
5815 if (current_depth > sdev->queue_depth) {
5816 if (ioc->logging_level &
5817 MPT_DEBUG_TASK_SET_FULL)
5818 sdev_printk(KERN_INFO, sdev, "strange "
5819 "observation, the queue depth is"
5820 " (%d) meanwhile fw queue depth "
5821 "is (%d)\n", sdev->queue_depth,
5822 current_depth);
5823 continue;
5824 }
5825 depth = scsi_track_queue_full(sdev,
5826 current_depth - 1);
5827 if (depth > 0)
5828 sdev_printk(KERN_INFO, sdev, "Queue depth "
5829 "reduced to (%d)\n", depth);
5830 else if (depth < 0)
5831 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5832 "Queueing is being disabled\n");
5833 else if (depth == 0)
5834 if (ioc->logging_level &
5835 MPT_DEBUG_TASK_SET_FULL)
5836 sdev_printk(KERN_INFO, sdev,
5837 "Queue depth not changed yet\n");
5838 }
5839 }
5840}
5841
5842/**
Kashyap, Desai14695852010-03-30 10:52:44 +05305843 * _scsih_prep_device_scan - initialize parameters prior to device scan
5844 * @ioc: per adapter object
5845 *
5846 * Set the deleted flag prior to device scan. If the device is found during
5847 * the scan, then we clear the deleted flag.
5848 */
5849static void
5850_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5851{
5852 struct MPT2SAS_DEVICE *sas_device_priv_data;
5853 struct scsi_device *sdev;
5854
5855 shost_for_each_device(sdev, ioc->shost) {
5856 sas_device_priv_data = sdev->hostdata;
5857 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5858 sas_device_priv_data->sas_target->deleted = 1;
5859 }
5860}
5861
5862/**
Eric Moore635374e2009-03-09 01:21:12 -06005863 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5864 * @ioc: per adapter object
5865 * @sas_address: sas address
5866 * @slot: enclosure slot id
5867 * @handle: device handle
5868 *
5869 * After host reset, find out whether devices are still responding.
5870 * Used in _scsi_remove_unresponsive_sas_devices.
5871 *
5872 * Return nothing.
5873 */
5874static void
5875_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5876 u16 slot, u16 handle)
5877{
5878 struct MPT2SAS_TARGET *sas_target_priv_data;
5879 struct scsi_target *starget;
5880 struct _sas_device *sas_device;
5881 unsigned long flags;
5882
5883 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5884 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5885 if (sas_device->sas_address == sas_address &&
5886 sas_device->slot == slot && sas_device->starget) {
5887 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305888 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05305889 if (starget && starget->hostdata) {
5890 sas_target_priv_data = starget->hostdata;
5891 sas_target_priv_data->tm_busy = 0;
5892 sas_target_priv_data->deleted = 0;
5893 } else
5894 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005895 starget_printk(KERN_INFO, sas_device->starget,
5896 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5897 "logical id(0x%016llx), slot(%d)\n", handle,
5898 (unsigned long long)sas_device->sas_address,
5899 (unsigned long long)
5900 sas_device->enclosure_logical_id,
5901 sas_device->slot);
5902 if (sas_device->handle == handle)
5903 goto out;
5904 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5905 sas_device->handle);
5906 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305907 if (sas_target_priv_data)
5908 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005909 goto out;
5910 }
5911 }
5912 out:
5913 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5914}
5915
5916/**
5917 * _scsih_search_responding_sas_devices -
5918 * @ioc: per adapter object
5919 *
5920 * After host reset, find out whether devices are still responding.
5921 * If not remove.
5922 *
5923 * Return nothing.
5924 */
5925static void
5926_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5927{
5928 Mpi2SasDevicePage0_t sas_device_pg0;
5929 Mpi2ConfigReply_t mpi_reply;
5930 u16 ioc_status;
5931 __le64 sas_address;
5932 u16 handle;
5933 u32 device_info;
5934 u16 slot;
5935
5936 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5937
5938 if (list_empty(&ioc->sas_device_list))
5939 return;
5940
5941 handle = 0xFFFF;
5942 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5943 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5944 handle))) {
5945 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5946 MPI2_IOCSTATUS_MASK;
5947 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5948 break;
5949 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5950 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5951 if (!(_scsih_is_end_device(device_info)))
5952 continue;
5953 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5954 slot = le16_to_cpu(sas_device_pg0.Slot);
5955 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5956 handle);
5957 }
5958}
5959
5960/**
5961 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5962 * @ioc: per adapter object
5963 * @wwid: world wide identifier for raid volume
5964 * @handle: device handle
5965 *
5966 * After host reset, find out whether devices are still responding.
5967 * Used in _scsi_remove_unresponsive_raid_devices.
5968 *
5969 * Return nothing.
5970 */
5971static void
5972_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5973 u16 handle)
5974{
5975 struct MPT2SAS_TARGET *sas_target_priv_data;
5976 struct scsi_target *starget;
5977 struct _raid_device *raid_device;
5978 unsigned long flags;
5979
5980 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5981 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5982 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05305983 starget = raid_device->starget;
5984 if (starget && starget->hostdata) {
5985 sas_target_priv_data = starget->hostdata;
5986 sas_target_priv_data->deleted = 0;
5987 } else
5988 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005989 raid_device->responding = 1;
5990 starget_printk(KERN_INFO, raid_device->starget,
5991 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5992 (unsigned long long)raid_device->wwid);
5993 if (raid_device->handle == handle)
5994 goto out;
5995 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5996 raid_device->handle);
5997 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305998 if (sas_target_priv_data)
5999 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006000 goto out;
6001 }
6002 }
6003 out:
6004 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6005}
6006
6007/**
6008 * _scsih_search_responding_raid_devices -
6009 * @ioc: per adapter object
6010 *
6011 * After host reset, find out whether devices are still responding.
6012 * If not remove.
6013 *
6014 * Return nothing.
6015 */
6016static void
6017_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6018{
6019 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306020 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306021 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006022 Mpi2ConfigReply_t mpi_reply;
6023 u16 ioc_status;
6024 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306025 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006026
6027 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6028
6029 if (list_empty(&ioc->raid_device_list))
6030 return;
6031
6032 handle = 0xFFFF;
6033 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6034 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6035 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6036 MPI2_IOCSTATUS_MASK;
6037 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6038 break;
6039 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306040
6041 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6042 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6043 sizeof(Mpi2RaidVolPage0_t)))
6044 continue;
6045
6046 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6047 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6048 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6049 _scsih_mark_responding_raid_device(ioc,
6050 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006051 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306052
6053 /* refresh the pd_handles */
6054 phys_disk_num = 0xFF;
6055 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6056 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6057 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6058 phys_disk_num))) {
6059 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6060 MPI2_IOCSTATUS_MASK;
6061 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6062 break;
6063 phys_disk_num = pd_pg0.PhysDiskNum;
6064 handle = le16_to_cpu(pd_pg0.DevHandle);
6065 set_bit(handle, ioc->pd_handles);
6066 }
Eric Moore635374e2009-03-09 01:21:12 -06006067}
6068
6069/**
6070 * _scsih_mark_responding_expander - mark a expander as responding
6071 * @ioc: per adapter object
6072 * @sas_address: sas address
6073 * @handle:
6074 *
6075 * After host reset, find out whether devices are still responding.
6076 * Used in _scsi_remove_unresponsive_expanders.
6077 *
6078 * Return nothing.
6079 */
6080static void
6081_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6082 u16 handle)
6083{
6084 struct _sas_node *sas_expander;
6085 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306086 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006087
6088 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6089 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306090 if (sas_expander->sas_address != sas_address)
6091 continue;
6092 sas_expander->responding = 1;
6093 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006094 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306095 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6096 " from(0x%04x) to (0x%04x)!!!\n",
6097 (unsigned long long)sas_expander->sas_address,
6098 sas_expander->handle, handle);
6099 sas_expander->handle = handle;
6100 for (i = 0 ; i < sas_expander->num_phys ; i++)
6101 sas_expander->phy[i].handle = handle;
6102 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006103 }
6104 out:
6105 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6106}
6107
6108/**
6109 * _scsih_search_responding_expanders -
6110 * @ioc: per adapter object
6111 *
6112 * After host reset, find out whether devices are still responding.
6113 * If not remove.
6114 *
6115 * Return nothing.
6116 */
6117static void
6118_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6119{
6120 Mpi2ExpanderPage0_t expander_pg0;
6121 Mpi2ConfigReply_t mpi_reply;
6122 u16 ioc_status;
6123 __le64 sas_address;
6124 u16 handle;
6125
6126 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6127
6128 if (list_empty(&ioc->sas_expander_list))
6129 return;
6130
6131 handle = 0xFFFF;
6132 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6133 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6134
6135 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6136 MPI2_IOCSTATUS_MASK;
6137 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6138 break;
6139
6140 handle = le16_to_cpu(expander_pg0.DevHandle);
6141 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6142 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6143 "sas_addr(0x%016llx)\n", handle,
6144 (unsigned long long)sas_address);
6145 _scsih_mark_responding_expander(ioc, sas_address, handle);
6146 }
6147
6148}
6149
6150/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306151 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006152 * @ioc: per adapter object
6153 *
6154 * Return nothing.
6155 */
6156static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306157_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006158{
6159 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306160 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006161 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006162
Eric Moore635374e2009-03-09 01:21:12 -06006163
6164 list_for_each_entry_safe(sas_device, sas_device_next,
6165 &ioc->sas_device_list, list) {
6166 if (sas_device->responding) {
6167 sas_device->responding = 0;
6168 continue;
6169 }
6170 if (sas_device->starget)
6171 starget_printk(KERN_INFO, sas_device->starget,
6172 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6173 "enclosure logical id(0x%016llx), slot(%d)\n",
6174 sas_device->handle,
6175 (unsigned long long)sas_device->sas_address,
6176 (unsigned long long)
6177 sas_device->enclosure_logical_id,
6178 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306179 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006180 }
6181
6182 list_for_each_entry_safe(raid_device, raid_device_next,
6183 &ioc->raid_device_list, list) {
6184 if (raid_device->responding) {
6185 raid_device->responding = 0;
6186 continue;
6187 }
6188 if (raid_device->starget) {
6189 starget_printk(KERN_INFO, raid_device->starget,
6190 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6191 raid_device->handle,
6192 (unsigned long long)raid_device->wwid);
6193 scsi_remove_target(&raid_device->starget->dev);
6194 }
6195 _scsih_raid_device_remove(ioc, raid_device);
6196 }
6197
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306198 retry_expander_search:
6199 sas_expander = NULL;
6200 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006201 if (sas_expander->responding) {
6202 sas_expander->responding = 0;
6203 continue;
6204 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306205 _scsih_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306206 goto retry_expander_search;
6207 }
6208}
6209
6210/**
6211 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6212 * @ioc: per adapter object
6213 * @reset_phase: phase
6214 *
6215 * The handler for doing any required cleanup or initialization.
6216 *
6217 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6218 * MPT2_IOC_DONE_RESET
6219 *
6220 * Return nothing.
6221 */
6222void
6223mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6224{
6225 switch (reset_phase) {
6226 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306227 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306228 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306229 break;
6230 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306231 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306232 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306233 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6234 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6235 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6236 complete(&ioc->scsih_cmds.done);
6237 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306238 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6239 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6240 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6241 complete(&ioc->tm_cmds.done);
6242 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306243 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306244 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306245 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306246 break;
6247 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306248 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306249 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306250 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05306251 _scsih_prep_device_scan(ioc);
6252 _scsih_search_responding_sas_devices(ioc);
6253 _scsih_search_responding_raid_devices(ioc);
6254 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306255 break;
Eric Moore635374e2009-03-09 01:21:12 -06006256 }
6257}
6258
6259/**
6260 * _firmware_event_work - delayed task for processing firmware events
6261 * @ioc: per adapter object
6262 * @work: equal to the fw_event_work object
6263 * Context: user.
6264 *
6265 * Return nothing.
6266 */
6267static void
6268_firmware_event_work(struct work_struct *work)
6269{
6270 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306271 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06006272 unsigned long flags;
6273 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6274
Eric Moore635374e2009-03-09 01:21:12 -06006275 /* the queue is being flushed so ignore this event */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306276 if (ioc->remove_host || fw_event->cancel_pending_work) {
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;
6328 case MPI2_EVENT_TASK_SET_FULL:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306329 _scsih_task_set_full(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006330 break;
6331 }
6332 _scsih_fw_event_free(ioc, fw_event);
6333}
6334
6335/**
6336 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6337 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306338 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06006339 * @reply: reply message frame(lower 32bit addr)
6340 * Context: interrupt.
6341 *
6342 * This function merely adds a new work task into ioc->firmware_event_thread.
6343 * The tasks are worked from _firmware_event_work in user context.
6344 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306345 * Return 1 meaning mf should be freed from _base_interrupt
6346 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006347 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306348u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306349mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6350 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006351{
6352 struct fw_event_work *fw_event;
6353 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006354 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306355 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006356
6357 /* events turned off due to host reset or driver unloading */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306358 if (ioc->remove_host)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306359 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006360
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306361 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006362 event = le16_to_cpu(mpi_reply->Event);
6363
6364 switch (event) {
6365 /* handle these */
6366 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6367 {
6368 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6369 (Mpi2EventDataSasBroadcastPrimitive_t *)
6370 mpi_reply->EventData;
6371
6372 if (baen_data->Primitive !=
6373 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6374 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306375 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006376 ioc->broadcast_aen_busy = 1;
6377 break;
6378 }
6379
6380 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6381 _scsih_check_topo_delete_events(ioc,
6382 (Mpi2EventDataSasTopologyChangeList_t *)
6383 mpi_reply->EventData);
6384 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306385 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6386 _scsih_check_ir_config_unhide_events(ioc,
6387 (Mpi2EventDataIrConfigChangeList_t *)
6388 mpi_reply->EventData);
6389 break;
6390 case MPI2_EVENT_IR_VOLUME:
6391 _scsih_check_volume_delete_events(ioc,
6392 (Mpi2EventDataIrVolume_t *)
6393 mpi_reply->EventData);
6394 break;
Eric Moore635374e2009-03-09 01:21:12 -06006395 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6396 case MPI2_EVENT_IR_OPERATION_STATUS:
6397 case MPI2_EVENT_SAS_DISCOVERY:
6398 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06006399 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06006400 case MPI2_EVENT_TASK_SET_FULL:
6401 break;
6402
6403 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306404 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006405 }
6406
6407 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6408 if (!fw_event) {
6409 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6410 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306411 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006412 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306413 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6414 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06006415 if (!fw_event->event_data) {
6416 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6417 ioc->name, __FILE__, __LINE__, __func__);
6418 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306419 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006420 }
6421
6422 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306423 sz);
Eric Moore635374e2009-03-09 01:21:12 -06006424 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306425 fw_event->VF_ID = mpi_reply->VF_ID;
6426 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06006427 fw_event->event = event;
6428 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306429 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006430}
6431
6432/* shost template */
6433static struct scsi_host_template scsih_driver_template = {
6434 .module = THIS_MODULE,
6435 .name = "Fusion MPT SAS Host",
6436 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06006437 .queuecommand = _scsih_qcmd,
6438 .target_alloc = _scsih_target_alloc,
6439 .slave_alloc = _scsih_slave_alloc,
6440 .slave_configure = _scsih_slave_configure,
6441 .target_destroy = _scsih_target_destroy,
6442 .slave_destroy = _scsih_slave_destroy,
6443 .change_queue_depth = _scsih_change_queue_depth,
6444 .change_queue_type = _scsih_change_queue_type,
6445 .eh_abort_handler = _scsih_abort,
6446 .eh_device_reset_handler = _scsih_dev_reset,
6447 .eh_target_reset_handler = _scsih_target_reset,
6448 .eh_host_reset_handler = _scsih_host_reset,
6449 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06006450 .can_queue = 1,
6451 .this_id = -1,
6452 .sg_tablesize = MPT2SAS_SG_DEPTH,
6453 .max_sectors = 8192,
6454 .cmd_per_lun = 7,
6455 .use_clustering = ENABLE_CLUSTERING,
6456 .shost_attrs = mpt2sas_host_attrs,
6457 .sdev_attrs = mpt2sas_dev_attrs,
6458};
6459
6460/**
6461 * _scsih_expander_node_remove - removing expander device from list.
6462 * @ioc: per adapter object
6463 * @sas_expander: the sas_device object
6464 * Context: Calling function should acquire ioc->sas_node_lock.
6465 *
6466 * Removing object and freeing associated memory from the
6467 * ioc->sas_expander_list.
6468 *
6469 * Return nothing.
6470 */
6471static void
6472_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6473 struct _sas_node *sas_expander)
6474{
6475 struct _sas_port *mpt2sas_port;
6476 struct _sas_device *sas_device;
6477 struct _sas_node *expander_sibling;
6478 unsigned long flags;
6479
6480 if (!sas_expander)
6481 return;
6482
6483 /* remove sibling ports attached to this expander */
6484 retry_device_search:
6485 list_for_each_entry(mpt2sas_port,
6486 &sas_expander->sas_port_list, port_list) {
6487 if (mpt2sas_port->remote_identify.device_type ==
6488 SAS_END_DEVICE) {
6489 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6490 sas_device =
6491 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6492 mpt2sas_port->remote_identify.sas_address);
6493 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6494 if (!sas_device)
6495 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306496 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306497 if (ioc->shost_recovery)
6498 return;
Eric Moore635374e2009-03-09 01:21:12 -06006499 goto retry_device_search;
6500 }
6501 }
6502
6503 retry_expander_search:
6504 list_for_each_entry(mpt2sas_port,
6505 &sas_expander->sas_port_list, port_list) {
6506
6507 if (mpt2sas_port->remote_identify.device_type ==
6508 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
6509 mpt2sas_port->remote_identify.device_type ==
6510 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
6511
6512 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6513 expander_sibling =
6514 mpt2sas_scsih_expander_find_by_sas_address(
6515 ioc, mpt2sas_port->remote_identify.sas_address);
6516 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6517 if (!expander_sibling)
6518 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306519 _scsih_expander_remove(ioc,
6520 expander_sibling->sas_address);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306521 if (ioc->shost_recovery)
6522 return;
Eric Moore635374e2009-03-09 01:21:12 -06006523 goto retry_expander_search;
6524 }
6525 }
6526
6527 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306528 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006529
6530 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6531 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6532 sas_expander->handle, (unsigned long long)
6533 sas_expander->sas_address);
6534
6535 list_del(&sas_expander->list);
6536 kfree(sas_expander->phy);
6537 kfree(sas_expander);
6538}
6539
6540/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05306541 * _scsih_ir_shutdown - IR shutdown notification
6542 * @ioc: per adapter object
6543 *
6544 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6545 * the host system is shutting down.
6546 *
6547 * Return nothing.
6548 */
6549static void
6550_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6551{
6552 Mpi2RaidActionRequest_t *mpi_request;
6553 Mpi2RaidActionReply_t *mpi_reply;
6554 u16 smid;
6555
6556 /* is IR firmware build loaded ? */
6557 if (!ioc->ir_firmware)
6558 return;
6559
6560 /* are there any volumes ? */
6561 if (list_empty(&ioc->raid_device_list))
6562 return;
6563
6564 mutex_lock(&ioc->scsih_cmds.mutex);
6565
6566 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6567 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6568 ioc->name, __func__);
6569 goto out;
6570 }
6571 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6572
6573 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6574 if (!smid) {
6575 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6576 ioc->name, __func__);
6577 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6578 goto out;
6579 }
6580
6581 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6582 ioc->scsih_cmds.smid = smid;
6583 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6584
6585 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6586 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6587
6588 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6589 init_completion(&ioc->scsih_cmds.done);
6590 mpt2sas_base_put_smid_default(ioc, smid);
6591 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6592
6593 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6594 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6595 ioc->name, __func__);
6596 goto out;
6597 }
6598
6599 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6600 mpi_reply = ioc->scsih_cmds.reply;
6601
6602 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6603 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6604 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6605 le32_to_cpu(mpi_reply->IOCLogInfo));
6606 }
6607
6608 out:
6609 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6610 mutex_unlock(&ioc->scsih_cmds.mutex);
6611}
6612
6613/**
6614 * _scsih_shutdown - routine call during system shutdown
6615 * @pdev: PCI device struct
6616 *
6617 * Return nothing.
6618 */
6619static void
6620_scsih_shutdown(struct pci_dev *pdev)
6621{
6622 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6623 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306624 struct workqueue_struct *wq;
6625 unsigned long flags;
6626
6627 ioc->remove_host = 1;
6628 _scsih_fw_event_cleanup_queue(ioc);
6629
6630 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6631 wq = ioc->firmware_event_thread;
6632 ioc->firmware_event_thread = NULL;
6633 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6634 if (wq)
6635 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306636
6637 _scsih_ir_shutdown(ioc);
6638 mpt2sas_base_detach(ioc);
6639}
6640
6641/**
Eric Moored5d135b2009-05-18 13:02:08 -06006642 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006643 * @pdev: PCI device struct
6644 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306645 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006646 * Return nothing.
6647 */
6648static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006649_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006650{
6651 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6652 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6653 struct _sas_port *mpt2sas_port;
6654 struct _sas_device *sas_device;
6655 struct _sas_node *expander_sibling;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306656 struct _raid_device *raid_device, *next;
6657 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006658 struct workqueue_struct *wq;
6659 unsigned long flags;
6660
6661 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306662 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006663
6664 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6665 wq = ioc->firmware_event_thread;
6666 ioc->firmware_event_thread = NULL;
6667 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6668 if (wq)
6669 destroy_workqueue(wq);
6670
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306671 /* release all the volumes */
6672 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6673 list) {
6674 if (raid_device->starget) {
6675 sas_target_priv_data =
6676 raid_device->starget->hostdata;
6677 sas_target_priv_data->deleted = 1;
6678 scsi_remove_target(&raid_device->starget->dev);
6679 }
6680 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6681 "(0x%016llx)\n", ioc->name, raid_device->handle,
6682 (unsigned long long) raid_device->wwid);
6683 _scsih_raid_device_remove(ioc, raid_device);
6684 }
6685
Eric Moore635374e2009-03-09 01:21:12 -06006686 /* free ports attached to the sas_host */
6687 retry_again:
6688 list_for_each_entry(mpt2sas_port,
6689 &ioc->sas_hba.sas_port_list, port_list) {
6690 if (mpt2sas_port->remote_identify.device_type ==
6691 SAS_END_DEVICE) {
6692 sas_device =
6693 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6694 mpt2sas_port->remote_identify.sas_address);
6695 if (sas_device) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306696 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006697 goto retry_again;
6698 }
6699 } else {
6700 expander_sibling =
6701 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6702 mpt2sas_port->remote_identify.sas_address);
6703 if (expander_sibling) {
6704 _scsih_expander_remove(ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306705 expander_sibling->sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006706 goto retry_again;
6707 }
6708 }
6709 }
6710
6711 /* free phys attached to the sas_host */
6712 if (ioc->sas_hba.num_phys) {
6713 kfree(ioc->sas_hba.phy);
6714 ioc->sas_hba.phy = NULL;
6715 ioc->sas_hba.num_phys = 0;
6716 }
6717
6718 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306719 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006720 list_del(&ioc->list);
6721 scsi_remove_host(shost);
6722 scsi_host_put(shost);
6723}
6724
6725/**
6726 * _scsih_probe_boot_devices - reports 1st device
6727 * @ioc: per adapter object
6728 *
6729 * If specified in bios page 2, this routine reports the 1st
6730 * device scsi-ml or sas transport for persistent boot device
6731 * purposes. Please refer to function _scsih_determine_boot_device()
6732 */
6733static void
6734_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6735{
6736 u8 is_raid;
6737 void *device;
6738 struct _sas_device *sas_device;
6739 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306740 u16 handle;
6741 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006742 u64 sas_address;
6743 unsigned long flags;
6744 int rc;
6745
6746 device = NULL;
6747 if (ioc->req_boot_device.device) {
6748 device = ioc->req_boot_device.device;
6749 is_raid = ioc->req_boot_device.is_raid;
6750 } else if (ioc->req_alt_boot_device.device) {
6751 device = ioc->req_alt_boot_device.device;
6752 is_raid = ioc->req_alt_boot_device.is_raid;
6753 } else if (ioc->current_boot_device.device) {
6754 device = ioc->current_boot_device.device;
6755 is_raid = ioc->current_boot_device.is_raid;
6756 }
6757
6758 if (!device)
6759 return;
6760
6761 if (is_raid) {
6762 raid_device = device;
6763 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6764 raid_device->id, 0);
6765 if (rc)
6766 _scsih_raid_device_remove(ioc, raid_device);
6767 } else {
6768 sas_device = device;
6769 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306770 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006771 sas_address = sas_device->sas_address;
6772 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6773 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6774 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6775 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306776 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006777 _scsih_sas_device_remove(ioc, sas_device);
6778 } else if (!sas_device->starget) {
6779 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306780 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006781 _scsih_sas_device_remove(ioc, sas_device);
6782 }
6783 }
6784}
6785
6786/**
6787 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6788 * @ioc: per adapter object
6789 *
6790 * Called during initial loading of the driver.
6791 */
6792static void
6793_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6794{
6795 struct _raid_device *raid_device, *raid_next;
6796 int rc;
6797
6798 list_for_each_entry_safe(raid_device, raid_next,
6799 &ioc->raid_device_list, list) {
6800 if (raid_device->starget)
6801 continue;
6802 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6803 raid_device->id, 0);
6804 if (rc)
6805 _scsih_raid_device_remove(ioc, raid_device);
6806 }
6807}
6808
6809/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306810 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006811 * @ioc: per adapter object
6812 *
6813 * Called during initial loading of the driver.
6814 */
6815static void
6816_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6817{
6818 struct _sas_device *sas_device, *next;
6819 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006820
6821 /* SAS Device List */
6822 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6823 list) {
6824 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6825 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6826 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6827
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306828 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6829 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006830 _scsih_sas_device_remove(ioc, sas_device);
6831 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306832 mpt2sas_transport_port_remove(ioc,
6833 sas_device->sas_address,
6834 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006835 _scsih_sas_device_remove(ioc, sas_device);
6836 }
6837 }
6838}
6839
6840/**
6841 * _scsih_probe_devices - probing for devices
6842 * @ioc: per adapter object
6843 *
6844 * Called during initial loading of the driver.
6845 */
6846static void
6847_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6848{
6849 u16 volume_mapping_flags =
6850 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6851 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6852
6853 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6854 return; /* return when IOC doesn't support initiator mode */
6855
6856 _scsih_probe_boot_devices(ioc);
6857
6858 if (ioc->ir_firmware) {
6859 if ((volume_mapping_flags &
6860 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6861 _scsih_probe_sas(ioc);
6862 _scsih_probe_raid(ioc);
6863 } else {
6864 _scsih_probe_raid(ioc);
6865 _scsih_probe_sas(ioc);
6866 }
6867 } else
6868 _scsih_probe_sas(ioc);
6869}
6870
6871/**
Eric Moored5d135b2009-05-18 13:02:08 -06006872 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006873 * @pdev: PCI device struct
6874 * @id: pci device id
6875 *
6876 * Returns 0 success, anything else error.
6877 */
6878static int
Eric Moored5d135b2009-05-18 13:02:08 -06006879_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006880{
6881 struct MPT2SAS_ADAPTER *ioc;
6882 struct Scsi_Host *shost;
6883
6884 shost = scsi_host_alloc(&scsih_driver_template,
6885 sizeof(struct MPT2SAS_ADAPTER));
6886 if (!shost)
6887 return -ENODEV;
6888
6889 /* init local params */
6890 ioc = shost_priv(shost);
6891 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6892 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006893 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006894 ioc->shost = shost;
6895 ioc->id = mpt_ids++;
6896 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6897 ioc->pdev = pdev;
6898 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6899 ioc->tm_cb_idx = tm_cb_idx;
6900 ioc->ctl_cb_idx = ctl_cb_idx;
6901 ioc->base_cb_idx = base_cb_idx;
6902 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306903 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006904 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306905 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306906 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306907 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006908 ioc->logging_level = logging_level;
6909 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05306910 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06006911 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6912 spin_lock_init(&ioc->scsi_lookup_lock);
6913 spin_lock_init(&ioc->sas_device_lock);
6914 spin_lock_init(&ioc->sas_node_lock);
6915 spin_lock_init(&ioc->fw_event_lock);
6916 spin_lock_init(&ioc->raid_device_lock);
6917
6918 INIT_LIST_HEAD(&ioc->sas_device_list);
6919 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6920 INIT_LIST_HEAD(&ioc->sas_expander_list);
6921 INIT_LIST_HEAD(&ioc->fw_event_list);
6922 INIT_LIST_HEAD(&ioc->raid_device_list);
6923 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306924 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306925 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06006926
6927 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06006928 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06006929 shost->max_lun = max_lun;
6930 shost->transportt = mpt2sas_transport_template;
6931 shost->unique_id = ioc->id;
6932
6933 if ((scsi_add_host(shost, &pdev->dev))) {
6934 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6935 ioc->name, __FILE__, __LINE__, __func__);
6936 list_del(&ioc->list);
6937 goto out_add_shost_fail;
6938 }
6939
Eric Moore3c621b32009-05-18 12:59:41 -06006940 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06006941 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306942 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006943
Eric Moore635374e2009-03-09 01:21:12 -06006944 /* event thread */
6945 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6946 "fw_event%d", ioc->id);
6947 ioc->firmware_event_thread = create_singlethread_workqueue(
6948 ioc->firmware_event_name);
6949 if (!ioc->firmware_event_thread) {
6950 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6951 ioc->name, __FILE__, __LINE__, __func__);
6952 goto out_thread_fail;
6953 }
6954
6955 ioc->wait_for_port_enable_to_complete = 1;
6956 if ((mpt2sas_base_attach(ioc))) {
6957 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6958 ioc->name, __FILE__, __LINE__, __func__);
6959 goto out_attach_fail;
6960 }
6961
6962 ioc->wait_for_port_enable_to_complete = 0;
6963 _scsih_probe_devices(ioc);
6964 return 0;
6965
6966 out_attach_fail:
6967 destroy_workqueue(ioc->firmware_event_thread);
6968 out_thread_fail:
6969 list_del(&ioc->list);
6970 scsi_remove_host(shost);
6971 out_add_shost_fail:
6972 return -ENODEV;
6973}
6974
6975#ifdef CONFIG_PM
6976/**
Eric Moored5d135b2009-05-18 13:02:08 -06006977 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006978 * @pdev: PCI device struct
6979 * @state: PM state change to (usually PCI_D3)
6980 *
6981 * Returns 0 success, anything else error.
6982 */
6983static int
Eric Moored5d135b2009-05-18 13:02:08 -06006984_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006985{
6986 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6987 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6988 u32 device_state;
6989
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306990 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006991 flush_scheduled_work();
6992 scsi_block_requests(shost);
6993 device_state = pci_choose_state(pdev, state);
6994 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6995 "operating state [D%d]\n", ioc->name, pdev,
6996 pci_name(pdev), device_state);
6997
6998 mpt2sas_base_free_resources(ioc);
6999 pci_save_state(pdev);
7000 pci_disable_device(pdev);
7001 pci_set_power_state(pdev, device_state);
7002 return 0;
7003}
7004
7005/**
Eric Moored5d135b2009-05-18 13:02:08 -06007006 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06007007 * @pdev: PCI device struct
7008 *
7009 * Returns 0 success, anything else error.
7010 */
7011static int
Eric Moored5d135b2009-05-18 13:02:08 -06007012_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007013{
7014 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7015 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7016 u32 device_state = pdev->current_state;
7017 int r;
7018
7019 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7020 "operating state [D%d]\n", ioc->name, pdev,
7021 pci_name(pdev), device_state);
7022
7023 pci_set_power_state(pdev, PCI_D0);
7024 pci_enable_wake(pdev, PCI_D0, 0);
7025 pci_restore_state(pdev);
7026 ioc->pdev = pdev;
7027 r = mpt2sas_base_map_resources(ioc);
7028 if (r)
7029 return r;
7030
7031 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7032 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05307033 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007034 return 0;
7035}
7036#endif /* CONFIG_PM */
7037
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307038/**
7039 * _scsih_pci_error_detected - Called when a PCI error is detected.
7040 * @pdev: PCI device struct
7041 * @state: PCI channel state
7042 *
7043 * Description: Called when a PCI error is detected.
7044 *
7045 * Return value:
7046 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7047 */
7048static pci_ers_result_t
7049_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7050{
7051 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7052 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7053
7054 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7055 ioc->name, state);
7056
7057 switch (state) {
7058 case pci_channel_io_normal:
7059 return PCI_ERS_RESULT_CAN_RECOVER;
7060 case pci_channel_io_frozen:
7061 scsi_block_requests(ioc->shost);
7062 mpt2sas_base_stop_watchdog(ioc);
7063 mpt2sas_base_free_resources(ioc);
7064 return PCI_ERS_RESULT_NEED_RESET;
7065 case pci_channel_io_perm_failure:
7066 _scsih_remove(pdev);
7067 return PCI_ERS_RESULT_DISCONNECT;
7068 }
7069 return PCI_ERS_RESULT_NEED_RESET;
7070}
7071
7072/**
7073 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7074 * @pdev: PCI device struct
7075 *
7076 * Description: This routine is called by the pci error recovery
7077 * code after the PCI slot has been reset, just before we
7078 * should resume normal operations.
7079 */
7080static pci_ers_result_t
7081_scsih_pci_slot_reset(struct pci_dev *pdev)
7082{
7083 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7084 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7085 int rc;
7086
7087 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7088 ioc->name);
7089
7090 ioc->pdev = pdev;
7091 rc = mpt2sas_base_map_resources(ioc);
7092 if (rc)
7093 return PCI_ERS_RESULT_DISCONNECT;
7094
7095
7096 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7097 FORCE_BIG_HAMMER);
7098
7099 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7100 (rc == 0) ? "success" : "failed");
7101
7102 if (!rc)
7103 return PCI_ERS_RESULT_RECOVERED;
7104 else
7105 return PCI_ERS_RESULT_DISCONNECT;
7106}
7107
7108/**
7109 * _scsih_pci_resume() - resume normal ops after PCI reset
7110 * @pdev: pointer to PCI device
7111 *
7112 * Called when the error recovery driver tells us that its
7113 * OK to resume normal operation. Use completion to allow
7114 * halted scsi ops to resume.
7115 */
7116static void
7117_scsih_pci_resume(struct pci_dev *pdev)
7118{
7119 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7120 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7121
7122 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7123
7124 pci_cleanup_aer_uncorrect_error_status(pdev);
7125 mpt2sas_base_start_watchdog(ioc);
7126 scsi_unblock_requests(ioc->shost);
7127}
7128
7129/**
7130 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7131 * @pdev: pointer to PCI device
7132 */
7133static pci_ers_result_t
7134_scsih_pci_mmio_enabled(struct pci_dev *pdev)
7135{
7136 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7137 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7138
7139 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7140 ioc->name);
7141
7142 /* TODO - dump whatever for debugging purposes */
7143
7144 /* Request a slot reset. */
7145 return PCI_ERS_RESULT_NEED_RESET;
7146}
7147
7148static struct pci_error_handlers _scsih_err_handler = {
7149 .error_detected = _scsih_pci_error_detected,
7150 .mmio_enabled = _scsih_pci_mmio_enabled,
7151 .slot_reset = _scsih_pci_slot_reset,
7152 .resume = _scsih_pci_resume,
7153};
Eric Moore635374e2009-03-09 01:21:12 -06007154
7155static struct pci_driver scsih_driver = {
7156 .name = MPT2SAS_DRIVER_NAME,
7157 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06007158 .probe = _scsih_probe,
7159 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05307160 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307161 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06007162#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06007163 .suspend = _scsih_suspend,
7164 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06007165#endif
7166};
7167
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307168/* raid transport support */
7169static struct raid_function_template mpt2sas_raid_functions = {
7170 .cookie = &scsih_driver_template,
7171 .is_raid = _scsih_is_raid,
7172 .get_resync = _scsih_get_resync,
7173 .get_state = _scsih_get_state,
7174};
Eric Moore635374e2009-03-09 01:21:12 -06007175
7176/**
Eric Moored5d135b2009-05-18 13:02:08 -06007177 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06007178 *
7179 * Returns 0 success, anything else error.
7180 */
7181static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06007182_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06007183{
7184 int error;
7185
7186 mpt_ids = 0;
7187 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7188 MPT2SAS_DRIVER_VERSION);
7189
7190 mpt2sas_transport_template =
7191 sas_attach_transport(&mpt2sas_transport_functions);
7192 if (!mpt2sas_transport_template)
7193 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307194 /* raid transport support */
7195 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7196 if (!mpt2sas_raid_template) {
7197 sas_release_transport(mpt2sas_transport_template);
7198 return -ENODEV;
7199 }
Eric Moore635374e2009-03-09 01:21:12 -06007200
7201 mpt2sas_base_initialize_callback_handler();
7202
7203 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06007204 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06007205
7206 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06007207 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06007208
7209 /* base internal commands callback handler */
7210 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7211
7212 /* transport internal commands callback handler */
7213 transport_cb_idx = mpt2sas_base_register_callback_handler(
7214 mpt2sas_transport_done);
7215
Kashyap, Desai744090d2009-10-05 15:56:56 +05307216 /* scsih internal commands callback handler */
7217 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7218
Eric Moore635374e2009-03-09 01:21:12 -06007219 /* configuration page API internal commands callback handler */
7220 config_cb_idx = mpt2sas_base_register_callback_handler(
7221 mpt2sas_config_done);
7222
7223 /* ctl module callback handler */
7224 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7225
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307226 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7227 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307228
7229 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7230 _scsih_tm_volume_tr_complete);
7231
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307232 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7233 _scsih_sas_control_complete);
7234
Eric Moore635374e2009-03-09 01:21:12 -06007235 mpt2sas_ctl_init();
7236
7237 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307238 if (error) {
7239 /* raid transport support */
7240 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06007241 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307242 }
Eric Moore635374e2009-03-09 01:21:12 -06007243
7244 return error;
7245}
7246
7247/**
Eric Moored5d135b2009-05-18 13:02:08 -06007248 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06007249 *
7250 * Returns 0 success, anything else error.
7251 */
7252static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06007253_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06007254{
7255 printk(KERN_INFO "mpt2sas version %s unloading\n",
7256 MPT2SAS_DRIVER_VERSION);
7257
7258 pci_unregister_driver(&scsih_driver);
7259
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307260 mpt2sas_ctl_exit();
7261
Eric Moore635374e2009-03-09 01:21:12 -06007262 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7263 mpt2sas_base_release_callback_handler(tm_cb_idx);
7264 mpt2sas_base_release_callback_handler(base_cb_idx);
7265 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307266 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06007267 mpt2sas_base_release_callback_handler(config_cb_idx);
7268 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7269
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307270 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307271 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307272 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7273
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307274 /* raid transport support */
7275 raid_class_release(mpt2sas_raid_template);
7276 sas_release_transport(mpt2sas_transport_template);
7277
Eric Moore635374e2009-03-09 01:21:12 -06007278}
7279
Eric Moored5d135b2009-05-18 13:02:08 -06007280module_init(_scsih_init);
7281module_exit(_scsih_exit);