blob: 228961b94a68fbce3e4c05aaaaf0aab7b62638bd [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/**
Eric Moored5d135b2009-05-18 13:02:08 -06002126 * _scsih_abort - eh threads main abort routine
Eric Moore635374e2009-03-09 01:21:12 -06002127 * @sdev: scsi device struct
2128 *
2129 * Returns SUCCESS if command aborted else FAILED
2130 */
2131static int
Eric Moored5d135b2009-05-18 13:02:08 -06002132_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002133{
2134 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2135 struct MPT2SAS_DEVICE *sas_device_priv_data;
2136 u16 smid;
2137 u16 handle;
2138 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002139
2140 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
2141 ioc->name, scmd);
2142 scsi_print_command(scmd);
2143
2144 sas_device_priv_data = scmd->device->hostdata;
2145 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2146 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2147 ioc->name, scmd);
2148 scmd->result = DID_NO_CONNECT << 16;
2149 scmd->scsi_done(scmd);
2150 r = SUCCESS;
2151 goto out;
2152 }
2153
2154 /* search for the command */
2155 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2156 if (!smid) {
2157 scmd->result = DID_RESET << 16;
2158 r = SUCCESS;
2159 goto out;
2160 }
2161
2162 /* for hidden raid components and volumes this is not supported */
2163 if (sas_device_priv_data->sas_target->flags &
2164 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2165 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2166 scmd->result = DID_RESET << 16;
2167 r = FAILED;
2168 goto out;
2169 }
2170
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302171 mpt2sas_halt_firmware(ioc);
2172
Eric Moore635374e2009-03-09 01:21:12 -06002173 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302174 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2175 scmd->device->id, scmd->device->lun,
2176 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002177
2178 out:
2179 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
2180 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2181 return r;
2182}
2183
Eric Moore635374e2009-03-09 01:21:12 -06002184/**
Eric Moored5d135b2009-05-18 13:02:08 -06002185 * _scsih_dev_reset - eh threads main device reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002186 * @sdev: scsi device struct
2187 *
2188 * Returns SUCCESS if command aborted else FAILED
2189 */
2190static int
Eric Moored5d135b2009-05-18 13:02:08 -06002191_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002192{
2193 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2194 struct MPT2SAS_DEVICE *sas_device_priv_data;
2195 struct _sas_device *sas_device;
2196 unsigned long flags;
2197 u16 handle;
2198 int r;
2199
Eric Moore993e0da2009-05-18 13:00:45 -06002200 printk(MPT2SAS_INFO_FMT "attempting device reset! scmd(%p)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002201 ioc->name, scmd);
2202 scsi_print_command(scmd);
2203
2204 sas_device_priv_data = scmd->device->hostdata;
2205 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2206 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
2207 ioc->name, scmd);
2208 scmd->result = DID_NO_CONNECT << 16;
2209 scmd->scsi_done(scmd);
2210 r = SUCCESS;
2211 goto out;
2212 }
2213
2214 /* for hidden raid components obtain the volume_handle */
2215 handle = 0;
2216 if (sas_device_priv_data->sas_target->flags &
2217 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2218 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2219 sas_device = _scsih_sas_device_find_by_handle(ioc,
2220 sas_device_priv_data->sas_target->handle);
2221 if (sas_device)
2222 handle = sas_device->volume_handle;
2223 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2224 } else
2225 handle = sas_device_priv_data->sas_target->handle;
2226
2227 if (!handle) {
2228 scmd->result = DID_RESET << 16;
2229 r = FAILED;
2230 goto out;
2231 }
2232
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302233 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2234 scmd->device->id, scmd->device->lun,
2235 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002236
2237 out:
2238 printk(MPT2SAS_INFO_FMT "device reset: %s scmd(%p)\n",
2239 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2240 return r;
2241}
2242
2243/**
Eric Moored5d135b2009-05-18 13:02:08 -06002244 * _scsih_target_reset - eh threads main target reset routine
Eric Moore993e0da2009-05-18 13:00:45 -06002245 * @sdev: scsi device struct
2246 *
2247 * Returns SUCCESS if command aborted else FAILED
2248 */
2249static int
Eric Moored5d135b2009-05-18 13:02:08 -06002250_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002251{
2252 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2253 struct MPT2SAS_DEVICE *sas_device_priv_data;
2254 struct _sas_device *sas_device;
2255 unsigned long flags;
2256 u16 handle;
2257 int r;
2258
2259 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
2260 ioc->name, scmd);
2261 scsi_print_command(scmd);
2262
2263 sas_device_priv_data = scmd->device->hostdata;
2264 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2265 printk(MPT2SAS_INFO_FMT "target been deleted! scmd(%p)\n",
2266 ioc->name, scmd);
2267 scmd->result = DID_NO_CONNECT << 16;
2268 scmd->scsi_done(scmd);
2269 r = SUCCESS;
2270 goto out;
2271 }
2272
2273 /* for hidden raid components obtain the volume_handle */
2274 handle = 0;
2275 if (sas_device_priv_data->sas_target->flags &
2276 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2277 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2278 sas_device = _scsih_sas_device_find_by_handle(ioc,
2279 sas_device_priv_data->sas_target->handle);
2280 if (sas_device)
2281 handle = sas_device->volume_handle;
2282 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2283 } else
2284 handle = sas_device_priv_data->sas_target->handle;
2285
2286 if (!handle) {
2287 scmd->result = DID_RESET << 16;
2288 r = FAILED;
2289 goto out;
2290 }
2291
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302292 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2293 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2294 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002295
2296 out:
2297 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
2298 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2299 return r;
2300}
2301
2302/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302303 * _scsih_host_reset - eh threads main host reset routine
Eric Moore635374e2009-03-09 01:21:12 -06002304 * @sdev: scsi device struct
2305 *
2306 * Returns SUCCESS if command aborted else FAILED
2307 */
2308static int
Eric Moored5d135b2009-05-18 13:02:08 -06002309_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002310{
2311 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2312 int r, retval;
2313
2314 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2315 ioc->name, scmd);
2316 scsi_print_command(scmd);
2317
2318 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2319 FORCE_BIG_HAMMER);
2320 r = (retval < 0) ? FAILED : SUCCESS;
2321 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2322 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2323
2324 return r;
2325}
2326
2327/**
2328 * _scsih_fw_event_add - insert and queue up fw_event
2329 * @ioc: per adapter object
2330 * @fw_event: object describing the event
2331 * Context: This function will acquire ioc->fw_event_lock.
2332 *
2333 * This adds the firmware event object into link list, then queues it up to
2334 * be processed from user context.
2335 *
2336 * Return nothing.
2337 */
2338static void
2339_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2340{
2341 unsigned long flags;
2342
2343 if (ioc->firmware_event_thread == NULL)
2344 return;
2345
2346 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2347 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302348 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2349 queue_delayed_work(ioc->firmware_event_thread,
2350 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002351 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2352}
2353
2354/**
2355 * _scsih_fw_event_free - delete fw_event
2356 * @ioc: per adapter object
2357 * @fw_event: object describing the event
2358 * Context: This function will acquire ioc->fw_event_lock.
2359 *
2360 * This removes firmware event object from link list, frees associated memory.
2361 *
2362 * Return nothing.
2363 */
2364static void
2365_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2366 *fw_event)
2367{
2368 unsigned long flags;
2369
2370 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2371 list_del(&fw_event->list);
2372 kfree(fw_event->event_data);
2373 kfree(fw_event);
2374 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2375}
2376
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302377
Eric Moore635374e2009-03-09 01:21:12 -06002378/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302379 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002380 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002381 *
2382 * Return nothing.
2383 */
2384static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302385_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002386{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302387 struct fw_event_work *fw_event;
2388
2389 if (ioc->wait_for_port_enable_to_complete)
2390 return;
2391 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2392 if (!fw_event)
2393 return;
2394 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2395 fw_event->ioc = ioc;
2396 _scsih_fw_event_add(ioc, fw_event);
2397}
2398
2399/**
2400 * _scsih_fw_event_cleanup_queue - cleanup event queue
2401 * @ioc: per adapter object
2402 *
2403 * Walk the firmware event queue, either killing timers, or waiting
2404 * for outstanding events to complete
2405 *
2406 * Return nothing.
2407 */
2408static void
2409_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2410{
2411 struct fw_event_work *fw_event, *next;
2412
2413 if (list_empty(&ioc->fw_event_list) ||
2414 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002415 return;
2416
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302417 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2418 if (cancel_delayed_work(&fw_event->delayed_work)) {
2419 _scsih_fw_event_free(ioc, fw_event);
2420 continue;
2421 }
2422 fw_event->cancel_pending_work = 1;
2423 }
Eric Moore635374e2009-03-09 01:21:12 -06002424}
2425
Eric Moore635374e2009-03-09 01:21:12 -06002426/**
2427 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2428 * @ioc: per adapter object
2429 * @handle: device handle
2430 *
2431 * During device pull we need to appropiately set the sdev state.
2432 */
2433static void
2434_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2435{
2436 struct MPT2SAS_DEVICE *sas_device_priv_data;
2437 struct scsi_device *sdev;
2438
2439 shost_for_each_device(sdev, ioc->shost) {
2440 sas_device_priv_data = sdev->hostdata;
2441 if (!sas_device_priv_data)
2442 continue;
2443 if (!sas_device_priv_data->block)
2444 continue;
2445 if (sas_device_priv_data->sas_target->handle == handle) {
2446 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2447 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2448 "handle(0x%04x)\n", ioc->name, handle));
2449 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302450 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002451 }
2452 }
2453}
2454
2455/**
2456 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2457 * @ioc: per adapter object
2458 * @handle: device handle
2459 *
2460 * During device pull we need to appropiately set the sdev state.
2461 */
2462static void
2463_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2464{
2465 struct MPT2SAS_DEVICE *sas_device_priv_data;
2466 struct scsi_device *sdev;
2467
2468 shost_for_each_device(sdev, ioc->shost) {
2469 sas_device_priv_data = sdev->hostdata;
2470 if (!sas_device_priv_data)
2471 continue;
2472 if (sas_device_priv_data->block)
2473 continue;
2474 if (sas_device_priv_data->sas_target->handle == handle) {
2475 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2476 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2477 "handle(0x%04x)\n", ioc->name, handle));
2478 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302479 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002480 }
2481 }
2482}
2483
2484/**
2485 * _scsih_block_io_to_children_attached_to_ex
2486 * @ioc: per adapter object
2487 * @sas_expander: the sas_device object
2488 *
2489 * This routine set sdev state to SDEV_BLOCK for all devices
2490 * attached to this expander. This function called when expander is
2491 * pulled.
2492 */
2493static void
2494_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2495 struct _sas_node *sas_expander)
2496{
2497 struct _sas_port *mpt2sas_port;
2498 struct _sas_device *sas_device;
2499 struct _sas_node *expander_sibling;
2500 unsigned long flags;
2501
2502 if (!sas_expander)
2503 return;
2504
2505 list_for_each_entry(mpt2sas_port,
2506 &sas_expander->sas_port_list, port_list) {
2507 if (mpt2sas_port->remote_identify.device_type ==
2508 SAS_END_DEVICE) {
2509 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2510 sas_device =
2511 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2512 mpt2sas_port->remote_identify.sas_address);
2513 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2514 if (!sas_device)
2515 continue;
2516 _scsih_block_io_device(ioc, sas_device->handle);
2517 }
2518 }
2519
2520 list_for_each_entry(mpt2sas_port,
2521 &sas_expander->sas_port_list, port_list) {
2522
2523 if (mpt2sas_port->remote_identify.device_type ==
2524 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2525 mpt2sas_port->remote_identify.device_type ==
2526 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2527
2528 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2529 expander_sibling =
2530 mpt2sas_scsih_expander_find_by_sas_address(
2531 ioc, mpt2sas_port->remote_identify.sas_address);
2532 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2533 _scsih_block_io_to_children_attached_to_ex(ioc,
2534 expander_sibling);
2535 }
2536 }
2537}
2538
2539/**
2540 * _scsih_block_io_to_children_attached_directly
2541 * @ioc: per adapter object
2542 * @event_data: topology change event data
2543 *
2544 * This routine set sdev state to SDEV_BLOCK for all devices
2545 * direct attached during device pull.
2546 */
2547static void
2548_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2549 Mpi2EventDataSasTopologyChangeList_t *event_data)
2550{
2551 int i;
2552 u16 handle;
2553 u16 reason_code;
2554 u8 phy_number;
2555
2556 for (i = 0; i < event_data->NumEntries; i++) {
2557 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2558 if (!handle)
2559 continue;
2560 phy_number = event_data->StartPhyNum + i;
2561 reason_code = event_data->PHY[i].PhyStatus &
2562 MPI2_EVENT_SAS_TOPO_RC_MASK;
2563 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2564 _scsih_block_io_device(ioc, handle);
2565 }
2566}
2567
2568/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302569 * _scsih_tm_tr_send - send task management request
2570 * @ioc: per adapter object
2571 * @handle: device handle
2572 * Context: interrupt time.
2573 *
2574 * This code is to initiate the device removal handshake protocal
2575 * with controller firmware. This function will issue target reset
2576 * using high priority request queue. It will send a sas iounit
2577 * controll request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2578 *
2579 * This is designed to send muliple task management request at the same
2580 * time to the fifo. If the fifo is full, we will append the request,
2581 * and process it in a future completion.
2582 */
2583static void
2584_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2585{
2586 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302587 u16 smid;
2588 struct _sas_device *sas_device;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302589 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302590 unsigned long flags;
2591 struct _tr_list *delayed_tr;
2592
Kashyap, Desai1278b112010-03-09 17:34:13 +05302593 if (ioc->shost_recovery || ioc->remove_host) {
2594 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2595 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302596 return;
2597 }
2598
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302599 /* if PD, then return */
2600 if (test_bit(handle, ioc->pd_handles))
2601 return;
2602
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2604 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302605 if (sas_device && sas_device->starget &&
2606 sas_device->starget->hostdata) {
2607 sas_target_priv_data = sas_device->starget->hostdata;
2608 sas_target_priv_data->deleted = 1;
2609 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2610 "setting delete flag: handle(0x%04x), "
2611 "sas_addr(0x%016llx)\n", ioc->name, handle,
2612 (unsigned long long) sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05302613 }
2614 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302615
2616 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2617 if (!smid) {
2618 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2619 if (!delayed_tr)
2620 return;
2621 INIT_LIST_HEAD(&delayed_tr->list);
2622 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302623 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2624 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2625 "DELAYED:tr:handle(0x%04x), (open)\n",
2626 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302627 return;
2628 }
2629
Kashyap, Desai1278b112010-03-09 17:34:13 +05302630 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2631 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2632 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302633 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2634 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2635 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2636 mpi_request->DevHandle = cpu_to_le16(handle);
2637 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302638 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2639}
2640
2641
2642
2643/**
2644 * _scsih_sas_control_complete - completion routine
2645 * @ioc: per adapter object
2646 * @smid: system request message index
2647 * @msix_index: MSIX table index supplied by the OS
2648 * @reply: reply message frame(lower 32bit addr)
2649 * Context: interrupt time.
2650 *
2651 * This is the sas iounit controll completion routine.
2652 * This code is part of the code to initiate the device removal
2653 * handshake protocal with controller firmware.
2654 *
2655 * Return 1 meaning mf should be freed from _base_interrupt
2656 * 0 means the mf is freed from this function.
2657 */
2658static u8
2659_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2660 u8 msix_index, u32 reply)
2661{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302662 Mpi2SasIoUnitControlReply_t *mpi_reply =
2663 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2664
Kashyap, Desai1278b112010-03-09 17:34:13 +05302665 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2666 "sc_complete:handle(0x%04x), (open) "
2667 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2668 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2669 le16_to_cpu(mpi_reply->IOCStatus),
2670 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302671 return 1;
2672}
2673
2674/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302675 * _scsih_tm_tr_volume_send - send target reset request for volumes
2676 * @ioc: per adapter object
2677 * @handle: device handle
2678 * Context: interrupt time.
2679 *
2680 * This is designed to send muliple task management request at the same
2681 * time to the fifo. If the fifo is full, we will append the request,
2682 * and process it in a future completion.
2683 */
2684static void
2685_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2686{
2687 Mpi2SCSITaskManagementRequest_t *mpi_request;
2688 u16 smid;
2689 struct _tr_list *delayed_tr;
2690
2691 if (ioc->shost_recovery || ioc->remove_host) {
2692 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2693 "progress!\n", __func__, ioc->name));
2694 return;
2695 }
2696
2697 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
2698 if (!smid) {
2699 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2700 if (!delayed_tr)
2701 return;
2702 INIT_LIST_HEAD(&delayed_tr->list);
2703 delayed_tr->handle = handle;
2704 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
2705 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2706 "DELAYED:tr:handle(0x%04x), (open)\n",
2707 ioc->name, handle));
2708 return;
2709 }
2710
2711 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2712 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2713 ioc->tm_tr_volume_cb_idx));
2714 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2715 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2716 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2717 mpi_request->DevHandle = cpu_to_le16(handle);
2718 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2719 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2720}
2721
2722/**
2723 * _scsih_tm_volume_tr_complete - target reset completion
2724 * @ioc: per adapter object
2725 * @smid: system request message index
2726 * @msix_index: MSIX table index supplied by the OS
2727 * @reply: reply message frame(lower 32bit addr)
2728 * Context: interrupt time.
2729 *
2730 * Return 1 meaning mf should be freed from _base_interrupt
2731 * 0 means the mf is freed from this function.
2732 */
2733static u8
2734_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2735 u8 msix_index, u32 reply)
2736{
2737 u16 handle;
2738 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2739 Mpi2SCSITaskManagementReply_t *mpi_reply =
2740 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2741
2742 if (ioc->shost_recovery || ioc->remove_host) {
2743 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2744 "progress!\n", __func__, ioc->name));
2745 return 1;
2746 }
2747
2748 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2749 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2750 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2751 dewtprintk(ioc, printk("spurious interrupt: "
2752 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
2753 le16_to_cpu(mpi_reply->DevHandle), smid));
2754 return 0;
2755 }
2756
2757 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2758 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2759 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2760 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2761 le32_to_cpu(mpi_reply->IOCLogInfo),
2762 le32_to_cpu(mpi_reply->TerminationCount)));
2763
2764 return _scsih_check_for_pending_tm(ioc, smid);
2765}
2766
2767/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302768 * _scsih_tm_tr_complete -
2769 * @ioc: per adapter object
2770 * @smid: system request message index
2771 * @msix_index: MSIX table index supplied by the OS
2772 * @reply: reply message frame(lower 32bit addr)
2773 * Context: interrupt time.
2774 *
2775 * This is the target reset completion routine.
2776 * This code is part of the code to initiate the device removal
2777 * handshake protocal with controller firmware.
2778 * It will send a sas iounit controll request (MPI2_SAS_OP_REMOVE_DEVICE)
2779 *
2780 * Return 1 meaning mf should be freed from _base_interrupt
2781 * 0 means the mf is freed from this function.
2782 */
2783static u8
2784_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2785 u32 reply)
2786{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302787 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302788 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302789 Mpi2SCSITaskManagementReply_t *mpi_reply =
2790 mpt2sas_base_get_reply_virt_addr(ioc, reply);
2791 Mpi2SasIoUnitControlRequest_t *mpi_request;
2792 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302793
Kashyap, Desai1278b112010-03-09 17:34:13 +05302794 if (ioc->shost_recovery || ioc->remove_host) {
2795 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2796 "progress!\n", __func__, ioc->name));
2797 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302798 }
2799
Kashyap, Desai1278b112010-03-09 17:34:13 +05302800 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
2801 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2802 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2803 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
2804 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
2805 le16_to_cpu(mpi_reply->DevHandle), smid));
2806 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302807 }
2808
Kashyap, Desai1278b112010-03-09 17:34:13 +05302809 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2810 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
2811 "loginfo(0x%08x), completed(%d)\n", ioc->name,
2812 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
2813 le32_to_cpu(mpi_reply->IOCLogInfo),
2814 le32_to_cpu(mpi_reply->TerminationCount)));
2815
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302816 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
2817 if (!smid_sas_ctrl) {
2818 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2819 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302820 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302821 }
2822
Kashyap, Desai1278b112010-03-09 17:34:13 +05302823 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
2824 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
2825 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302826 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
2827 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
2828 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
2829 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302830 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302831 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05302832
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302833 return _scsih_check_for_pending_tm(ioc, smid);
2834}
2835
2836/**
2837 * _scsih_check_for_pending_tm - check for pending task management
2838 * @ioc: per adapter object
2839 * @smid: system request message index
2840 *
2841 * This will check delayed target reset list, and feed the
2842 * next reqeust.
2843 *
2844 * Return 1 meaning mf should be freed from _base_interrupt
2845 * 0 means the mf is freed from this function.
2846 */
2847static u8
2848_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
2849{
2850 struct _tr_list *delayed_tr;
2851
2852 if (!list_empty(&ioc->delayed_tr_volume_list)) {
2853 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
2854 struct _tr_list, list);
2855 mpt2sas_base_free_smid(ioc, smid);
2856 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
2857 list_del(&delayed_tr->list);
2858 kfree(delayed_tr);
2859 return 0;
2860 }
2861
Kashyap, Desai1278b112010-03-09 17:34:13 +05302862 if (!list_empty(&ioc->delayed_tr_list)) {
2863 delayed_tr = list_entry(ioc->delayed_tr_list.next,
2864 struct _tr_list, list);
2865 mpt2sas_base_free_smid(ioc, smid);
2866 _scsih_tm_tr_send(ioc, delayed_tr->handle);
2867 list_del(&delayed_tr->list);
2868 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302869 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302870 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302871
Kashyap, Desai1278b112010-03-09 17:34:13 +05302872 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302873}
2874
2875/**
Eric Moore635374e2009-03-09 01:21:12 -06002876 * _scsih_check_topo_delete_events - sanity check on topo events
2877 * @ioc: per adapter object
2878 * @event_data: the event data payload
2879 *
2880 * This routine added to better handle cable breaker.
2881 *
2882 * This handles the case where driver recieves multiple expander
2883 * add and delete events in a single shot. When there is a delete event
2884 * the routine will void any pending add events waiting in the event queue.
2885 *
2886 * Return nothing.
2887 */
2888static void
2889_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2890 Mpi2EventDataSasTopologyChangeList_t *event_data)
2891{
2892 struct fw_event_work *fw_event;
2893 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2894 u16 expander_handle;
2895 struct _sas_node *sas_expander;
2896 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302897 int i, reason_code;
2898 u16 handle;
2899
2900 for (i = 0 ; i < event_data->NumEntries; i++) {
2901 if (event_data->PHY[i].PhyStatus &
2902 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
2903 continue;
2904 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2905 if (!handle)
2906 continue;
2907 reason_code = event_data->PHY[i].PhyStatus &
2908 MPI2_EVENT_SAS_TOPO_RC_MASK;
2909 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
2910 _scsih_tm_tr_send(ioc, handle);
2911 }
Eric Moore635374e2009-03-09 01:21:12 -06002912
2913 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2914 if (expander_handle < ioc->sas_hba.num_phys) {
2915 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2916 return;
2917 }
2918
2919 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2920 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2921 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2922 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2923 expander_handle);
2924 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2925 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2926 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2927 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2928
2929 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2930 return;
2931
2932 /* mark ignore flag for pending events */
2933 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2934 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2935 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2936 fw_event->ignore)
2937 continue;
2938 local_event_data = fw_event->event_data;
2939 if (local_event_data->ExpStatus ==
2940 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2941 local_event_data->ExpStatus ==
2942 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2943 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2944 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302945 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06002946 "setting ignoring flag\n", ioc->name));
2947 fw_event->ignore = 1;
2948 }
2949 }
2950 }
2951 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2952}
2953
2954/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302955 * _scsih_set_volume_delete_flag - setting volume delete flag
2956 * @ioc: per adapter object
2957 * @handle: device handle
2958 *
2959 * This
2960 * Return nothing.
2961 */
2962static void
2963_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2964{
2965 struct _raid_device *raid_device;
2966 struct MPT2SAS_TARGET *sas_target_priv_data;
2967 unsigned long flags;
2968
2969 spin_lock_irqsave(&ioc->raid_device_lock, flags);
2970 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
2971 if (raid_device && raid_device->starget &&
2972 raid_device->starget->hostdata) {
2973 sas_target_priv_data =
2974 raid_device->starget->hostdata;
2975 sas_target_priv_data->deleted = 1;
2976 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2977 "setting delete flag: handle(0x%04x), "
2978 "wwid(0x%016llx)\n", ioc->name, handle,
2979 (unsigned long long) raid_device->wwid));
2980 }
2981 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2982}
2983
2984/**
2985 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
2986 * @handle: input handle
2987 * @a: handle for volume a
2988 * @b: handle for volume b
2989 *
2990 * IR firmware only supports two raid volumes. The purpose of this
2991 * routine is to set the volume handle in either a or b. When the given
2992 * input handle is non-zero, or when a and b have not been set before.
2993 */
2994static void
2995_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
2996{
2997 if (!handle || handle == *a || handle == *b)
2998 return;
2999 if (!*a)
3000 *a = handle;
3001 else if (!*b)
3002 *b = handle;
3003}
3004
3005/**
3006 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3007 * @ioc: per adapter object
3008 * @event_data: the event data payload
3009 * Context: interrupt time.
3010 *
3011 * This routine will send target reset to volume, followed by target
3012 * resets to the PDs. This is called when a PD has been removed, or
3013 * volume has been deleted or removed. When the target reset is sent
3014 * to volume, the PD target resets need to be queued to start upon
3015 * completion of the volume target reset.
3016 *
3017 * Return nothing.
3018 */
3019static void
3020_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3021 Mpi2EventDataIrConfigChangeList_t *event_data)
3022{
3023 Mpi2EventIrConfigElement_t *element;
3024 int i;
3025 u16 handle, volume_handle, a, b;
3026 struct _tr_list *delayed_tr;
3027
3028 a = 0;
3029 b = 0;
3030
3031 /* Volume Resets for Deleted or Removed */
3032 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3033 for (i = 0; i < event_data->NumElements; i++, element++) {
3034 if (element->ReasonCode ==
3035 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3036 element->ReasonCode ==
3037 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3038 volume_handle = le16_to_cpu(element->VolDevHandle);
3039 _scsih_set_volume_delete_flag(ioc, volume_handle);
3040 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3041 }
3042 }
3043
3044 /* Volume Resets for UNHIDE events */
3045 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3046 for (i = 0; i < event_data->NumElements; i++, element++) {
3047 if (le32_to_cpu(event_data->Flags) &
3048 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3049 continue;
3050 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3051 volume_handle = le16_to_cpu(element->VolDevHandle);
3052 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3053 }
3054 }
3055
3056 if (a)
3057 _scsih_tm_tr_volume_send(ioc, a);
3058 if (b)
3059 _scsih_tm_tr_volume_send(ioc, b);
3060
3061 /* PD target resets */
3062 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3063 for (i = 0; i < event_data->NumElements; i++, element++) {
3064 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3065 continue;
3066 handle = le16_to_cpu(element->PhysDiskDevHandle);
3067 volume_handle = le16_to_cpu(element->VolDevHandle);
3068 clear_bit(handle, ioc->pd_handles);
3069 if (!volume_handle)
3070 _scsih_tm_tr_send(ioc, handle);
3071 else if (volume_handle == a || volume_handle == b) {
3072 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3073 BUG_ON(!delayed_tr);
3074 INIT_LIST_HEAD(&delayed_tr->list);
3075 delayed_tr->handle = handle;
3076 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3077 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3078 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3079 handle));
3080 } else
3081 _scsih_tm_tr_send(ioc, handle);
3082 }
3083}
3084
3085
3086/**
3087 * _scsih_check_volume_delete_events - set delete flag for volumes
3088 * @ioc: per adapter object
3089 * @event_data: the event data payload
3090 * Context: interrupt time.
3091 *
3092 * This will handle the case when the cable connected to entire volume is
3093 * pulled. We will take care of setting the deleted flag so normal IO will
3094 * not be sent.
3095 *
3096 * Return nothing.
3097 */
3098static void
3099_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3100 Mpi2EventDataIrVolume_t *event_data)
3101{
3102 u32 state;
3103
3104 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3105 return;
3106 state = le32_to_cpu(event_data->NewValue);
3107 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3108 MPI2_RAID_VOL_STATE_FAILED)
3109 _scsih_set_volume_delete_flag(ioc,
3110 le16_to_cpu(event_data->VolDevHandle));
3111}
3112
3113/**
Eric Moore635374e2009-03-09 01:21:12 -06003114 * _scsih_flush_running_cmds - completing outstanding commands.
3115 * @ioc: per adapter object
3116 *
3117 * The flushing out of all pending scmd commands following host reset,
3118 * where all IO is dropped to the floor.
3119 *
3120 * Return nothing.
3121 */
3122static void
3123_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3124{
3125 struct scsi_cmnd *scmd;
3126 u16 smid;
3127 u16 count = 0;
3128
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303129 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3130 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003131 if (!scmd)
3132 continue;
3133 count++;
3134 mpt2sas_base_free_smid(ioc, smid);
3135 scsi_dma_unmap(scmd);
3136 scmd->result = DID_RESET << 16;
3137 scmd->scsi_done(scmd);
3138 }
3139 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3140 ioc->name, count));
3141}
3142
3143/**
Eric Moore3c621b32009-05-18 12:59:41 -06003144 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3145 * @scmd: pointer to scsi command object
3146 * @mpi_request: pointer to the SCSI_IO reqest message frame
3147 *
3148 * Supporting protection 1 and 3.
3149 *
3150 * Returns nothing
3151 */
3152static void
3153_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3154{
3155 u16 eedp_flags;
3156 unsigned char prot_op = scsi_get_prot_op(scmd);
3157 unsigned char prot_type = scsi_get_prot_type(scmd);
3158
Eric Moored334aa72010-04-22 10:47:40 -06003159 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003160 return;
3161
3162 if (prot_op == SCSI_PROT_READ_STRIP)
3163 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3164 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3165 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3166 else
3167 return;
3168
Eric Moore3c621b32009-05-18 12:59:41 -06003169 switch (prot_type) {
3170 case SCSI_PROT_DIF_TYPE1:
3171
3172 /*
3173 * enable ref/guard checking
3174 * auto increment ref tag
3175 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303176 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003177 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3178 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3179 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3180 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003181 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003182
Eric Moored334aa72010-04-22 10:47:40 -06003183 case SCSI_PROT_DIF_TYPE2:
3184
3185 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3186 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3187 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003188 break;
3189
3190 case SCSI_PROT_DIF_TYPE3:
3191
3192 /*
3193 * enable guard checking
3194 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303195 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003196 break;
3197 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303198 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3199 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003200}
3201
3202/**
3203 * _scsih_eedp_error_handling - return sense code for EEDP errors
3204 * @scmd: pointer to scsi command object
3205 * @ioc_status: ioc status
3206 *
3207 * Returns nothing
3208 */
3209static void
3210_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3211{
3212 u8 ascq;
3213 u8 sk;
3214 u8 host_byte;
3215
3216 switch (ioc_status) {
3217 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3218 ascq = 0x01;
3219 break;
3220 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3221 ascq = 0x02;
3222 break;
3223 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3224 ascq = 0x03;
3225 break;
3226 default:
3227 ascq = 0x00;
3228 break;
3229 }
3230
3231 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3232 sk = ILLEGAL_REQUEST;
3233 host_byte = DID_ABORT;
3234 } else {
3235 sk = ABORTED_COMMAND;
3236 host_byte = DID_OK;
3237 }
3238
3239 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3240 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3241 SAM_STAT_CHECK_CONDITION;
3242}
3243
3244/**
Eric Moored5d135b2009-05-18 13:02:08 -06003245 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003246 * @scmd: pointer to scsi command object
3247 * @done: function pointer to be invoked on completion
3248 *
3249 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3250 *
3251 * Returns 0 on success. If there's a failure, return either:
3252 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3253 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3254 */
3255static int
Eric Moored5d135b2009-05-18 13:02:08 -06003256_scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003257{
3258 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3259 struct MPT2SAS_DEVICE *sas_device_priv_data;
3260 struct MPT2SAS_TARGET *sas_target_priv_data;
3261 Mpi2SCSIIORequest_t *mpi_request;
3262 u32 mpi_control;
3263 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003264
3265 scmd->scsi_done = done;
3266 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303267 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003268 scmd->result = DID_NO_CONNECT << 16;
3269 scmd->scsi_done(scmd);
3270 return 0;
3271 }
3272
3273 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303274 /* invalid device handle */
3275 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003276 scmd->result = DID_NO_CONNECT << 16;
3277 scmd->scsi_done(scmd);
3278 return 0;
3279 }
3280
Kashyap, Desai130b9582010-04-08 17:54:32 +05303281 /* host recovery or link resets sent via IOCTLs */
3282 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003283 return SCSI_MLQUEUE_HOST_BUSY;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303284 /* device busy with task managment */
3285 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3286 return SCSI_MLQUEUE_DEVICE_BUSY;
3287 /* device has been deleted */
3288 else if (sas_target_priv_data->deleted) {
3289 scmd->result = DID_NO_CONNECT << 16;
3290 scmd->scsi_done(scmd);
3291 return 0;
3292 }
Eric Moore635374e2009-03-09 01:21:12 -06003293
3294 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3295 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3296 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3297 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3298 else
3299 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3300
3301 /* set tags */
3302 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3303 if (scmd->device->tagged_supported) {
3304 if (scmd->device->ordered_tags)
3305 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3306 else
3307 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3308 } else
3309/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3310/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3311 */
3312 mpi_control |= (0x500);
3313
3314 } else
3315 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303316 /* Make sure Device is not raid volume */
3317 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003318 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003319 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3320
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303321 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003322 if (!smid) {
3323 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3324 ioc->name, __func__);
3325 goto out;
3326 }
3327 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3328 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003329 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003330 if (scmd->cmd_len == 32)
3331 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003332 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3333 if (sas_device_priv_data->sas_target->flags &
3334 MPT_TARGET_FLAGS_RAID_COMPONENT)
3335 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3336 else
3337 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3338 mpi_request->DevHandle =
3339 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3340 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3341 mpi_request->Control = cpu_to_le32(mpi_control);
3342 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3343 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3344 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3345 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303346 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003347 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3348 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3349 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303350 mpi_request->VF_ID = 0; /* TODO */
3351 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003352 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3353 mpi_request->LUN);
3354 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3355
3356 if (!mpi_request->DataLength) {
3357 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3358 } else {
3359 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3360 mpt2sas_base_free_smid(ioc, smid);
3361 goto out;
3362 }
3363 }
3364
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303365 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3366 mpt2sas_base_put_smid_scsi_io(ioc, smid,
3367 sas_device_priv_data->sas_target->handle);
3368 else
3369 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003370 return 0;
3371
3372 out:
3373 return SCSI_MLQUEUE_HOST_BUSY;
3374}
3375
3376/**
3377 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3378 * @sense_buffer: sense data returned by target
3379 * @data: normalized skey/asc/ascq
3380 *
3381 * Return nothing.
3382 */
3383static void
3384_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3385{
3386 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3387 /* descriptor format */
3388 data->skey = sense_buffer[1] & 0x0F;
3389 data->asc = sense_buffer[2];
3390 data->ascq = sense_buffer[3];
3391 } else {
3392 /* fixed format */
3393 data->skey = sense_buffer[2] & 0x0F;
3394 data->asc = sense_buffer[12];
3395 data->ascq = sense_buffer[13];
3396 }
3397}
3398
3399#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3400/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003401 * _scsih_scsi_ioc_info - translated non-successfull SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003402 * @ioc: per adapter object
3403 * @scmd: pointer to scsi command object
3404 * @mpi_reply: reply mf payload returned from firmware
3405 *
3406 * scsi_status - SCSI Status code returned from target device
3407 * scsi_state - state info associated with SCSI_IO determined by ioc
3408 * ioc_status - ioc supplied status info
3409 *
3410 * Return nothing.
3411 */
3412static void
3413_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3414 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3415{
3416 u32 response_info;
3417 u8 *response_bytes;
3418 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3419 MPI2_IOCSTATUS_MASK;
3420 u8 scsi_state = mpi_reply->SCSIState;
3421 u8 scsi_status = mpi_reply->SCSIStatus;
3422 char *desc_ioc_state = NULL;
3423 char *desc_scsi_status = NULL;
3424 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303425 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303426 struct _sas_device *sas_device = NULL;
3427 unsigned long flags;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303428
3429 if (log_info == 0x31170000)
3430 return;
Eric Moore635374e2009-03-09 01:21:12 -06003431
3432 switch (ioc_status) {
3433 case MPI2_IOCSTATUS_SUCCESS:
3434 desc_ioc_state = "success";
3435 break;
3436 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3437 desc_ioc_state = "invalid function";
3438 break;
3439 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3440 desc_ioc_state = "scsi recovered error";
3441 break;
3442 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3443 desc_ioc_state = "scsi invalid dev handle";
3444 break;
3445 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3446 desc_ioc_state = "scsi device not there";
3447 break;
3448 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3449 desc_ioc_state = "scsi data overrun";
3450 break;
3451 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3452 desc_ioc_state = "scsi data underrun";
3453 break;
3454 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3455 desc_ioc_state = "scsi io data error";
3456 break;
3457 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3458 desc_ioc_state = "scsi protocol error";
3459 break;
3460 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3461 desc_ioc_state = "scsi task terminated";
3462 break;
3463 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3464 desc_ioc_state = "scsi residual mismatch";
3465 break;
3466 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3467 desc_ioc_state = "scsi task mgmt failed";
3468 break;
3469 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3470 desc_ioc_state = "scsi ioc terminated";
3471 break;
3472 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3473 desc_ioc_state = "scsi ext terminated";
3474 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003475 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3476 desc_ioc_state = "eedp guard error";
3477 break;
3478 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3479 desc_ioc_state = "eedp ref tag error";
3480 break;
3481 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3482 desc_ioc_state = "eedp app tag error";
3483 break;
Eric Moore635374e2009-03-09 01:21:12 -06003484 default:
3485 desc_ioc_state = "unknown";
3486 break;
3487 }
3488
3489 switch (scsi_status) {
3490 case MPI2_SCSI_STATUS_GOOD:
3491 desc_scsi_status = "good";
3492 break;
3493 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3494 desc_scsi_status = "check condition";
3495 break;
3496 case MPI2_SCSI_STATUS_CONDITION_MET:
3497 desc_scsi_status = "condition met";
3498 break;
3499 case MPI2_SCSI_STATUS_BUSY:
3500 desc_scsi_status = "busy";
3501 break;
3502 case MPI2_SCSI_STATUS_INTERMEDIATE:
3503 desc_scsi_status = "intermediate";
3504 break;
3505 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3506 desc_scsi_status = "intermediate condmet";
3507 break;
3508 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3509 desc_scsi_status = "reservation conflict";
3510 break;
3511 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3512 desc_scsi_status = "command terminated";
3513 break;
3514 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3515 desc_scsi_status = "task set full";
3516 break;
3517 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3518 desc_scsi_status = "aca active";
3519 break;
3520 case MPI2_SCSI_STATUS_TASK_ABORTED:
3521 desc_scsi_status = "task aborted";
3522 break;
3523 default:
3524 desc_scsi_status = "unknown";
3525 break;
3526 }
3527
3528 desc_scsi_state[0] = '\0';
3529 if (!scsi_state)
3530 desc_scsi_state = " ";
3531 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3532 strcat(desc_scsi_state, "response info ");
3533 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3534 strcat(desc_scsi_state, "state terminated ");
3535 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3536 strcat(desc_scsi_state, "no status ");
3537 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3538 strcat(desc_scsi_state, "autosense failed ");
3539 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3540 strcat(desc_scsi_state, "autosense valid ");
3541
3542 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303543
3544 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3545 sas_device = _scsih_sas_device_find_by_handle(ioc,
3546 le16_to_cpu(mpi_reply->DevHandle));
3547 if (sas_device) {
3548 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), phy(%d)\n",
3549 ioc->name, sas_device->sas_address, sas_device->phy);
3550 printk(MPT2SAS_WARN_FMT "\tenclosure_logical_id(0x%016llx), "
3551 "slot(%d)\n", ioc->name, sas_device->enclosure_logical_id,
3552 sas_device->slot);
3553 }
3554 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3555
Eric Moore635374e2009-03-09 01:21:12 -06003556 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
3557 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
3558 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
3559 ioc_status, smid);
3560 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
3561 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
3562 scsi_get_resid(scmd));
3563 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
3564 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3565 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3566 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
3567 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
3568 scsi_status, desc_scsi_state, scsi_state);
3569
3570 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3571 struct sense_info data;
3572 _scsih_normalize_sense(scmd->sense_buffer, &data);
3573 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303574 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
3575 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06003576 }
3577
3578 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3579 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3580 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303581 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06003582 }
3583}
3584#endif
3585
3586/**
3587 * _scsih_smart_predicted_fault - illuminate Fault LED
3588 * @ioc: per adapter object
3589 * @handle: device handle
3590 *
3591 * Return nothing.
3592 */
3593static void
3594_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3595{
3596 Mpi2SepReply_t mpi_reply;
3597 Mpi2SepRequest_t mpi_request;
3598 struct scsi_target *starget;
3599 struct MPT2SAS_TARGET *sas_target_priv_data;
3600 Mpi2EventNotificationReply_t *event_reply;
3601 Mpi2EventDataSasDeviceStatusChange_t *event_data;
3602 struct _sas_device *sas_device;
3603 ssize_t sz;
3604 unsigned long flags;
3605
3606 /* only handle non-raid devices */
3607 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3608 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3609 if (!sas_device) {
3610 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3611 return;
3612 }
3613 starget = sas_device->starget;
3614 sas_target_priv_data = starget->hostdata;
3615
3616 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
3617 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
3618 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3619 return;
3620 }
3621 starget_printk(KERN_WARNING, starget, "predicted fault\n");
3622 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3623
3624 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
3625 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3626 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3627 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3628 mpi_request.SlotStatus =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303629 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
Eric Moore635374e2009-03-09 01:21:12 -06003630 mpi_request.DevHandle = cpu_to_le16(handle);
3631 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
3632 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
3633 &mpi_request)) != 0) {
3634 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3635 ioc->name, __FILE__, __LINE__, __func__);
3636 return;
3637 }
3638
3639 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
3640 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3641 "enclosure_processor: ioc_status (0x%04x), "
3642 "loginfo(0x%08x)\n", ioc->name,
3643 le16_to_cpu(mpi_reply.IOCStatus),
3644 le32_to_cpu(mpi_reply.IOCLogInfo)));
3645 return;
3646 }
3647 }
3648
3649 /* insert into event log */
3650 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
3651 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
3652 event_reply = kzalloc(sz, GFP_KERNEL);
3653 if (!event_reply) {
3654 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3655 ioc->name, __FILE__, __LINE__, __func__);
3656 return;
3657 }
3658
3659 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3660 event_reply->Event =
3661 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3662 event_reply->MsgLength = sz/4;
3663 event_reply->EventDataLength =
3664 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
3665 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
3666 event_reply->EventData;
3667 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
3668 event_data->ASC = 0x5D;
3669 event_data->DevHandle = cpu_to_le16(handle);
3670 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
3671 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
3672 kfree(event_reply);
3673}
3674
3675/**
Eric Moored5d135b2009-05-18 13:02:08 -06003676 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06003677 * @ioc: per adapter object
3678 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303679 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06003680 * @reply: reply message frame(lower 32bit addr)
3681 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303682 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06003683 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303684 * Return 1 meaning mf should be freed from _base_interrupt
3685 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06003686 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303687static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303688_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06003689{
3690 Mpi2SCSIIORequest_t *mpi_request;
3691 Mpi2SCSIIOReply_t *mpi_reply;
3692 struct scsi_cmnd *scmd;
3693 u16 ioc_status;
3694 u32 xfer_cnt;
3695 u8 scsi_state;
3696 u8 scsi_status;
3697 u32 log_info;
3698 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303699 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003700
3701 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303702 scmd = _scsih_scsi_lookup_get(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003703 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303704 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003705
3706 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3707
3708 if (mpi_reply == NULL) {
3709 scmd->result = DID_OK << 16;
3710 goto out;
3711 }
3712
3713 sas_device_priv_data = scmd->device->hostdata;
3714 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
3715 sas_device_priv_data->sas_target->deleted) {
3716 scmd->result = DID_NO_CONNECT << 16;
3717 goto out;
3718 }
3719
3720 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05303721 scsi_state = mpi_reply->SCSIState;
3722 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3723 response_code =
3724 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06003725 if (!sas_device_priv_data->tlr_snoop_check) {
3726 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai3ed21522010-02-17 16:08:36 +05303727 if (!_scsih_is_raid(&scmd->device->sdev_gendev) &&
3728 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05303729 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
3730 sas_disable_tlr(scmd->device);
3731 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
3732 }
Eric Moore635374e2009-03-09 01:21:12 -06003733 }
3734
3735 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
3736 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
3737 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
3738 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
3739 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3740 else
3741 log_info = 0;
3742 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06003743 scsi_status = mpi_reply->SCSIStatus;
3744
3745 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
3746 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
3747 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
3748 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
3749 ioc_status = MPI2_IOCSTATUS_SUCCESS;
3750 }
3751
3752 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3753 struct sense_info data;
3754 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
3755 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06003756 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06003757 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06003758 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06003759 _scsih_normalize_sense(scmd->sense_buffer, &data);
3760 /* failure prediction threshold exceeded */
3761 if (data.asc == 0x5D)
3762 _scsih_smart_predicted_fault(ioc,
3763 le16_to_cpu(mpi_reply->DevHandle));
3764 }
3765
3766 switch (ioc_status) {
3767 case MPI2_IOCSTATUS_BUSY:
3768 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
3769 scmd->result = SAM_STAT_BUSY;
3770 break;
3771
3772 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3773 scmd->result = DID_NO_CONNECT << 16;
3774 break;
3775
3776 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3777 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05303778 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
3779 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003780 }
Eric Moore635374e2009-03-09 01:21:12 -06003781 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3782 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3783 scmd->result = DID_RESET << 16;
3784 break;
3785
3786 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3787 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
3788 scmd->result = DID_SOFT_ERROR << 16;
3789 else
3790 scmd->result = (DID_OK << 16) | scsi_status;
3791 break;
3792
3793 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3794 scmd->result = (DID_OK << 16) | scsi_status;
3795
3796 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
3797 break;
3798
3799 if (xfer_cnt < scmd->underflow) {
3800 if (scsi_status == SAM_STAT_BUSY)
3801 scmd->result = SAM_STAT_BUSY;
3802 else
3803 scmd->result = DID_SOFT_ERROR << 16;
3804 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3805 MPI2_SCSI_STATE_NO_SCSI_STATUS))
3806 scmd->result = DID_SOFT_ERROR << 16;
3807 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3808 scmd->result = DID_RESET << 16;
3809 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
3810 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
3811 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
3812 scmd->result = (DRIVER_SENSE << 24) |
3813 SAM_STAT_CHECK_CONDITION;
3814 scmd->sense_buffer[0] = 0x70;
3815 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
3816 scmd->sense_buffer[12] = 0x20;
3817 scmd->sense_buffer[13] = 0;
3818 }
3819 break;
3820
3821 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3822 scsi_set_resid(scmd, 0);
3823 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3824 case MPI2_IOCSTATUS_SUCCESS:
3825 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05303826 if (response_code ==
3827 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
3828 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
3829 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06003830 scmd->result = DID_SOFT_ERROR << 16;
3831 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3832 scmd->result = DID_RESET << 16;
3833 break;
3834
Eric Moore3c621b32009-05-18 12:59:41 -06003835 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3836 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3837 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3838 _scsih_eedp_error_handling(scmd, ioc_status);
3839 break;
Eric Moore635374e2009-03-09 01:21:12 -06003840 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3841 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3842 case MPI2_IOCSTATUS_INVALID_SGL:
3843 case MPI2_IOCSTATUS_INTERNAL_ERROR:
3844 case MPI2_IOCSTATUS_INVALID_FIELD:
3845 case MPI2_IOCSTATUS_INVALID_STATE:
3846 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3847 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3848 default:
3849 scmd->result = DID_SOFT_ERROR << 16;
3850 break;
3851
3852 }
3853
3854#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3855 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
3856 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
3857#endif
3858
3859 out:
3860 scsi_dma_unmap(scmd);
3861 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303862 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06003863}
3864
3865/**
Eric Moore635374e2009-03-09 01:21:12 -06003866 * _scsih_sas_host_refresh - refreshing sas host object contents
3867 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003868 * Context: user
3869 *
3870 * During port enable, fw will send topology events for every device. Its
3871 * possible that the handles may change from the previous setting, so this
3872 * code keeping handles updating if changed.
3873 *
3874 * Return nothing.
3875 */
3876static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303877_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06003878{
3879 u16 sz;
3880 u16 ioc_status;
3881 int i;
3882 Mpi2ConfigReply_t mpi_reply;
3883 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303884 u16 attached_handle;
Eric Moore635374e2009-03-09 01:21:12 -06003885
3886 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3887 "updating handles for sas_host(0x%016llx)\n",
3888 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3889
3890 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3891 * sizeof(Mpi2SasIOUnit0PhyData_t));
3892 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3893 if (!sas_iounit_pg0) {
3894 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3895 ioc->name, __FILE__, __LINE__, __func__);
3896 return;
3897 }
Eric Moore635374e2009-03-09 01:21:12 -06003898
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05303899 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3900 sas_iounit_pg0, sz)) != 0)
3901 goto out;
3902 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3903 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3904 goto out;
3905 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3906 if (i == 0)
3907 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
3908 PhyData[0].ControllerDevHandle);
3909 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
3910 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
3911 AttachedDevHandle);
3912 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
3913 attached_handle, i, sas_iounit_pg0->PhyData[i].
3914 NegotiatedLinkRate >> 4);
3915 }
Eric Moore635374e2009-03-09 01:21:12 -06003916 out:
3917 kfree(sas_iounit_pg0);
3918}
3919
3920/**
3921 * _scsih_sas_host_add - create sas host object
3922 * @ioc: per adapter object
3923 *
3924 * Creating host side data object, stored in ioc->sas_hba
3925 *
3926 * Return nothing.
3927 */
3928static void
3929_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3930{
3931 int i;
3932 Mpi2ConfigReply_t mpi_reply;
3933 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3934 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3935 Mpi2SasPhyPage0_t phy_pg0;
3936 Mpi2SasDevicePage0_t sas_device_pg0;
3937 Mpi2SasEnclosurePage0_t enclosure_pg0;
3938 u16 ioc_status;
3939 u16 sz;
3940 u16 device_missing_delay;
3941
3942 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3943 if (!ioc->sas_hba.num_phys) {
3944 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3945 ioc->name, __FILE__, __LINE__, __func__);
3946 return;
3947 }
3948
3949 /* sas_iounit page 0 */
3950 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3951 sizeof(Mpi2SasIOUnit0PhyData_t));
3952 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3953 if (!sas_iounit_pg0) {
3954 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3955 ioc->name, __FILE__, __LINE__, __func__);
3956 return;
3957 }
3958 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3959 sas_iounit_pg0, sz))) {
3960 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3961 ioc->name, __FILE__, __LINE__, __func__);
3962 goto out;
3963 }
3964 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3965 MPI2_IOCSTATUS_MASK;
3966 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3967 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3968 ioc->name, __FILE__, __LINE__, __func__);
3969 goto out;
3970 }
3971
3972 /* sas_iounit page 1 */
3973 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3974 sizeof(Mpi2SasIOUnit1PhyData_t));
3975 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3976 if (!sas_iounit_pg1) {
3977 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3978 ioc->name, __FILE__, __LINE__, __func__);
3979 goto out;
3980 }
3981 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3982 sas_iounit_pg1, sz))) {
3983 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3984 ioc->name, __FILE__, __LINE__, __func__);
3985 goto out;
3986 }
3987 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3988 MPI2_IOCSTATUS_MASK;
3989 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3990 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3991 ioc->name, __FILE__, __LINE__, __func__);
3992 goto out;
3993 }
3994
3995 ioc->io_missing_delay =
3996 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3997 device_missing_delay =
3998 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3999 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4000 ioc->device_missing_delay = (device_missing_delay &
4001 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4002 else
4003 ioc->device_missing_delay = device_missing_delay &
4004 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4005
4006 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4007 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4008 sizeof(struct _sas_phy), GFP_KERNEL);
4009 if (!ioc->sas_hba.phy) {
4010 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4011 ioc->name, __FILE__, __LINE__, __func__);
4012 goto out;
4013 }
4014 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4015 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4016 i))) {
4017 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4018 ioc->name, __FILE__, __LINE__, __func__);
4019 goto out;
4020 }
4021 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4022 MPI2_IOCSTATUS_MASK;
4023 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4024 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4025 ioc->name, __FILE__, __LINE__, __func__);
4026 goto out;
4027 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304028
4029 if (i == 0)
4030 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4031 PhyData[0].ControllerDevHandle);
4032 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004033 ioc->sas_hba.phy[i].phy_id = i;
4034 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4035 phy_pg0, ioc->sas_hba.parent_dev);
4036 }
4037 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304038 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004039 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4040 ioc->name, __FILE__, __LINE__, __func__);
4041 goto out;
4042 }
Eric Moore635374e2009-03-09 01:21:12 -06004043 ioc->sas_hba.enclosure_handle =
4044 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4045 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4046 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4047 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4048 (unsigned long long) ioc->sas_hba.sas_address,
4049 ioc->sas_hba.num_phys) ;
4050
4051 if (ioc->sas_hba.enclosure_handle) {
4052 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4053 &enclosure_pg0,
4054 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4055 ioc->sas_hba.enclosure_handle))) {
4056 ioc->sas_hba.enclosure_logical_id =
4057 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4058 }
4059 }
4060
4061 out:
4062 kfree(sas_iounit_pg1);
4063 kfree(sas_iounit_pg0);
4064}
4065
4066/**
4067 * _scsih_expander_add - creating expander object
4068 * @ioc: per adapter object
4069 * @handle: expander handle
4070 *
4071 * Creating expander object, stored in ioc->sas_expander_list.
4072 *
4073 * Return 0 for success, else error.
4074 */
4075static int
4076_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4077{
4078 struct _sas_node *sas_expander;
4079 Mpi2ConfigReply_t mpi_reply;
4080 Mpi2ExpanderPage0_t expander_pg0;
4081 Mpi2ExpanderPage1_t expander_pg1;
4082 Mpi2SasEnclosurePage0_t enclosure_pg0;
4083 u32 ioc_status;
4084 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304085 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004086 int i;
4087 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304088 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004089 int rc = 0;
4090
4091 if (!handle)
4092 return -1;
4093
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304094 if (ioc->shost_recovery)
4095 return -1;
4096
Eric Moore635374e2009-03-09 01:21:12 -06004097 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4098 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4099 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4100 ioc->name, __FILE__, __LINE__, __func__);
4101 return -1;
4102 }
4103
4104 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4105 MPI2_IOCSTATUS_MASK;
4106 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4107 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4108 ioc->name, __FILE__, __LINE__, __func__);
4109 return -1;
4110 }
4111
4112 /* handle out of order topology events */
4113 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304114 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4115 != 0) {
4116 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4117 ioc->name, __FILE__, __LINE__, __func__);
4118 return -1;
4119 }
4120 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004121 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304122 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4123 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004124 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4125 if (!sas_expander) {
4126 rc = _scsih_expander_add(ioc, parent_handle);
4127 if (rc != 0)
4128 return rc;
4129 }
4130 }
4131
Eric Moore635374e2009-03-09 01:21:12 -06004132 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304133 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004134 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4135 sas_address);
4136 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4137
4138 if (sas_expander)
4139 return 0;
4140
4141 sas_expander = kzalloc(sizeof(struct _sas_node),
4142 GFP_KERNEL);
4143 if (!sas_expander) {
4144 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4145 ioc->name, __FILE__, __LINE__, __func__);
4146 return -1;
4147 }
4148
4149 sas_expander->handle = handle;
4150 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304151 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004152 sas_expander->sas_address = sas_address;
4153
4154 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4155 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304156 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004157 sas_expander->sas_address, sas_expander->num_phys);
4158
4159 if (!sas_expander->num_phys)
4160 goto out_fail;
4161 sas_expander->phy = kcalloc(sas_expander->num_phys,
4162 sizeof(struct _sas_phy), GFP_KERNEL);
4163 if (!sas_expander->phy) {
4164 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4165 ioc->name, __FILE__, __LINE__, __func__);
4166 rc = -1;
4167 goto out_fail;
4168 }
4169
4170 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4171 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304172 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004173 if (!mpt2sas_port) {
4174 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4175 ioc->name, __FILE__, __LINE__, __func__);
4176 rc = -1;
4177 goto out_fail;
4178 }
4179 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4180
4181 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4182 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4183 &expander_pg1, i, handle))) {
4184 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4185 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304186 rc = -1;
4187 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004188 }
4189 sas_expander->phy[i].handle = handle;
4190 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304191
4192 if ((mpt2sas_transport_add_expander_phy(ioc,
4193 &sas_expander->phy[i], expander_pg1,
4194 sas_expander->parent_dev))) {
4195 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4196 ioc->name, __FILE__, __LINE__, __func__);
4197 rc = -1;
4198 goto out_fail;
4199 }
Eric Moore635374e2009-03-09 01:21:12 -06004200 }
4201
4202 if (sas_expander->enclosure_handle) {
4203 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4204 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4205 sas_expander->enclosure_handle))) {
4206 sas_expander->enclosure_logical_id =
4207 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4208 }
4209 }
4210
4211 _scsih_expander_node_add(ioc, sas_expander);
4212 return 0;
4213
4214 out_fail:
4215
Kashyap, Desai20f58952009-08-07 19:34:26 +05304216 if (mpt2sas_port)
4217 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304218 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004219 kfree(sas_expander);
4220 return rc;
4221}
4222
4223/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304224 * _scsih_done - scsih callback handler.
4225 * @ioc: per adapter object
4226 * @smid: system request message index
4227 * @msix_index: MSIX table index supplied by the OS
4228 * @reply: reply message frame(lower 32bit addr)
4229 *
4230 * Callback handler when sending internal generated message frames.
4231 * The callback index passed is `ioc->scsih_cb_idx`
4232 *
4233 * Return 1 meaning mf should be freed from _base_interrupt
4234 * 0 means the mf is freed from this function.
4235 */
4236static u8
4237_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4238{
4239 MPI2DefaultReply_t *mpi_reply;
4240
4241 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4242 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4243 return 1;
4244 if (ioc->scsih_cmds.smid != smid)
4245 return 1;
4246 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4247 if (mpi_reply) {
4248 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4249 mpi_reply->MsgLength*4);
4250 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4251 }
4252 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4253 complete(&ioc->scsih_cmds.done);
4254 return 1;
4255}
4256
4257/**
Eric Moore635374e2009-03-09 01:21:12 -06004258 * _scsih_expander_remove - removing expander object
4259 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304260 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004261 *
4262 * Return nothing.
4263 */
4264static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304265_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004266{
4267 struct _sas_node *sas_expander;
4268 unsigned long flags;
4269
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304270 if (ioc->shost_recovery)
4271 return;
4272
Eric Moore635374e2009-03-09 01:21:12 -06004273 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304274 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4275 sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004276 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4277 _scsih_expander_node_remove(ioc, sas_expander);
4278}
4279
4280/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304281 * _scsih_check_access_status - check access flags
4282 * @ioc: per adapter object
4283 * @sas_address: sas address
4284 * @handle: sas device handle
4285 * @access_flags: errors returned during discovery of the device
4286 *
4287 * Return 0 for success, else failure
4288 */
4289static u8
4290_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4291 u16 handle, u8 access_status)
4292{
4293 u8 rc = 1;
4294 char *desc = NULL;
4295
4296 switch (access_status) {
4297 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4298 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4299 rc = 0;
4300 break;
4301 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4302 desc = "sata capability failed";
4303 break;
4304 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4305 desc = "sata affiliation conflict";
4306 break;
4307 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4308 desc = "route not addressable";
4309 break;
4310 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4311 desc = "smp error not addressable";
4312 break;
4313 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4314 desc = "device blocked";
4315 break;
4316 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4317 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4318 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4319 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4320 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4321 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4322 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4323 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4324 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4325 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4326 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4327 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4328 desc = "sata initialization failed";
4329 break;
4330 default:
4331 desc = "unknown";
4332 break;
4333 }
4334
4335 if (!rc)
4336 return 0;
4337
4338 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4339 "handle(0x%04x)\n", ioc->name, desc,
4340 (unsigned long long)sas_address, handle);
4341 return rc;
4342}
4343
4344static void
4345_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4346{
4347 Mpi2ConfigReply_t mpi_reply;
4348 Mpi2SasDevicePage0_t sas_device_pg0;
4349 struct _sas_device *sas_device;
4350 u32 ioc_status;
4351 unsigned long flags;
4352 u64 sas_address;
4353 struct scsi_target *starget;
4354 struct MPT2SAS_TARGET *sas_target_priv_data;
4355 u32 device_info;
4356
4357 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4358 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4359 return;
4360
4361 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4362 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4363 return;
4364
4365 /* check if this is end device */
4366 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4367 if (!(_scsih_is_end_device(device_info)))
4368 return;
4369
4370 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4371 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4372 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4373 sas_address);
4374
4375 if (!sas_device) {
4376 printk(MPT2SAS_ERR_FMT "device is not present "
4377 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4378 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4379 return;
4380 }
4381
4382 if (unlikely(sas_device->handle != handle)) {
4383 starget = sas_device->starget;
4384 sas_target_priv_data = starget->hostdata;
4385 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4386 " to (0x%04x)!!!\n", sas_device->handle, handle);
4387 sas_target_priv_data->handle = handle;
4388 sas_device->handle = handle;
4389 }
4390 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4391
4392 /* check if device is present */
4393 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4394 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4395 printk(MPT2SAS_ERR_FMT "device is not present "
4396 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4397 return;
4398 }
4399
4400 /* check if there were any issues with discovery */
4401 if (_scsih_check_access_status(ioc, sas_address, handle,
4402 sas_device_pg0.AccessStatus))
4403 return;
4404 _scsih_ublock_io_device(ioc, handle);
4405
4406}
4407
4408/**
Eric Moore635374e2009-03-09 01:21:12 -06004409 * _scsih_add_device - creating sas device object
4410 * @ioc: per adapter object
4411 * @handle: sas device handle
4412 * @phy_num: phy number end device attached to
4413 * @is_pd: is this hidden raid component
4414 *
4415 * Creating end device object, stored in ioc->sas_device_list.
4416 *
4417 * Returns 0 for success, non-zero for failure.
4418 */
4419static int
4420_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4421{
4422 Mpi2ConfigReply_t mpi_reply;
4423 Mpi2SasDevicePage0_t sas_device_pg0;
4424 Mpi2SasEnclosurePage0_t enclosure_pg0;
4425 struct _sas_device *sas_device;
4426 u32 ioc_status;
4427 __le64 sas_address;
4428 u32 device_info;
4429 unsigned long flags;
4430
4431 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4432 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4433 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4434 ioc->name, __FILE__, __LINE__, __func__);
4435 return -1;
4436 }
4437
4438 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4439 MPI2_IOCSTATUS_MASK;
4440 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4441 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4442 ioc->name, __FILE__, __LINE__, __func__);
4443 return -1;
4444 }
4445
Kashyap, Desaib4344272010-03-17 16:24:14 +05304446 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4447
Eric Moore635374e2009-03-09 01:21:12 -06004448 /* check if device is present */
4449 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4450 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4451 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4452 ioc->name, __FILE__, __LINE__, __func__);
4453 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4454 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4455 return -1;
4456 }
4457
Kashyap, Desaib4344272010-03-17 16:24:14 +05304458 /* check if there were any issues with discovery */
4459 if (_scsih_check_access_status(ioc, sas_address, handle,
4460 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06004461 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06004462
4463 /* check if this is end device */
4464 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4465 if (!(_scsih_is_end_device(device_info))) {
4466 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4467 ioc->name, __FILE__, __LINE__, __func__);
4468 return -1;
4469 }
4470
Eric Moore635374e2009-03-09 01:21:12 -06004471
4472 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4473 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4474 sas_address);
4475 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4476
Kashyap, Desaib4344272010-03-17 16:24:14 +05304477 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06004478 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06004479
4480 sas_device = kzalloc(sizeof(struct _sas_device),
4481 GFP_KERNEL);
4482 if (!sas_device) {
4483 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4484 ioc->name, __FILE__, __LINE__, __func__);
4485 return -1;
4486 }
4487
4488 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304489 if (_scsih_get_sas_address(ioc, le16_to_cpu
4490 (sas_device_pg0.ParentDevHandle),
4491 &sas_device->sas_address_parent) != 0)
4492 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4493 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06004494 sas_device->enclosure_handle =
4495 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4496 sas_device->slot =
4497 le16_to_cpu(sas_device_pg0.Slot);
4498 sas_device->device_info = device_info;
4499 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304500 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06004501
4502 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05304503 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
4504 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4505 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06004506 sas_device->enclosure_logical_id =
4507 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06004508
4509 /* get device name */
4510 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
4511
4512 if (ioc->wait_for_port_enable_to_complete)
4513 _scsih_sas_device_init_add(ioc, sas_device);
4514 else
4515 _scsih_sas_device_add(ioc, sas_device);
4516
4517 return 0;
4518}
4519
4520/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05304521 * _scsih_remove_device - removing sas device object
4522 * @ioc: per adapter object
4523 * @sas_device_delete: the sas_device object
4524 *
4525 * Return nothing.
4526 */
4527static void
4528_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
4529 struct _sas_device *sas_device)
4530{
4531 struct _sas_device sas_device_backup;
4532 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05304533
Kashyap, Desai1278b112010-03-09 17:34:13 +05304534 if (!sas_device)
4535 return;
Eric Moore635374e2009-03-09 01:21:12 -06004536
Kashyap, Desai1278b112010-03-09 17:34:13 +05304537 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06004538 _scsih_sas_device_remove(ioc, sas_device);
4539
Kashyap, Desai1278b112010-03-09 17:34:13 +05304540 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
4541 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4542 sas_device_backup.handle, (unsigned long long)
4543 sas_device_backup.sas_address));
4544
4545 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
4546 sas_target_priv_data = sas_device_backup.starget->hostdata;
4547 sas_target_priv_data->deleted = 1;
4548 }
4549
Kashyap, Desai1278b112010-03-09 17:34:13 +05304550 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
4551
4552 mpt2sas_transport_port_remove(ioc, sas_device_backup.sas_address,
4553 sas_device_backup.sas_address_parent);
4554
4555 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
4556 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
4557 (unsigned long long) sas_device_backup.sas_address);
4558
4559 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
4560 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
4561 sas_device_backup.handle, (unsigned long long)
4562 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06004563}
4564
4565#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4566/**
4567 * _scsih_sas_topology_change_event_debug - debug for topology event
4568 * @ioc: per adapter object
4569 * @event_data: event data payload
4570 * Context: user.
4571 */
4572static void
4573_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4574 Mpi2EventDataSasTopologyChangeList_t *event_data)
4575{
4576 int i;
4577 u16 handle;
4578 u16 reason_code;
4579 u8 phy_number;
4580 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304581 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004582
4583 switch (event_data->ExpStatus) {
4584 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
4585 status_str = "add";
4586 break;
4587 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
4588 status_str = "remove";
4589 break;
4590 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304591 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06004592 status_str = "responding";
4593 break;
4594 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
4595 status_str = "remove delay";
4596 break;
4597 default:
4598 status_str = "unknown status";
4599 break;
4600 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304601 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06004602 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304603 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06004604 "start_phy(%02d), count(%d)\n",
4605 le16_to_cpu(event_data->ExpanderDevHandle),
4606 le16_to_cpu(event_data->EnclosureHandle),
4607 event_data->StartPhyNum, event_data->NumEntries);
4608 for (i = 0; i < event_data->NumEntries; i++) {
4609 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4610 if (!handle)
4611 continue;
4612 phy_number = event_data->StartPhyNum + i;
4613 reason_code = event_data->PHY[i].PhyStatus &
4614 MPI2_EVENT_SAS_TOPO_RC_MASK;
4615 switch (reason_code) {
4616 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304617 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06004618 break;
4619 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304620 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004621 break;
4622 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304623 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06004624 break;
4625 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304626 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06004627 break;
4628 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304629 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06004630 break;
4631 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304632 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06004633 break;
4634 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304635 link_rate = event_data->PHY[i].LinkRate >> 4;
4636 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304637 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304638 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
4639 handle, status_str, link_rate, prev_link_rate);
4640
Eric Moore635374e2009-03-09 01:21:12 -06004641 }
4642}
4643#endif
4644
4645/**
4646 * _scsih_sas_topology_change_event - handle topology changes
4647 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304648 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004649 * Context: user.
4650 *
4651 */
4652static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304653_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06004654 struct fw_event_work *fw_event)
4655{
4656 int i;
4657 u16 parent_handle, handle;
4658 u16 reason_code;
4659 u8 phy_number;
4660 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304661 struct _sas_device *sas_device;
4662 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06004663 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304664 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304665 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06004666
4667#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4668 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4669 _scsih_sas_topology_change_event_debug(ioc, event_data);
4670#endif
4671
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304672 if (ioc->shost_recovery || ioc->remove_host)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304673 return;
4674
Eric Moore635374e2009-03-09 01:21:12 -06004675 if (!ioc->sas_hba.num_phys)
4676 _scsih_sas_host_add(ioc);
4677 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304678 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004679
4680 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304681 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06004682 "event\n", ioc->name));
4683 return;
4684 }
4685
4686 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
4687
4688 /* handle expander add */
4689 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
4690 if (_scsih_expander_add(ioc, parent_handle) != 0)
4691 return;
4692
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304693 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4694 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
4695 parent_handle);
4696 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4697 if (sas_expander)
4698 sas_address = sas_expander->sas_address;
4699 else if (parent_handle < ioc->sas_hba.num_phys)
4700 sas_address = ioc->sas_hba.sas_address;
4701 else
4702 return;
4703
Eric Moore635374e2009-03-09 01:21:12 -06004704 /* handle siblings events */
4705 for (i = 0; i < event_data->NumEntries; i++) {
4706 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304707 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06004708 "expander event\n", ioc->name));
4709 return;
4710 }
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304711 if (ioc->shost_recovery || ioc->remove_host)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304712 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05304713 phy_number = event_data->StartPhyNum + i;
4714 reason_code = event_data->PHY[i].PhyStatus &
4715 MPI2_EVENT_SAS_TOPO_RC_MASK;
4716 if ((event_data->PHY[i].PhyStatus &
4717 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
4718 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06004719 continue;
4720 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
4721 if (!handle)
4722 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304723 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304724 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06004725 switch (reason_code) {
4726 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304727
4728 if (link_rate == prev_link_rate)
4729 break;
4730
4731 mpt2sas_transport_update_links(ioc, sas_address,
4732 handle, phy_number, link_rate);
4733
Kashyap, Desaib4344272010-03-17 16:24:14 +05304734 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4735 break;
4736
4737 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304738 break;
Eric Moore635374e2009-03-09 01:21:12 -06004739 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304740
4741 mpt2sas_transport_update_links(ioc, sas_address,
4742 handle, phy_number, link_rate);
4743
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05304744 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06004745 break;
4746 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304747
4748 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4749 sas_device = _scsih_sas_device_find_by_handle(ioc,
4750 handle);
4751 if (!sas_device) {
4752 spin_unlock_irqrestore(&ioc->sas_device_lock,
4753 flags);
4754 break;
4755 }
4756 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4757 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06004758 break;
4759 }
4760 }
4761
4762 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304763 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
4764 sas_expander)
4765 _scsih_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06004766
4767}
4768
4769#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4770/**
4771 * _scsih_sas_device_status_change_event_debug - debug for device event
4772 * @event_data: event data payload
4773 * Context: user.
4774 *
4775 * Return nothing.
4776 */
4777static void
4778_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4779 Mpi2EventDataSasDeviceStatusChange_t *event_data)
4780{
4781 char *reason_str = NULL;
4782
4783 switch (event_data->ReasonCode) {
4784 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
4785 reason_str = "smart data";
4786 break;
4787 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
4788 reason_str = "unsupported device discovered";
4789 break;
4790 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
4791 reason_str = "internal device reset";
4792 break;
4793 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
4794 reason_str = "internal task abort";
4795 break;
4796 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
4797 reason_str = "internal task abort set";
4798 break;
4799 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
4800 reason_str = "internal clear task set";
4801 break;
4802 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
4803 reason_str = "internal query task";
4804 break;
4805 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
4806 reason_str = "sata init failure";
4807 break;
4808 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
4809 reason_str = "internal device reset complete";
4810 break;
4811 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
4812 reason_str = "internal task abort complete";
4813 break;
4814 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
4815 reason_str = "internal async notification";
4816 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05304817 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
4818 reason_str = "expander reduced functionality";
4819 break;
4820 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
4821 reason_str = "expander reduced functionality complete";
4822 break;
Eric Moore635374e2009-03-09 01:21:12 -06004823 default:
4824 reason_str = "unknown reason";
4825 break;
4826 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304827 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06004828 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
4829 reason_str, le16_to_cpu(event_data->DevHandle),
4830 (unsigned long long)le64_to_cpu(event_data->SASAddress));
4831 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304832 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004833 event_data->ASC, event_data->ASCQ);
4834 printk(KERN_INFO "\n");
4835}
4836#endif
4837
4838/**
4839 * _scsih_sas_device_status_change_event - handle device status change
4840 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304841 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004842 * Context: user.
4843 *
4844 * Return nothing.
4845 */
4846static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304847_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
4848 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004849{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304850 struct MPT2SAS_TARGET *target_priv_data;
4851 struct _sas_device *sas_device;
4852 __le64 sas_address;
4853 unsigned long flags;
4854 Mpi2EventDataSasDeviceStatusChange_t *event_data =
4855 fw_event->event_data;
4856
Eric Moore635374e2009-03-09 01:21:12 -06004857#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4858 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304859 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304860 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004861#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304862
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05304863 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304864 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05304865 event_data->ReasonCode !=
4866 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05304867 return;
4868
4869 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4870 sas_address = le64_to_cpu(event_data->SASAddress);
4871 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4872 sas_address);
4873 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4874
4875 if (!sas_device || !sas_device->starget)
4876 return;
4877
4878 target_priv_data = sas_device->starget->hostdata;
4879 if (!target_priv_data)
4880 return;
4881
4882 if (event_data->ReasonCode ==
4883 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
4884 target_priv_data->tm_busy = 1;
4885 else
4886 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004887}
4888
4889#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4890/**
4891 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
4892 * @ioc: per adapter object
4893 * @event_data: event data payload
4894 * Context: user.
4895 *
4896 * Return nothing.
4897 */
4898static void
4899_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4900 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
4901{
4902 char *reason_str = NULL;
4903
4904 switch (event_data->ReasonCode) {
4905 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
4906 reason_str = "enclosure add";
4907 break;
4908 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
4909 reason_str = "enclosure remove";
4910 break;
4911 default:
4912 reason_str = "unknown reason";
4913 break;
4914 }
4915
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304916 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06004917 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
4918 " number slots(%d)\n", ioc->name, reason_str,
4919 le16_to_cpu(event_data->EnclosureHandle),
4920 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
4921 le16_to_cpu(event_data->StartSlot));
4922}
4923#endif
4924
4925/**
4926 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
4927 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304928 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004929 * Context: user.
4930 *
4931 * Return nothing.
4932 */
4933static void
4934_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304935 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004936{
4937#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4938 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4939 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304940 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06004941#endif
4942}
4943
4944/**
4945 * _scsih_sas_broadcast_primative_event - handle broadcast events
4946 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304947 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06004948 * Context: user.
4949 *
4950 * Return nothing.
4951 */
4952static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304953_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
4954 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06004955{
4956 struct scsi_cmnd *scmd;
4957 u16 smid, handle;
4958 u32 lun;
4959 struct MPT2SAS_DEVICE *sas_device_priv_data;
4960 u32 termination_count;
4961 u32 query_count;
4962 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304963#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4964 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
4965#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05304966 u16 ioc_status;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304967 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primative: "
Eric Moore635374e2009-03-09 01:21:12 -06004968 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
4969 event_data->PortWidth));
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304970 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004971 __func__));
4972
Eric Moore635374e2009-03-09 01:21:12 -06004973 termination_count = 0;
4974 query_count = 0;
4975 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304976 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06004977 scmd = _scsih_scsi_lookup_get(ioc, smid);
4978 if (!scmd)
4979 continue;
4980 sas_device_priv_data = scmd->device->hostdata;
4981 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
4982 continue;
4983 /* skip hidden raid components */
4984 if (sas_device_priv_data->sas_target->flags &
4985 MPT_TARGET_FLAGS_RAID_COMPONENT)
4986 continue;
4987 /* skip volumes */
4988 if (sas_device_priv_data->sas_target->flags &
4989 MPT_TARGET_FLAGS_VOLUME)
4990 continue;
4991
4992 handle = sas_device_priv_data->sas_target->handle;
4993 lun = sas_device_priv_data->lun;
4994 query_count++;
4995
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05304996 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
4997 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
Eric Moore8901cbb2009-04-21 15:41:32 -06004998 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05304999 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5000 & MPI2_IOCSTATUS_MASK;
5001 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06005002 (mpi_reply->ResponseCode ==
5003 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5004 mpi_reply->ResponseCode ==
5005 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
5006 continue;
5007
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305008 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5009 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, 0, 30, NULL);
Eric Moore635374e2009-03-09 01:21:12 -06005010 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5011 }
Eric Moore635374e2009-03-09 01:21:12 -06005012 ioc->broadcast_aen_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005013
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305014 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005015 "%s - exit, query_count = %d termination_count = %d\n",
5016 ioc->name, __func__, query_count, termination_count));
5017}
5018
5019/**
5020 * _scsih_sas_discovery_event - handle discovery events
5021 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305022 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005023 * Context: user.
5024 *
5025 * Return nothing.
5026 */
5027static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305028_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5029 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005030{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305031 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5032
Eric Moore635374e2009-03-09 01:21:12 -06005033#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5034 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305035 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005036 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5037 "start" : "stop");
5038 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305039 printk("discovery_status(0x%08x)",
5040 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005041 printk("\n");
5042 }
5043#endif
5044
5045 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5046 !ioc->sas_hba.num_phys)
5047 _scsih_sas_host_add(ioc);
5048}
5049
5050/**
5051 * _scsih_reprobe_lun - reprobing lun
5052 * @sdev: scsi device struct
5053 * @no_uld_attach: sdev->no_uld_attach flag setting
5054 *
5055 **/
5056static void
5057_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5058{
5059 int rc;
5060
5061 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5062 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5063 sdev->no_uld_attach ? "hidding" : "exposing");
5064 rc = scsi_device_reprobe(sdev);
5065}
5066
5067/**
5068 * _scsih_reprobe_target - reprobing target
5069 * @starget: scsi target struct
5070 * @no_uld_attach: sdev->no_uld_attach flag setting
5071 *
5072 * Note: no_uld_attach flag determines whether the disk device is attached
5073 * to block layer. A value of `1` means to not attach.
5074 **/
5075static void
5076_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5077{
5078 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5079
5080 if (no_uld_attach)
5081 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5082 else
5083 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5084
5085 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5086 _scsih_reprobe_lun);
5087}
5088/**
5089 * _scsih_sas_volume_add - add new volume
5090 * @ioc: per adapter object
5091 * @element: IR config element data
5092 * Context: user.
5093 *
5094 * Return nothing.
5095 */
5096static void
5097_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5098 Mpi2EventIrConfigElement_t *element)
5099{
5100 struct _raid_device *raid_device;
5101 unsigned long flags;
5102 u64 wwid;
5103 u16 handle = le16_to_cpu(element->VolDevHandle);
5104 int rc;
5105
Eric Moore635374e2009-03-09 01:21:12 -06005106 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5107 if (!wwid) {
5108 printk(MPT2SAS_ERR_FMT
5109 "failure at %s:%d/%s()!\n", ioc->name,
5110 __FILE__, __LINE__, __func__);
5111 return;
5112 }
5113
5114 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5115 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5116 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5117
5118 if (raid_device)
5119 return;
5120
5121 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5122 if (!raid_device) {
5123 printk(MPT2SAS_ERR_FMT
5124 "failure at %s:%d/%s()!\n", ioc->name,
5125 __FILE__, __LINE__, __func__);
5126 return;
5127 }
5128
5129 raid_device->id = ioc->sas_id++;
5130 raid_device->channel = RAID_CHANNEL;
5131 raid_device->handle = handle;
5132 raid_device->wwid = wwid;
5133 _scsih_raid_device_add(ioc, raid_device);
5134 if (!ioc->wait_for_port_enable_to_complete) {
5135 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5136 raid_device->id, 0);
5137 if (rc)
5138 _scsih_raid_device_remove(ioc, raid_device);
5139 } else
5140 _scsih_determine_boot_device(ioc, raid_device, 1);
5141}
5142
5143/**
5144 * _scsih_sas_volume_delete - delete volume
5145 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305146 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005147 * Context: user.
5148 *
5149 * Return nothing.
5150 */
5151static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305152_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005153{
5154 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06005155 unsigned long flags;
5156 struct MPT2SAS_TARGET *sas_target_priv_data;
5157
Eric Moore635374e2009-03-09 01:21:12 -06005158 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5159 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5160 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5161 if (!raid_device)
5162 return;
5163 if (raid_device->starget) {
5164 sas_target_priv_data = raid_device->starget->hostdata;
5165 sas_target_priv_data->deleted = 1;
5166 scsi_remove_target(&raid_device->starget->dev);
5167 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305168 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5169 "(0x%016llx)\n", ioc->name, raid_device->handle,
5170 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06005171 _scsih_raid_device_remove(ioc, raid_device);
5172}
5173
5174/**
5175 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5176 * @ioc: per adapter object
5177 * @element: IR config element data
5178 * Context: user.
5179 *
5180 * Return nothing.
5181 */
5182static void
5183_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5184 Mpi2EventIrConfigElement_t *element)
5185{
5186 struct _sas_device *sas_device;
5187 unsigned long flags;
5188 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5189
5190 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5191 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5192 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5193 if (!sas_device)
5194 return;
5195
5196 /* exposing raid component */
5197 sas_device->volume_handle = 0;
5198 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305199 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005200 _scsih_reprobe_target(sas_device->starget, 0);
5201}
5202
5203/**
5204 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5205 * @ioc: per adapter object
5206 * @element: IR config element data
5207 * Context: user.
5208 *
5209 * Return nothing.
5210 */
5211static void
5212_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5213 Mpi2EventIrConfigElement_t *element)
5214{
5215 struct _sas_device *sas_device;
5216 unsigned long flags;
5217 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5218
5219 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5220 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5221 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5222 if (!sas_device)
5223 return;
5224
5225 /* hiding raid component */
5226 mpt2sas_config_get_volume_handle(ioc, handle,
5227 &sas_device->volume_handle);
5228 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5229 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305230 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005231 _scsih_reprobe_target(sas_device->starget, 1);
5232}
5233
5234/**
5235 * _scsih_sas_pd_delete - delete pd component
5236 * @ioc: per adapter object
5237 * @element: IR config element data
5238 * Context: user.
5239 *
5240 * Return nothing.
5241 */
5242static void
5243_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5244 Mpi2EventIrConfigElement_t *element)
5245{
5246 struct _sas_device *sas_device;
5247 unsigned long flags;
5248 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5249
5250 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5251 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5252 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5253 if (!sas_device)
5254 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305255 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005256}
5257
5258/**
5259 * _scsih_sas_pd_add - remove pd component
5260 * @ioc: per adapter object
5261 * @element: IR config element data
5262 * Context: user.
5263 *
5264 * Return nothing.
5265 */
5266static void
5267_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5268 Mpi2EventIrConfigElement_t *element)
5269{
5270 struct _sas_device *sas_device;
5271 unsigned long flags;
5272 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305273 Mpi2ConfigReply_t mpi_reply;
5274 Mpi2SasDevicePage0_t sas_device_pg0;
5275 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305276 u64 sas_address;
5277 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005278
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305279 set_bit(handle, ioc->pd_handles);
5280
Eric Moore635374e2009-03-09 01:21:12 -06005281 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5282 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5283 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305284 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305285 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305286
5287 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5288 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5289 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5290 ioc->name, __FILE__, __LINE__, __func__);
5291 return;
5292 }
5293
5294 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5295 MPI2_IOCSTATUS_MASK;
5296 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5297 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5298 ioc->name, __FILE__, __LINE__, __func__);
5299 return;
5300 }
5301
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305302 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5303 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5304 mpt2sas_transport_update_links(ioc, sas_address, handle,
5305 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305306
5307 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06005308}
5309
5310#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5311/**
5312 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5313 * @ioc: per adapter object
5314 * @event_data: event data payload
5315 * Context: user.
5316 *
5317 * Return nothing.
5318 */
5319static void
5320_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5321 Mpi2EventDataIrConfigChangeList_t *event_data)
5322{
5323 Mpi2EventIrConfigElement_t *element;
5324 u8 element_type;
5325 int i;
5326 char *reason_str = NULL, *element_str = NULL;
5327
5328 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5329
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305330 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005331 ioc->name, (le32_to_cpu(event_data->Flags) &
5332 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5333 "foreign" : "native", event_data->NumElements);
5334 for (i = 0; i < event_data->NumElements; i++, element++) {
5335 switch (element->ReasonCode) {
5336 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5337 reason_str = "add";
5338 break;
5339 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5340 reason_str = "remove";
5341 break;
5342 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5343 reason_str = "no change";
5344 break;
5345 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5346 reason_str = "hide";
5347 break;
5348 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5349 reason_str = "unhide";
5350 break;
5351 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5352 reason_str = "volume_created";
5353 break;
5354 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5355 reason_str = "volume_deleted";
5356 break;
5357 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5358 reason_str = "pd_created";
5359 break;
5360 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5361 reason_str = "pd_deleted";
5362 break;
5363 default:
5364 reason_str = "unknown reason";
5365 break;
5366 }
5367 element_type = le16_to_cpu(element->ElementFlags) &
5368 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5369 switch (element_type) {
5370 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5371 element_str = "volume";
5372 break;
5373 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5374 element_str = "phys disk";
5375 break;
5376 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5377 element_str = "hot spare";
5378 break;
5379 default:
5380 element_str = "unknown element";
5381 break;
5382 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305383 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005384 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5385 reason_str, le16_to_cpu(element->VolDevHandle),
5386 le16_to_cpu(element->PhysDiskDevHandle),
5387 element->PhysDiskNum);
5388 }
5389}
5390#endif
5391
5392/**
5393 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5394 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305395 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005396 * Context: user.
5397 *
5398 * Return nothing.
5399 */
5400static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305401_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5402 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005403{
5404 Mpi2EventIrConfigElement_t *element;
5405 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305406 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305407 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005408
5409#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5410 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5411 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5412
5413#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305414 foreign_config = (le32_to_cpu(event_data->Flags) &
5415 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005416
5417 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5418 for (i = 0; i < event_data->NumElements; i++, element++) {
5419
5420 switch (element->ReasonCode) {
5421 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5422 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305423 if (!foreign_config)
5424 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06005425 break;
5426 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5427 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05305428 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305429 _scsih_sas_volume_delete(ioc,
5430 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06005431 break;
5432 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5433 _scsih_sas_pd_hide(ioc, element);
5434 break;
5435 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5436 _scsih_sas_pd_expose(ioc, element);
5437 break;
5438 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5439 _scsih_sas_pd_add(ioc, element);
5440 break;
5441 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5442 _scsih_sas_pd_delete(ioc, element);
5443 break;
5444 }
5445 }
5446}
5447
5448/**
5449 * _scsih_sas_ir_volume_event - IR volume event
5450 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305451 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005452 * Context: user.
5453 *
5454 * Return nothing.
5455 */
5456static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305457_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
5458 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005459{
5460 u64 wwid;
5461 unsigned long flags;
5462 struct _raid_device *raid_device;
5463 u16 handle;
5464 u32 state;
5465 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305466 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005467
5468 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
5469 return;
5470
5471 handle = le16_to_cpu(event_data->VolDevHandle);
5472 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305473 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005474 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5475 le32_to_cpu(event_data->PreviousValue), state));
5476
Eric Moore635374e2009-03-09 01:21:12 -06005477 switch (state) {
5478 case MPI2_RAID_VOL_STATE_MISSING:
5479 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305480 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005481 break;
5482
5483 case MPI2_RAID_VOL_STATE_ONLINE:
5484 case MPI2_RAID_VOL_STATE_DEGRADED:
5485 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305486
5487 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5488 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5489 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5490
Eric Moore635374e2009-03-09 01:21:12 -06005491 if (raid_device)
5492 break;
5493
5494 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5495 if (!wwid) {
5496 printk(MPT2SAS_ERR_FMT
5497 "failure at %s:%d/%s()!\n", ioc->name,
5498 __FILE__, __LINE__, __func__);
5499 break;
5500 }
5501
5502 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5503 if (!raid_device) {
5504 printk(MPT2SAS_ERR_FMT
5505 "failure at %s:%d/%s()!\n", ioc->name,
5506 __FILE__, __LINE__, __func__);
5507 break;
5508 }
5509
5510 raid_device->id = ioc->sas_id++;
5511 raid_device->channel = RAID_CHANNEL;
5512 raid_device->handle = handle;
5513 raid_device->wwid = wwid;
5514 _scsih_raid_device_add(ioc, raid_device);
5515 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5516 raid_device->id, 0);
5517 if (rc)
5518 _scsih_raid_device_remove(ioc, raid_device);
5519 break;
5520
5521 case MPI2_RAID_VOL_STATE_INITIALIZING:
5522 default:
5523 break;
5524 }
5525}
5526
5527/**
5528 * _scsih_sas_ir_physical_disk_event - PD event
5529 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305530 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005531 * Context: user.
5532 *
5533 * Return nothing.
5534 */
5535static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305536_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
5537 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005538{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305539 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005540 u32 state;
5541 struct _sas_device *sas_device;
5542 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305543 Mpi2ConfigReply_t mpi_reply;
5544 Mpi2SasDevicePage0_t sas_device_pg0;
5545 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305546 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305547 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005548
5549 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
5550 return;
5551
5552 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
5553 state = le32_to_cpu(event_data->NewValue);
5554
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305555 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005556 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
5557 le32_to_cpu(event_data->PreviousValue), state));
5558
Eric Moore635374e2009-03-09 01:21:12 -06005559 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06005560 case MPI2_RAID_PD_STATE_ONLINE:
5561 case MPI2_RAID_PD_STATE_DEGRADED:
5562 case MPI2_RAID_PD_STATE_REBUILDING:
5563 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305564 case MPI2_RAID_PD_STATE_HOT_SPARE:
5565
5566 set_bit(handle, ioc->pd_handles);
5567
5568 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5569 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5570 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5571
5572 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305573 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305574
5575 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5576 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
5577 handle))) {
5578 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5579 ioc->name, __FILE__, __LINE__, __func__);
5580 return;
5581 }
5582
5583 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5584 MPI2_IOCSTATUS_MASK;
5585 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5586 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5587 ioc->name, __FILE__, __LINE__, __func__);
5588 return;
5589 }
5590
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305591 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5592 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5593 mpt2sas_transport_update_links(ioc, sas_address, handle,
5594 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305595
5596 _scsih_add_device(ioc, handle, 0, 1);
5597
Eric Moore635374e2009-03-09 01:21:12 -06005598 break;
5599
Kashyap, Desai62727a72009-08-07 19:35:18 +05305600 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06005601 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
5602 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06005603 default:
5604 break;
5605 }
5606}
5607
5608#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5609/**
5610 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
5611 * @ioc: per adapter object
5612 * @event_data: event data payload
5613 * Context: user.
5614 *
5615 * Return nothing.
5616 */
5617static void
5618_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
5619 Mpi2EventDataIrOperationStatus_t *event_data)
5620{
5621 char *reason_str = NULL;
5622
5623 switch (event_data->RAIDOperation) {
5624 case MPI2_EVENT_IR_RAIDOP_RESYNC:
5625 reason_str = "resync";
5626 break;
5627 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
5628 reason_str = "online capacity expansion";
5629 break;
5630 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
5631 reason_str = "consistency check";
5632 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305633 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
5634 reason_str = "background init";
5635 break;
5636 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
5637 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06005638 break;
5639 }
5640
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305641 if (!reason_str)
5642 return;
5643
Eric Moore635374e2009-03-09 01:21:12 -06005644 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
5645 "\thandle(0x%04x), percent complete(%d)\n",
5646 ioc->name, reason_str,
5647 le16_to_cpu(event_data->VolDevHandle),
5648 event_data->PercentComplete);
5649}
5650#endif
5651
5652/**
5653 * _scsih_sas_ir_operation_status_event - handle RAID operation events
5654 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305655 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005656 * Context: user.
5657 *
5658 * Return nothing.
5659 */
5660static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305661_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
5662 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005663{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305664 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
5665 static struct _raid_device *raid_device;
5666 unsigned long flags;
5667 u16 handle;
5668
Eric Moore635374e2009-03-09 01:21:12 -06005669#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5670 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305671 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305672 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005673#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05305674
5675 /* code added for raid transport support */
5676 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
5677
5678 handle = le16_to_cpu(event_data->VolDevHandle);
5679
5680 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5681 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5682 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5683
5684 if (!raid_device)
5685 return;
5686
5687 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
5688 raid_device->percent_complete =
5689 event_data->PercentComplete;
5690 }
Eric Moore635374e2009-03-09 01:21:12 -06005691}
5692
5693/**
5694 * _scsih_task_set_full - handle task set full
5695 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305696 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005697 * Context: user.
5698 *
5699 * Throttle back qdepth.
5700 */
5701static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305702_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
5703 *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005704{
5705 unsigned long flags;
5706 struct _sas_device *sas_device;
5707 static struct _raid_device *raid_device;
5708 struct scsi_device *sdev;
5709 int depth;
5710 u16 current_depth;
5711 u16 handle;
5712 int id, channel;
5713 u64 sas_address;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305714 Mpi2EventDataTaskSetFull_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005715
5716 current_depth = le16_to_cpu(event_data->CurrentDepth);
5717 handle = le16_to_cpu(event_data->DevHandle);
5718 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5719 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5720 if (!sas_device) {
5721 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5722 return;
5723 }
5724 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5725 id = sas_device->id;
5726 channel = sas_device->channel;
5727 sas_address = sas_device->sas_address;
5728
5729 /* if hidden raid component, then change to volume characteristics */
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305730 if (test_bit(handle, ioc->pd_handles) && sas_device->volume_handle) {
Eric Moore635374e2009-03-09 01:21:12 -06005731 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5732 raid_device = _scsih_raid_device_find_by_handle(
5733 ioc, sas_device->volume_handle);
5734 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5735 if (raid_device) {
5736 id = raid_device->id;
5737 channel = raid_device->channel;
5738 handle = raid_device->handle;
5739 sas_address = raid_device->wwid;
5740 }
5741 }
5742
5743 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305744 starget_printk(KERN_INFO, sas_device->starget, "task set "
Eric Moore635374e2009-03-09 01:21:12 -06005745 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
5746 handle, (unsigned long long)sas_address, current_depth);
5747
5748 shost_for_each_device(sdev, ioc->shost) {
5749 if (sdev->id == id && sdev->channel == channel) {
5750 if (current_depth > sdev->queue_depth) {
5751 if (ioc->logging_level &
5752 MPT_DEBUG_TASK_SET_FULL)
5753 sdev_printk(KERN_INFO, sdev, "strange "
5754 "observation, the queue depth is"
5755 " (%d) meanwhile fw queue depth "
5756 "is (%d)\n", sdev->queue_depth,
5757 current_depth);
5758 continue;
5759 }
5760 depth = scsi_track_queue_full(sdev,
5761 current_depth - 1);
5762 if (depth > 0)
5763 sdev_printk(KERN_INFO, sdev, "Queue depth "
5764 "reduced to (%d)\n", depth);
5765 else if (depth < 0)
5766 sdev_printk(KERN_INFO, sdev, "Tagged Command "
5767 "Queueing is being disabled\n");
5768 else if (depth == 0)
5769 if (ioc->logging_level &
5770 MPT_DEBUG_TASK_SET_FULL)
5771 sdev_printk(KERN_INFO, sdev,
5772 "Queue depth not changed yet\n");
5773 }
5774 }
5775}
5776
5777/**
Kashyap, Desai14695852010-03-30 10:52:44 +05305778 * _scsih_prep_device_scan - initialize parameters prior to device scan
5779 * @ioc: per adapter object
5780 *
5781 * Set the deleted flag prior to device scan. If the device is found during
5782 * the scan, then we clear the deleted flag.
5783 */
5784static void
5785_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
5786{
5787 struct MPT2SAS_DEVICE *sas_device_priv_data;
5788 struct scsi_device *sdev;
5789
5790 shost_for_each_device(sdev, ioc->shost) {
5791 sas_device_priv_data = sdev->hostdata;
5792 if (sas_device_priv_data && sas_device_priv_data->sas_target)
5793 sas_device_priv_data->sas_target->deleted = 1;
5794 }
5795}
5796
5797/**
Eric Moore635374e2009-03-09 01:21:12 -06005798 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
5799 * @ioc: per adapter object
5800 * @sas_address: sas address
5801 * @slot: enclosure slot id
5802 * @handle: device handle
5803 *
5804 * After host reset, find out whether devices are still responding.
5805 * Used in _scsi_remove_unresponsive_sas_devices.
5806 *
5807 * Return nothing.
5808 */
5809static void
5810_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5811 u16 slot, u16 handle)
5812{
5813 struct MPT2SAS_TARGET *sas_target_priv_data;
5814 struct scsi_target *starget;
5815 struct _sas_device *sas_device;
5816 unsigned long flags;
5817
5818 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5819 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
5820 if (sas_device->sas_address == sas_address &&
5821 sas_device->slot == slot && sas_device->starget) {
5822 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05305823 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05305824 if (starget && starget->hostdata) {
5825 sas_target_priv_data = starget->hostdata;
5826 sas_target_priv_data->tm_busy = 0;
5827 sas_target_priv_data->deleted = 0;
5828 } else
5829 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005830 starget_printk(KERN_INFO, sas_device->starget,
5831 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
5832 "logical id(0x%016llx), slot(%d)\n", handle,
5833 (unsigned long long)sas_device->sas_address,
5834 (unsigned long long)
5835 sas_device->enclosure_logical_id,
5836 sas_device->slot);
5837 if (sas_device->handle == handle)
5838 goto out;
5839 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5840 sas_device->handle);
5841 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305842 if (sas_target_priv_data)
5843 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005844 goto out;
5845 }
5846 }
5847 out:
5848 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5849}
5850
5851/**
5852 * _scsih_search_responding_sas_devices -
5853 * @ioc: per adapter object
5854 *
5855 * After host reset, find out whether devices are still responding.
5856 * If not remove.
5857 *
5858 * Return nothing.
5859 */
5860static void
5861_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
5862{
5863 Mpi2SasDevicePage0_t sas_device_pg0;
5864 Mpi2ConfigReply_t mpi_reply;
5865 u16 ioc_status;
5866 __le64 sas_address;
5867 u16 handle;
5868 u32 device_info;
5869 u16 slot;
5870
5871 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5872
5873 if (list_empty(&ioc->sas_device_list))
5874 return;
5875
5876 handle = 0xFFFF;
5877 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
5878 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
5879 handle))) {
5880 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5881 MPI2_IOCSTATUS_MASK;
5882 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5883 break;
5884 handle = le16_to_cpu(sas_device_pg0.DevHandle);
5885 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5886 if (!(_scsih_is_end_device(device_info)))
5887 continue;
5888 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5889 slot = le16_to_cpu(sas_device_pg0.Slot);
5890 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
5891 handle);
5892 }
5893}
5894
5895/**
5896 * _scsih_mark_responding_raid_device - mark a raid_device as responding
5897 * @ioc: per adapter object
5898 * @wwid: world wide identifier for raid volume
5899 * @handle: device handle
5900 *
5901 * After host reset, find out whether devices are still responding.
5902 * Used in _scsi_remove_unresponsive_raid_devices.
5903 *
5904 * Return nothing.
5905 */
5906static void
5907_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
5908 u16 handle)
5909{
5910 struct MPT2SAS_TARGET *sas_target_priv_data;
5911 struct scsi_target *starget;
5912 struct _raid_device *raid_device;
5913 unsigned long flags;
5914
5915 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5916 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
5917 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05305918 starget = raid_device->starget;
5919 if (starget && starget->hostdata) {
5920 sas_target_priv_data = starget->hostdata;
5921 sas_target_priv_data->deleted = 0;
5922 } else
5923 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06005924 raid_device->responding = 1;
5925 starget_printk(KERN_INFO, raid_device->starget,
5926 "handle(0x%04x), wwid(0x%016llx)\n", handle,
5927 (unsigned long long)raid_device->wwid);
5928 if (raid_device->handle == handle)
5929 goto out;
5930 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
5931 raid_device->handle);
5932 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05305933 if (sas_target_priv_data)
5934 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06005935 goto out;
5936 }
5937 }
5938 out:
5939 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5940}
5941
5942/**
5943 * _scsih_search_responding_raid_devices -
5944 * @ioc: per adapter object
5945 *
5946 * After host reset, find out whether devices are still responding.
5947 * If not remove.
5948 *
5949 * Return nothing.
5950 */
5951static void
5952_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
5953{
5954 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05305955 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305956 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06005957 Mpi2ConfigReply_t mpi_reply;
5958 u16 ioc_status;
5959 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305960 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06005961
5962 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
5963
5964 if (list_empty(&ioc->raid_device_list))
5965 return;
5966
5967 handle = 0xFFFF;
5968 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
5969 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
5970 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5971 MPI2_IOCSTATUS_MASK;
5972 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5973 break;
5974 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05305975
5976 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
5977 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
5978 sizeof(Mpi2RaidVolPage0_t)))
5979 continue;
5980
5981 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
5982 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
5983 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
5984 _scsih_mark_responding_raid_device(ioc,
5985 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06005986 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305987
5988 /* refresh the pd_handles */
5989 phys_disk_num = 0xFF;
5990 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
5991 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
5992 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
5993 phys_disk_num))) {
5994 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5995 MPI2_IOCSTATUS_MASK;
5996 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
5997 break;
5998 phys_disk_num = pd_pg0.PhysDiskNum;
5999 handle = le16_to_cpu(pd_pg0.DevHandle);
6000 set_bit(handle, ioc->pd_handles);
6001 }
Eric Moore635374e2009-03-09 01:21:12 -06006002}
6003
6004/**
6005 * _scsih_mark_responding_expander - mark a expander as responding
6006 * @ioc: per adapter object
6007 * @sas_address: sas address
6008 * @handle:
6009 *
6010 * After host reset, find out whether devices are still responding.
6011 * Used in _scsi_remove_unresponsive_expanders.
6012 *
6013 * Return nothing.
6014 */
6015static void
6016_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6017 u16 handle)
6018{
6019 struct _sas_node *sas_expander;
6020 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306021 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006022
6023 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6024 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306025 if (sas_expander->sas_address != sas_address)
6026 continue;
6027 sas_expander->responding = 1;
6028 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006029 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306030 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6031 " from(0x%04x) to (0x%04x)!!!\n",
6032 (unsigned long long)sas_expander->sas_address,
6033 sas_expander->handle, handle);
6034 sas_expander->handle = handle;
6035 for (i = 0 ; i < sas_expander->num_phys ; i++)
6036 sas_expander->phy[i].handle = handle;
6037 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006038 }
6039 out:
6040 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6041}
6042
6043/**
6044 * _scsih_search_responding_expanders -
6045 * @ioc: per adapter object
6046 *
6047 * After host reset, find out whether devices are still responding.
6048 * If not remove.
6049 *
6050 * Return nothing.
6051 */
6052static void
6053_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6054{
6055 Mpi2ExpanderPage0_t expander_pg0;
6056 Mpi2ConfigReply_t mpi_reply;
6057 u16 ioc_status;
6058 __le64 sas_address;
6059 u16 handle;
6060
6061 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6062
6063 if (list_empty(&ioc->sas_expander_list))
6064 return;
6065
6066 handle = 0xFFFF;
6067 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6068 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6069
6070 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6071 MPI2_IOCSTATUS_MASK;
6072 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6073 break;
6074
6075 handle = le16_to_cpu(expander_pg0.DevHandle);
6076 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6077 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6078 "sas_addr(0x%016llx)\n", handle,
6079 (unsigned long long)sas_address);
6080 _scsih_mark_responding_expander(ioc, sas_address, handle);
6081 }
6082
6083}
6084
6085/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306086 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006087 * @ioc: per adapter object
6088 *
6089 * Return nothing.
6090 */
6091static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306092_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006093{
6094 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306095 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006096 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006097
Eric Moore635374e2009-03-09 01:21:12 -06006098
6099 list_for_each_entry_safe(sas_device, sas_device_next,
6100 &ioc->sas_device_list, list) {
6101 if (sas_device->responding) {
6102 sas_device->responding = 0;
6103 continue;
6104 }
6105 if (sas_device->starget)
6106 starget_printk(KERN_INFO, sas_device->starget,
6107 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6108 "enclosure logical id(0x%016llx), slot(%d)\n",
6109 sas_device->handle,
6110 (unsigned long long)sas_device->sas_address,
6111 (unsigned long long)
6112 sas_device->enclosure_logical_id,
6113 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306114 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006115 }
6116
6117 list_for_each_entry_safe(raid_device, raid_device_next,
6118 &ioc->raid_device_list, list) {
6119 if (raid_device->responding) {
6120 raid_device->responding = 0;
6121 continue;
6122 }
6123 if (raid_device->starget) {
6124 starget_printk(KERN_INFO, raid_device->starget,
6125 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6126 raid_device->handle,
6127 (unsigned long long)raid_device->wwid);
6128 scsi_remove_target(&raid_device->starget->dev);
6129 }
6130 _scsih_raid_device_remove(ioc, raid_device);
6131 }
6132
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306133 retry_expander_search:
6134 sas_expander = NULL;
6135 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006136 if (sas_expander->responding) {
6137 sas_expander->responding = 0;
6138 continue;
6139 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306140 _scsih_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306141 goto retry_expander_search;
6142 }
6143}
6144
6145/**
6146 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6147 * @ioc: per adapter object
6148 * @reset_phase: phase
6149 *
6150 * The handler for doing any required cleanup or initialization.
6151 *
6152 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6153 * MPT2_IOC_DONE_RESET
6154 *
6155 * Return nothing.
6156 */
6157void
6158mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6159{
6160 switch (reset_phase) {
6161 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306162 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306163 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306164 break;
6165 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306166 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306167 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306168 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6169 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6170 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6171 complete(&ioc->scsih_cmds.done);
6172 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306173 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6174 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6175 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6176 complete(&ioc->tm_cmds.done);
6177 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306178 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306179 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306180 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306181 break;
6182 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306183 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306184 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306185 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05306186 _scsih_prep_device_scan(ioc);
6187 _scsih_search_responding_sas_devices(ioc);
6188 _scsih_search_responding_raid_devices(ioc);
6189 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306190 break;
Eric Moore635374e2009-03-09 01:21:12 -06006191 }
6192}
6193
6194/**
6195 * _firmware_event_work - delayed task for processing firmware events
6196 * @ioc: per adapter object
6197 * @work: equal to the fw_event_work object
6198 * Context: user.
6199 *
6200 * Return nothing.
6201 */
6202static void
6203_firmware_event_work(struct work_struct *work)
6204{
6205 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306206 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06006207 unsigned long flags;
6208 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6209
Eric Moore635374e2009-03-09 01:21:12 -06006210 /* the queue is being flushed so ignore this event */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306211 if (ioc->remove_host || fw_event->cancel_pending_work) {
Eric Moore635374e2009-03-09 01:21:12 -06006212 _scsih_fw_event_free(ioc, fw_event);
6213 return;
6214 }
Eric Moore635374e2009-03-09 01:21:12 -06006215
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306216 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6217 _scsih_fw_event_free(ioc, fw_event);
6218 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6219 if (ioc->shost_recovery) {
6220 init_completion(&ioc->shost_recovery_done);
6221 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6222 flags);
6223 wait_for_completion(&ioc->shost_recovery_done);
6224 } else
6225 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6226 flags);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306227 _scsih_remove_unresponding_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006228 return;
6229 }
Eric Moore635374e2009-03-09 01:21:12 -06006230
6231 switch (fw_event->event) {
6232 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306233 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006234 break;
6235 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306236 _scsih_sas_device_status_change_event(ioc,
6237 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006238 break;
6239 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306240 _scsih_sas_discovery_event(ioc,
6241 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006242 break;
6243 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306244 _scsih_sas_broadcast_primative_event(ioc,
6245 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006246 break;
6247 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6248 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306249 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006250 break;
6251 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306252 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006253 break;
6254 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306255 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006256 break;
6257 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306258 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006259 break;
6260 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306261 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006262 break;
6263 case MPI2_EVENT_TASK_SET_FULL:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306264 _scsih_task_set_full(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006265 break;
6266 }
6267 _scsih_fw_event_free(ioc, fw_event);
6268}
6269
6270/**
6271 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6272 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306273 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06006274 * @reply: reply message frame(lower 32bit addr)
6275 * Context: interrupt.
6276 *
6277 * This function merely adds a new work task into ioc->firmware_event_thread.
6278 * The tasks are worked from _firmware_event_work in user context.
6279 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306280 * Return 1 meaning mf should be freed from _base_interrupt
6281 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006282 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306283u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306284mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6285 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006286{
6287 struct fw_event_work *fw_event;
6288 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006289 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306290 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006291
6292 /* events turned off due to host reset or driver unloading */
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306293 if (ioc->remove_host)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306294 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006295
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306296 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006297 event = le16_to_cpu(mpi_reply->Event);
6298
6299 switch (event) {
6300 /* handle these */
6301 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6302 {
6303 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6304 (Mpi2EventDataSasBroadcastPrimitive_t *)
6305 mpi_reply->EventData;
6306
6307 if (baen_data->Primitive !=
6308 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6309 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306310 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006311 ioc->broadcast_aen_busy = 1;
6312 break;
6313 }
6314
6315 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6316 _scsih_check_topo_delete_events(ioc,
6317 (Mpi2EventDataSasTopologyChangeList_t *)
6318 mpi_reply->EventData);
6319 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306320 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6321 _scsih_check_ir_config_unhide_events(ioc,
6322 (Mpi2EventDataIrConfigChangeList_t *)
6323 mpi_reply->EventData);
6324 break;
6325 case MPI2_EVENT_IR_VOLUME:
6326 _scsih_check_volume_delete_events(ioc,
6327 (Mpi2EventDataIrVolume_t *)
6328 mpi_reply->EventData);
6329 break;
Eric Moore635374e2009-03-09 01:21:12 -06006330 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6331 case MPI2_EVENT_IR_OPERATION_STATUS:
6332 case MPI2_EVENT_SAS_DISCOVERY:
6333 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06006334 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06006335 case MPI2_EVENT_TASK_SET_FULL:
6336 break;
6337
6338 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306339 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006340 }
6341
6342 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6343 if (!fw_event) {
6344 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6345 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306346 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006347 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306348 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6349 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06006350 if (!fw_event->event_data) {
6351 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6352 ioc->name, __FILE__, __LINE__, __func__);
6353 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306354 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006355 }
6356
6357 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306358 sz);
Eric Moore635374e2009-03-09 01:21:12 -06006359 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306360 fw_event->VF_ID = mpi_reply->VF_ID;
6361 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06006362 fw_event->event = event;
6363 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306364 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006365}
6366
6367/* shost template */
6368static struct scsi_host_template scsih_driver_template = {
6369 .module = THIS_MODULE,
6370 .name = "Fusion MPT SAS Host",
6371 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06006372 .queuecommand = _scsih_qcmd,
6373 .target_alloc = _scsih_target_alloc,
6374 .slave_alloc = _scsih_slave_alloc,
6375 .slave_configure = _scsih_slave_configure,
6376 .target_destroy = _scsih_target_destroy,
6377 .slave_destroy = _scsih_slave_destroy,
6378 .change_queue_depth = _scsih_change_queue_depth,
6379 .change_queue_type = _scsih_change_queue_type,
6380 .eh_abort_handler = _scsih_abort,
6381 .eh_device_reset_handler = _scsih_dev_reset,
6382 .eh_target_reset_handler = _scsih_target_reset,
6383 .eh_host_reset_handler = _scsih_host_reset,
6384 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06006385 .can_queue = 1,
6386 .this_id = -1,
6387 .sg_tablesize = MPT2SAS_SG_DEPTH,
6388 .max_sectors = 8192,
6389 .cmd_per_lun = 7,
6390 .use_clustering = ENABLE_CLUSTERING,
6391 .shost_attrs = mpt2sas_host_attrs,
6392 .sdev_attrs = mpt2sas_dev_attrs,
6393};
6394
6395/**
6396 * _scsih_expander_node_remove - removing expander device from list.
6397 * @ioc: per adapter object
6398 * @sas_expander: the sas_device object
6399 * Context: Calling function should acquire ioc->sas_node_lock.
6400 *
6401 * Removing object and freeing associated memory from the
6402 * ioc->sas_expander_list.
6403 *
6404 * Return nothing.
6405 */
6406static void
6407_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
6408 struct _sas_node *sas_expander)
6409{
6410 struct _sas_port *mpt2sas_port;
6411 struct _sas_device *sas_device;
6412 struct _sas_node *expander_sibling;
6413 unsigned long flags;
6414
6415 if (!sas_expander)
6416 return;
6417
6418 /* remove sibling ports attached to this expander */
6419 retry_device_search:
6420 list_for_each_entry(mpt2sas_port,
6421 &sas_expander->sas_port_list, port_list) {
6422 if (mpt2sas_port->remote_identify.device_type ==
6423 SAS_END_DEVICE) {
6424 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6425 sas_device =
6426 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6427 mpt2sas_port->remote_identify.sas_address);
6428 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6429 if (!sas_device)
6430 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306431 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306432 if (ioc->shost_recovery)
6433 return;
Eric Moore635374e2009-03-09 01:21:12 -06006434 goto retry_device_search;
6435 }
6436 }
6437
6438 retry_expander_search:
6439 list_for_each_entry(mpt2sas_port,
6440 &sas_expander->sas_port_list, port_list) {
6441
6442 if (mpt2sas_port->remote_identify.device_type ==
6443 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
6444 mpt2sas_port->remote_identify.device_type ==
6445 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
6446
6447 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6448 expander_sibling =
6449 mpt2sas_scsih_expander_find_by_sas_address(
6450 ioc, mpt2sas_port->remote_identify.sas_address);
6451 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6452 if (!expander_sibling)
6453 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306454 _scsih_expander_remove(ioc,
6455 expander_sibling->sas_address);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05306456 if (ioc->shost_recovery)
6457 return;
Eric Moore635374e2009-03-09 01:21:12 -06006458 goto retry_expander_search;
6459 }
6460 }
6461
6462 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306463 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006464
6465 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
6466 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
6467 sas_expander->handle, (unsigned long long)
6468 sas_expander->sas_address);
6469
6470 list_del(&sas_expander->list);
6471 kfree(sas_expander->phy);
6472 kfree(sas_expander);
6473}
6474
6475/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05306476 * _scsih_ir_shutdown - IR shutdown notification
6477 * @ioc: per adapter object
6478 *
6479 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
6480 * the host system is shutting down.
6481 *
6482 * Return nothing.
6483 */
6484static void
6485_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
6486{
6487 Mpi2RaidActionRequest_t *mpi_request;
6488 Mpi2RaidActionReply_t *mpi_reply;
6489 u16 smid;
6490
6491 /* is IR firmware build loaded ? */
6492 if (!ioc->ir_firmware)
6493 return;
6494
6495 /* are there any volumes ? */
6496 if (list_empty(&ioc->raid_device_list))
6497 return;
6498
6499 mutex_lock(&ioc->scsih_cmds.mutex);
6500
6501 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
6502 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
6503 ioc->name, __func__);
6504 goto out;
6505 }
6506 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
6507
6508 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6509 if (!smid) {
6510 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
6511 ioc->name, __func__);
6512 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6513 goto out;
6514 }
6515
6516 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
6517 ioc->scsih_cmds.smid = smid;
6518 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6519
6520 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6521 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
6522
6523 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
6524 init_completion(&ioc->scsih_cmds.done);
6525 mpt2sas_base_put_smid_default(ioc, smid);
6526 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6527
6528 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
6529 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
6530 ioc->name, __func__);
6531 goto out;
6532 }
6533
6534 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
6535 mpi_reply = ioc->scsih_cmds.reply;
6536
6537 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
6538 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6539 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
6540 le32_to_cpu(mpi_reply->IOCLogInfo));
6541 }
6542
6543 out:
6544 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
6545 mutex_unlock(&ioc->scsih_cmds.mutex);
6546}
6547
6548/**
6549 * _scsih_shutdown - routine call during system shutdown
6550 * @pdev: PCI device struct
6551 *
6552 * Return nothing.
6553 */
6554static void
6555_scsih_shutdown(struct pci_dev *pdev)
6556{
6557 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6558 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306559 struct workqueue_struct *wq;
6560 unsigned long flags;
6561
6562 ioc->remove_host = 1;
6563 _scsih_fw_event_cleanup_queue(ioc);
6564
6565 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6566 wq = ioc->firmware_event_thread;
6567 ioc->firmware_event_thread = NULL;
6568 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6569 if (wq)
6570 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306571
6572 _scsih_ir_shutdown(ioc);
6573 mpt2sas_base_detach(ioc);
6574}
6575
6576/**
Eric Moored5d135b2009-05-18 13:02:08 -06006577 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06006578 * @pdev: PCI device struct
6579 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05306580 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06006581 * Return nothing.
6582 */
6583static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06006584_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006585{
6586 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6587 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6588 struct _sas_port *mpt2sas_port;
6589 struct _sas_device *sas_device;
6590 struct _sas_node *expander_sibling;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306591 struct _raid_device *raid_device, *next;
6592 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006593 struct workqueue_struct *wq;
6594 unsigned long flags;
6595
6596 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306597 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006598
6599 spin_lock_irqsave(&ioc->fw_event_lock, flags);
6600 wq = ioc->firmware_event_thread;
6601 ioc->firmware_event_thread = NULL;
6602 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
6603 if (wq)
6604 destroy_workqueue(wq);
6605
Kashyap, Desaid7384b22009-12-16 18:50:06 +05306606 /* release all the volumes */
6607 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
6608 list) {
6609 if (raid_device->starget) {
6610 sas_target_priv_data =
6611 raid_device->starget->hostdata;
6612 sas_target_priv_data->deleted = 1;
6613 scsi_remove_target(&raid_device->starget->dev);
6614 }
6615 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6616 "(0x%016llx)\n", ioc->name, raid_device->handle,
6617 (unsigned long long) raid_device->wwid);
6618 _scsih_raid_device_remove(ioc, raid_device);
6619 }
6620
Eric Moore635374e2009-03-09 01:21:12 -06006621 /* free ports attached to the sas_host */
6622 retry_again:
6623 list_for_each_entry(mpt2sas_port,
6624 &ioc->sas_hba.sas_port_list, port_list) {
6625 if (mpt2sas_port->remote_identify.device_type ==
6626 SAS_END_DEVICE) {
6627 sas_device =
6628 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
6629 mpt2sas_port->remote_identify.sas_address);
6630 if (sas_device) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306631 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006632 goto retry_again;
6633 }
6634 } else {
6635 expander_sibling =
6636 mpt2sas_scsih_expander_find_by_sas_address(ioc,
6637 mpt2sas_port->remote_identify.sas_address);
6638 if (expander_sibling) {
6639 _scsih_expander_remove(ioc,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306640 expander_sibling->sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06006641 goto retry_again;
6642 }
6643 }
6644 }
6645
6646 /* free phys attached to the sas_host */
6647 if (ioc->sas_hba.num_phys) {
6648 kfree(ioc->sas_hba.phy);
6649 ioc->sas_hba.phy = NULL;
6650 ioc->sas_hba.num_phys = 0;
6651 }
6652
6653 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05306654 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06006655 list_del(&ioc->list);
6656 scsi_remove_host(shost);
6657 scsi_host_put(shost);
6658}
6659
6660/**
6661 * _scsih_probe_boot_devices - reports 1st device
6662 * @ioc: per adapter object
6663 *
6664 * If specified in bios page 2, this routine reports the 1st
6665 * device scsi-ml or sas transport for persistent boot device
6666 * purposes. Please refer to function _scsih_determine_boot_device()
6667 */
6668static void
6669_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
6670{
6671 u8 is_raid;
6672 void *device;
6673 struct _sas_device *sas_device;
6674 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306675 u16 handle;
6676 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006677 u64 sas_address;
6678 unsigned long flags;
6679 int rc;
6680
6681 device = NULL;
6682 if (ioc->req_boot_device.device) {
6683 device = ioc->req_boot_device.device;
6684 is_raid = ioc->req_boot_device.is_raid;
6685 } else if (ioc->req_alt_boot_device.device) {
6686 device = ioc->req_alt_boot_device.device;
6687 is_raid = ioc->req_alt_boot_device.is_raid;
6688 } else if (ioc->current_boot_device.device) {
6689 device = ioc->current_boot_device.device;
6690 is_raid = ioc->current_boot_device.is_raid;
6691 }
6692
6693 if (!device)
6694 return;
6695
6696 if (is_raid) {
6697 raid_device = device;
6698 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6699 raid_device->id, 0);
6700 if (rc)
6701 _scsih_raid_device_remove(ioc, raid_device);
6702 } else {
6703 sas_device = device;
6704 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306705 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06006706 sas_address = sas_device->sas_address;
6707 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6708 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6709 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6710 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306711 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006712 _scsih_sas_device_remove(ioc, sas_device);
6713 } else if (!sas_device->starget) {
6714 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306715 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006716 _scsih_sas_device_remove(ioc, sas_device);
6717 }
6718 }
6719}
6720
6721/**
6722 * _scsih_probe_raid - reporting raid volumes to scsi-ml
6723 * @ioc: per adapter object
6724 *
6725 * Called during initial loading of the driver.
6726 */
6727static void
6728_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
6729{
6730 struct _raid_device *raid_device, *raid_next;
6731 int rc;
6732
6733 list_for_each_entry_safe(raid_device, raid_next,
6734 &ioc->raid_device_list, list) {
6735 if (raid_device->starget)
6736 continue;
6737 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6738 raid_device->id, 0);
6739 if (rc)
6740 _scsih_raid_device_remove(ioc, raid_device);
6741 }
6742}
6743
6744/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306745 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06006746 * @ioc: per adapter object
6747 *
6748 * Called during initial loading of the driver.
6749 */
6750static void
6751_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
6752{
6753 struct _sas_device *sas_device, *next;
6754 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06006755
6756 /* SAS Device List */
6757 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
6758 list) {
6759 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6760 list_move_tail(&sas_device->list, &ioc->sas_device_list);
6761 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6762
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306763 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6764 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06006765 _scsih_sas_device_remove(ioc, sas_device);
6766 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306767 mpt2sas_transport_port_remove(ioc,
6768 sas_device->sas_address,
6769 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06006770 _scsih_sas_device_remove(ioc, sas_device);
6771 }
6772 }
6773}
6774
6775/**
6776 * _scsih_probe_devices - probing for devices
6777 * @ioc: per adapter object
6778 *
6779 * Called during initial loading of the driver.
6780 */
6781static void
6782_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
6783{
6784 u16 volume_mapping_flags =
6785 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
6786 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
6787
6788 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
6789 return; /* return when IOC doesn't support initiator mode */
6790
6791 _scsih_probe_boot_devices(ioc);
6792
6793 if (ioc->ir_firmware) {
6794 if ((volume_mapping_flags &
6795 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
6796 _scsih_probe_sas(ioc);
6797 _scsih_probe_raid(ioc);
6798 } else {
6799 _scsih_probe_raid(ioc);
6800 _scsih_probe_sas(ioc);
6801 }
6802 } else
6803 _scsih_probe_sas(ioc);
6804}
6805
6806/**
Eric Moored5d135b2009-05-18 13:02:08 -06006807 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06006808 * @pdev: PCI device struct
6809 * @id: pci device id
6810 *
6811 * Returns 0 success, anything else error.
6812 */
6813static int
Eric Moored5d135b2009-05-18 13:02:08 -06006814_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06006815{
6816 struct MPT2SAS_ADAPTER *ioc;
6817 struct Scsi_Host *shost;
6818
6819 shost = scsi_host_alloc(&scsih_driver_template,
6820 sizeof(struct MPT2SAS_ADAPTER));
6821 if (!shost)
6822 return -ENODEV;
6823
6824 /* init local params */
6825 ioc = shost_priv(shost);
6826 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
6827 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06006828 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06006829 ioc->shost = shost;
6830 ioc->id = mpt_ids++;
6831 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
6832 ioc->pdev = pdev;
6833 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
6834 ioc->tm_cb_idx = tm_cb_idx;
6835 ioc->ctl_cb_idx = ctl_cb_idx;
6836 ioc->base_cb_idx = base_cb_idx;
6837 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05306838 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006839 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306840 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306841 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306842 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06006843 ioc->logging_level = logging_level;
6844 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05306845 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06006846 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
6847 spin_lock_init(&ioc->scsi_lookup_lock);
6848 spin_lock_init(&ioc->sas_device_lock);
6849 spin_lock_init(&ioc->sas_node_lock);
6850 spin_lock_init(&ioc->fw_event_lock);
6851 spin_lock_init(&ioc->raid_device_lock);
6852
6853 INIT_LIST_HEAD(&ioc->sas_device_list);
6854 INIT_LIST_HEAD(&ioc->sas_device_init_list);
6855 INIT_LIST_HEAD(&ioc->sas_expander_list);
6856 INIT_LIST_HEAD(&ioc->fw_event_list);
6857 INIT_LIST_HEAD(&ioc->raid_device_list);
6858 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306859 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306860 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06006861
6862 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06006863 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06006864 shost->max_lun = max_lun;
6865 shost->transportt = mpt2sas_transport_template;
6866 shost->unique_id = ioc->id;
6867
6868 if ((scsi_add_host(shost, &pdev->dev))) {
6869 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6870 ioc->name, __FILE__, __LINE__, __func__);
6871 list_del(&ioc->list);
6872 goto out_add_shost_fail;
6873 }
6874
Eric Moore3c621b32009-05-18 12:59:41 -06006875 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06006876 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306877 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06006878
Eric Moore635374e2009-03-09 01:21:12 -06006879 /* event thread */
6880 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
6881 "fw_event%d", ioc->id);
6882 ioc->firmware_event_thread = create_singlethread_workqueue(
6883 ioc->firmware_event_name);
6884 if (!ioc->firmware_event_thread) {
6885 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6886 ioc->name, __FILE__, __LINE__, __func__);
6887 goto out_thread_fail;
6888 }
6889
6890 ioc->wait_for_port_enable_to_complete = 1;
6891 if ((mpt2sas_base_attach(ioc))) {
6892 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6893 ioc->name, __FILE__, __LINE__, __func__);
6894 goto out_attach_fail;
6895 }
6896
6897 ioc->wait_for_port_enable_to_complete = 0;
6898 _scsih_probe_devices(ioc);
6899 return 0;
6900
6901 out_attach_fail:
6902 destroy_workqueue(ioc->firmware_event_thread);
6903 out_thread_fail:
6904 list_del(&ioc->list);
6905 scsi_remove_host(shost);
6906 out_add_shost_fail:
6907 return -ENODEV;
6908}
6909
6910#ifdef CONFIG_PM
6911/**
Eric Moored5d135b2009-05-18 13:02:08 -06006912 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006913 * @pdev: PCI device struct
6914 * @state: PM state change to (usually PCI_D3)
6915 *
6916 * Returns 0 success, anything else error.
6917 */
6918static int
Eric Moored5d135b2009-05-18 13:02:08 -06006919_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06006920{
6921 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6922 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6923 u32 device_state;
6924
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306925 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006926 flush_scheduled_work();
6927 scsi_block_requests(shost);
6928 device_state = pci_choose_state(pdev, state);
6929 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
6930 "operating state [D%d]\n", ioc->name, pdev,
6931 pci_name(pdev), device_state);
6932
6933 mpt2sas_base_free_resources(ioc);
6934 pci_save_state(pdev);
6935 pci_disable_device(pdev);
6936 pci_set_power_state(pdev, device_state);
6937 return 0;
6938}
6939
6940/**
Eric Moored5d135b2009-05-18 13:02:08 -06006941 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06006942 * @pdev: PCI device struct
6943 *
6944 * Returns 0 success, anything else error.
6945 */
6946static int
Eric Moored5d135b2009-05-18 13:02:08 -06006947_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06006948{
6949 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6950 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6951 u32 device_state = pdev->current_state;
6952 int r;
6953
6954 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
6955 "operating state [D%d]\n", ioc->name, pdev,
6956 pci_name(pdev), device_state);
6957
6958 pci_set_power_state(pdev, PCI_D0);
6959 pci_enable_wake(pdev, PCI_D0, 0);
6960 pci_restore_state(pdev);
6961 ioc->pdev = pdev;
6962 r = mpt2sas_base_map_resources(ioc);
6963 if (r)
6964 return r;
6965
6966 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
6967 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05306968 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006969 return 0;
6970}
6971#endif /* CONFIG_PM */
6972
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05306973/**
6974 * _scsih_pci_error_detected - Called when a PCI error is detected.
6975 * @pdev: PCI device struct
6976 * @state: PCI channel state
6977 *
6978 * Description: Called when a PCI error is detected.
6979 *
6980 * Return value:
6981 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
6982 */
6983static pci_ers_result_t
6984_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
6985{
6986 struct Scsi_Host *shost = pci_get_drvdata(pdev);
6987 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
6988
6989 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
6990 ioc->name, state);
6991
6992 switch (state) {
6993 case pci_channel_io_normal:
6994 return PCI_ERS_RESULT_CAN_RECOVER;
6995 case pci_channel_io_frozen:
6996 scsi_block_requests(ioc->shost);
6997 mpt2sas_base_stop_watchdog(ioc);
6998 mpt2sas_base_free_resources(ioc);
6999 return PCI_ERS_RESULT_NEED_RESET;
7000 case pci_channel_io_perm_failure:
7001 _scsih_remove(pdev);
7002 return PCI_ERS_RESULT_DISCONNECT;
7003 }
7004 return PCI_ERS_RESULT_NEED_RESET;
7005}
7006
7007/**
7008 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7009 * @pdev: PCI device struct
7010 *
7011 * Description: This routine is called by the pci error recovery
7012 * code after the PCI slot has been reset, just before we
7013 * should resume normal operations.
7014 */
7015static pci_ers_result_t
7016_scsih_pci_slot_reset(struct pci_dev *pdev)
7017{
7018 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7019 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7020 int rc;
7021
7022 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7023 ioc->name);
7024
7025 ioc->pdev = pdev;
7026 rc = mpt2sas_base_map_resources(ioc);
7027 if (rc)
7028 return PCI_ERS_RESULT_DISCONNECT;
7029
7030
7031 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7032 FORCE_BIG_HAMMER);
7033
7034 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7035 (rc == 0) ? "success" : "failed");
7036
7037 if (!rc)
7038 return PCI_ERS_RESULT_RECOVERED;
7039 else
7040 return PCI_ERS_RESULT_DISCONNECT;
7041}
7042
7043/**
7044 * _scsih_pci_resume() - resume normal ops after PCI reset
7045 * @pdev: pointer to PCI device
7046 *
7047 * Called when the error recovery driver tells us that its
7048 * OK to resume normal operation. Use completion to allow
7049 * halted scsi ops to resume.
7050 */
7051static void
7052_scsih_pci_resume(struct pci_dev *pdev)
7053{
7054 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7055 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7056
7057 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7058
7059 pci_cleanup_aer_uncorrect_error_status(pdev);
7060 mpt2sas_base_start_watchdog(ioc);
7061 scsi_unblock_requests(ioc->shost);
7062}
7063
7064/**
7065 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7066 * @pdev: pointer to PCI device
7067 */
7068static pci_ers_result_t
7069_scsih_pci_mmio_enabled(struct pci_dev *pdev)
7070{
7071 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7072 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7073
7074 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7075 ioc->name);
7076
7077 /* TODO - dump whatever for debugging purposes */
7078
7079 /* Request a slot reset. */
7080 return PCI_ERS_RESULT_NEED_RESET;
7081}
7082
7083static struct pci_error_handlers _scsih_err_handler = {
7084 .error_detected = _scsih_pci_error_detected,
7085 .mmio_enabled = _scsih_pci_mmio_enabled,
7086 .slot_reset = _scsih_pci_slot_reset,
7087 .resume = _scsih_pci_resume,
7088};
Eric Moore635374e2009-03-09 01:21:12 -06007089
7090static struct pci_driver scsih_driver = {
7091 .name = MPT2SAS_DRIVER_NAME,
7092 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06007093 .probe = _scsih_probe,
7094 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05307095 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307096 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06007097#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06007098 .suspend = _scsih_suspend,
7099 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06007100#endif
7101};
7102
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307103/* raid transport support */
7104static struct raid_function_template mpt2sas_raid_functions = {
7105 .cookie = &scsih_driver_template,
7106 .is_raid = _scsih_is_raid,
7107 .get_resync = _scsih_get_resync,
7108 .get_state = _scsih_get_state,
7109};
Eric Moore635374e2009-03-09 01:21:12 -06007110
7111/**
Eric Moored5d135b2009-05-18 13:02:08 -06007112 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06007113 *
7114 * Returns 0 success, anything else error.
7115 */
7116static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06007117_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06007118{
7119 int error;
7120
7121 mpt_ids = 0;
7122 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7123 MPT2SAS_DRIVER_VERSION);
7124
7125 mpt2sas_transport_template =
7126 sas_attach_transport(&mpt2sas_transport_functions);
7127 if (!mpt2sas_transport_template)
7128 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307129 /* raid transport support */
7130 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7131 if (!mpt2sas_raid_template) {
7132 sas_release_transport(mpt2sas_transport_template);
7133 return -ENODEV;
7134 }
Eric Moore635374e2009-03-09 01:21:12 -06007135
7136 mpt2sas_base_initialize_callback_handler();
7137
7138 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06007139 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06007140
7141 /* task managment callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06007142 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06007143
7144 /* base internal commands callback handler */
7145 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7146
7147 /* transport internal commands callback handler */
7148 transport_cb_idx = mpt2sas_base_register_callback_handler(
7149 mpt2sas_transport_done);
7150
Kashyap, Desai744090d2009-10-05 15:56:56 +05307151 /* scsih internal commands callback handler */
7152 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7153
Eric Moore635374e2009-03-09 01:21:12 -06007154 /* configuration page API internal commands callback handler */
7155 config_cb_idx = mpt2sas_base_register_callback_handler(
7156 mpt2sas_config_done);
7157
7158 /* ctl module callback handler */
7159 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7160
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307161 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7162 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307163
7164 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7165 _scsih_tm_volume_tr_complete);
7166
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307167 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7168 _scsih_sas_control_complete);
7169
Eric Moore635374e2009-03-09 01:21:12 -06007170 mpt2sas_ctl_init();
7171
7172 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307173 if (error) {
7174 /* raid transport support */
7175 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06007176 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307177 }
Eric Moore635374e2009-03-09 01:21:12 -06007178
7179 return error;
7180}
7181
7182/**
Eric Moored5d135b2009-05-18 13:02:08 -06007183 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06007184 *
7185 * Returns 0 success, anything else error.
7186 */
7187static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06007188_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06007189{
7190 printk(KERN_INFO "mpt2sas version %s unloading\n",
7191 MPT2SAS_DRIVER_VERSION);
7192
7193 pci_unregister_driver(&scsih_driver);
7194
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307195 mpt2sas_ctl_exit();
7196
Eric Moore635374e2009-03-09 01:21:12 -06007197 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7198 mpt2sas_base_release_callback_handler(tm_cb_idx);
7199 mpt2sas_base_release_callback_handler(base_cb_idx);
7200 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307201 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06007202 mpt2sas_base_release_callback_handler(config_cb_idx);
7203 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7204
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307205 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307206 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307207 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7208
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307209 /* raid transport support */
7210 raid_class_release(mpt2sas_raid_template);
7211 sas_release_transport(mpt2sas_transport_template);
7212
Eric Moore635374e2009-03-09 01:21:12 -06007213}
7214
Eric Moored5d135b2009-05-18 13:02:08 -06007215module_init(_scsih_init);
7216module_exit(_scsih_exit);