]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/mpt3sas/mpt3sas_scsih.c
0431cd0bd9485ff80f3b8e280597393e0bdd2450
[karo-tx-linux.git] / drivers / scsi / mpt3sas / mpt3sas_scsih.c
1 /*
2  * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3  *
4  * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
5  * Copyright (C) 2012-2014  LSI Corporation
6  * Copyright (C) 2013-2014 Avago Technologies
7  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * NO WARRANTY
20  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24  * solely responsible for determining the appropriateness of using and
25  * distributing the Program and assumes all risks associated with its
26  * exercise of rights under this Agreement, including but not limited to
27  * the risks and costs of program errors, damage to or loss of data,
28  * programs or equipment, and unavailability or interruption of operations.
29
30  * DISCLAIMER OF LIABILITY
31  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
42  * USA.
43  */
44
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>
55 #include <linux/aer.h>
56 #include <linux/raid_class.h>
57
58 #include "mpt3sas_base.h"
59
60 MODULE_AUTHOR(MPT3SAS_AUTHOR);
61 MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
62 MODULE_LICENSE("GPL");
63 MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
64
65 #define RAID_CHANNEL 1
66 /* forward proto's */
67 static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
68         struct _sas_node *sas_expander);
69 static void _firmware_event_work(struct work_struct *work);
70
71 static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
72         struct _sas_device *sas_device);
73 static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
74         u8 retry_count, u8 is_pd);
75
76 static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
77
78 static void _scsih_scan_start(struct Scsi_Host *shost);
79 static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
80
81 /* global parameters */
82 LIST_HEAD(mpt3sas_ioc_list);
83
84 /* local parameters */
85 static u8 scsi_io_cb_idx = -1;
86 static u8 tm_cb_idx = -1;
87 static u8 ctl_cb_idx = -1;
88 static u8 base_cb_idx = -1;
89 static u8 port_enable_cb_idx = -1;
90 static u8 transport_cb_idx = -1;
91 static u8 scsih_cb_idx = -1;
92 static u8 config_cb_idx = -1;
93 static int mpt_ids;
94
95 static u8 tm_tr_cb_idx = -1 ;
96 static u8 tm_tr_volume_cb_idx = -1 ;
97 static u8 tm_sas_control_cb_idx = -1;
98
99 /* command line options */
100 static u32 logging_level;
101 MODULE_PARM_DESC(logging_level,
102         " bits for enabling additional logging info (default=0)");
103
104
105 static ushort max_sectors = 0xFFFF;
106 module_param(max_sectors, ushort, 0);
107 MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767  default=32767");
108
109
110 static int missing_delay[2] = {-1, -1};
111 module_param_array(missing_delay, int, NULL, 0);
112 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
113
114 /* scsi-mid layer global parmeter is max_report_luns, which is 511 */
115 #define MPT3SAS_MAX_LUN (16895)
116 static u64 max_lun = MPT3SAS_MAX_LUN;
117 module_param(max_lun, ullong, 0);
118 MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
119
120
121
122
123 /* diag_buffer_enable is bitwise
124  * bit 0 set = TRACE
125  * bit 1 set = SNAPSHOT
126  * bit 2 set = EXTENDED
127  *
128  * Either bit can be set, or both
129  */
130 static int diag_buffer_enable = -1;
131 module_param(diag_buffer_enable, int, 0);
132 MODULE_PARM_DESC(diag_buffer_enable,
133         " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
134 static int disable_discovery = -1;
135 module_param(disable_discovery, int, 0);
136 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
137
138
139 /* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
140 static int prot_mask = -1;
141 module_param(prot_mask, int, 0);
142 MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
143
144
145 /* raid transport support */
146
147 static struct raid_template *mpt3sas_raid_template;
148
149
150 /**
151  * struct sense_info - common structure for obtaining sense keys
152  * @skey: sense key
153  * @asc: additional sense code
154  * @ascq: additional sense code qualifier
155  */
156 struct sense_info {
157         u8 skey;
158         u8 asc;
159         u8 ascq;
160 };
161
162 #define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
163 #define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
164 #define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
165 #define MPT3SAS_ABRT_TASK_SET (0xFFFE)
166 #define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
167 /**
168  * struct fw_event_work - firmware event struct
169  * @list: link list framework
170  * @work: work object (ioc->fault_reset_work_q)
171  * @cancel_pending_work: flag set during reset handling
172  * @ioc: per adapter object
173  * @device_handle: device handle
174  * @VF_ID: virtual function id
175  * @VP_ID: virtual port id
176  * @ignore: flag meaning this event has been marked to ignore
177  * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
178  * @refcount: reference count for fw_event_work
179  * @event_data: reply event data payload follows
180  *
181  * This object stored on ioc->fw_event_list.
182  */
183 struct fw_event_work {
184         struct list_head        list;
185         struct work_struct      work;
186         u8                      cancel_pending_work;
187         struct delayed_work     delayed_work;
188
189         struct MPT3SAS_ADAPTER *ioc;
190         u16                     device_handle;
191         u8                      VF_ID;
192         u8                      VP_ID;
193         u8                      ignore;
194         u16                     event;
195         struct kref             refcount;
196         char                    event_data[0] __aligned(4);
197 };
198
199 static void fw_event_work_free(struct kref *r)
200 {
201         kfree(container_of(r, struct fw_event_work, refcount));
202 }
203
204 static void fw_event_work_get(struct fw_event_work *fw_work)
205 {
206         kref_get(&fw_work->refcount);
207 }
208
209 static void fw_event_work_put(struct fw_event_work *fw_work)
210 {
211         kref_put(&fw_work->refcount, fw_event_work_free);
212 }
213
214 static struct fw_event_work *alloc_fw_event_work(int len)
215 {
216         struct fw_event_work *fw_event;
217
218         fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
219         if (!fw_event)
220                 return NULL;
221
222         kref_init(&fw_event->refcount);
223         return fw_event;
224 }
225
226 /* raid transport support */
227 static struct raid_template *mpt3sas_raid_template;
228
229 /**
230  * struct _scsi_io_transfer - scsi io transfer
231  * @handle: sas device handle (assigned by firmware)
232  * @is_raid: flag set for hidden raid components
233  * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
234  * @data_length: data transfer length
235  * @data_dma: dma pointer to data
236  * @sense: sense data
237  * @lun: lun number
238  * @cdb_length: cdb length
239  * @cdb: cdb contents
240  * @timeout: timeout for this command
241  * @VF_ID: virtual function id
242  * @VP_ID: virtual port id
243  * @valid_reply: flag set for reply message
244  * @sense_length: sense length
245  * @ioc_status: ioc status
246  * @scsi_state: scsi state
247  * @scsi_status: scsi staus
248  * @log_info: log information
249  * @transfer_length: data length transfer when there is a reply message
250  *
251  * Used for sending internal scsi commands to devices within this module.
252  * Refer to _scsi_send_scsi_io().
253  */
254 struct _scsi_io_transfer {
255         u16     handle;
256         u8      is_raid;
257         enum dma_data_direction dir;
258         u32     data_length;
259         dma_addr_t data_dma;
260         u8      sense[SCSI_SENSE_BUFFERSIZE];
261         u32     lun;
262         u8      cdb_length;
263         u8      cdb[32];
264         u8      timeout;
265         u8      VF_ID;
266         u8      VP_ID;
267         u8      valid_reply;
268   /* the following bits are only valid when 'valid_reply = 1' */
269         u32     sense_length;
270         u16     ioc_status;
271         u8      scsi_state;
272         u8      scsi_status;
273         u32     log_info;
274         u32     transfer_length;
275 };
276
277 /*
278  * The pci device ids are defined in mpi/mpi2_cnfg.h.
279  */
280 static const struct pci_device_id scsih_pci_table[] = {
281         /* Fury ~ 3004 and 3008 */
282         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
283                 PCI_ANY_ID, PCI_ANY_ID },
284         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
285                 PCI_ANY_ID, PCI_ANY_ID },
286         /* Invader ~ 3108 */
287         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
288                 PCI_ANY_ID, PCI_ANY_ID },
289         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
290                 PCI_ANY_ID, PCI_ANY_ID },
291         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
292                 PCI_ANY_ID, PCI_ANY_ID },
293         { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
294                 PCI_ANY_ID, PCI_ANY_ID },
295         {0}     /* Terminating entry */
296 };
297 MODULE_DEVICE_TABLE(pci, scsih_pci_table);
298
299 /**
300  * _scsih_set_debug_level - global setting of ioc->logging_level.
301  *
302  * Note: The logging levels are defined in mpt3sas_debug.h.
303  */
304 static int
305 _scsih_set_debug_level(const char *val, struct kernel_param *kp)
306 {
307         int ret = param_set_int(val, kp);
308         struct MPT3SAS_ADAPTER *ioc;
309
310         if (ret)
311                 return ret;
312
313         pr_info("setting logging_level(0x%08x)\n", logging_level);
314         list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
315                 ioc->logging_level = logging_level;
316         return 0;
317 }
318 module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
319         &logging_level, 0644);
320
321 /**
322  * _scsih_srch_boot_sas_address - search based on sas_address
323  * @sas_address: sas address
324  * @boot_device: boot device object from bios page 2
325  *
326  * Returns 1 when there's a match, 0 means no match.
327  */
328 static inline int
329 _scsih_srch_boot_sas_address(u64 sas_address,
330         Mpi2BootDeviceSasWwid_t *boot_device)
331 {
332         return (sas_address == le64_to_cpu(boot_device->SASAddress)) ?  1 : 0;
333 }
334
335 /**
336  * _scsih_srch_boot_device_name - search based on device name
337  * @device_name: device name specified in INDENTIFY fram
338  * @boot_device: boot device object from bios page 2
339  *
340  * Returns 1 when there's a match, 0 means no match.
341  */
342 static inline int
343 _scsih_srch_boot_device_name(u64 device_name,
344         Mpi2BootDeviceDeviceName_t *boot_device)
345 {
346         return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
347 }
348
349 /**
350  * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
351  * @enclosure_logical_id: enclosure logical id
352  * @slot_number: slot number
353  * @boot_device: boot device object from bios page 2
354  *
355  * Returns 1 when there's a match, 0 means no match.
356  */
357 static inline int
358 _scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
359         Mpi2BootDeviceEnclosureSlot_t *boot_device)
360 {
361         return (enclosure_logical_id == le64_to_cpu(boot_device->
362             EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
363             SlotNumber)) ? 1 : 0;
364 }
365
366 /**
367  * _scsih_is_boot_device - search for matching boot device.
368  * @sas_address: sas address
369  * @device_name: device name specified in INDENTIFY fram
370  * @enclosure_logical_id: enclosure logical id
371  * @slot_number: slot number
372  * @form: specifies boot device form
373  * @boot_device: boot device object from bios page 2
374  *
375  * Returns 1 when there's a match, 0 means no match.
376  */
377 static int
378 _scsih_is_boot_device(u64 sas_address, u64 device_name,
379         u64 enclosure_logical_id, u16 slot, u8 form,
380         Mpi2BiosPage2BootDevice_t *boot_device)
381 {
382         int rc = 0;
383
384         switch (form) {
385         case MPI2_BIOSPAGE2_FORM_SAS_WWID:
386                 if (!sas_address)
387                         break;
388                 rc = _scsih_srch_boot_sas_address(
389                     sas_address, &boot_device->SasWwid);
390                 break;
391         case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
392                 if (!enclosure_logical_id)
393                         break;
394                 rc = _scsih_srch_boot_encl_slot(
395                     enclosure_logical_id,
396                     slot, &boot_device->EnclosureSlot);
397                 break;
398         case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
399                 if (!device_name)
400                         break;
401                 rc = _scsih_srch_boot_device_name(
402                     device_name, &boot_device->DeviceName);
403                 break;
404         case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
405                 break;
406         }
407
408         return rc;
409 }
410
411 /**
412  * _scsih_get_sas_address - set the sas_address for given device handle
413  * @handle: device handle
414  * @sas_address: sas address
415  *
416  * Returns 0 success, non-zero when failure
417  */
418 static int
419 _scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
420         u64 *sas_address)
421 {
422         Mpi2SasDevicePage0_t sas_device_pg0;
423         Mpi2ConfigReply_t mpi_reply;
424         u32 ioc_status;
425
426         *sas_address = 0;
427
428         if (handle <= ioc->sas_hba.num_phys) {
429                 *sas_address = ioc->sas_hba.sas_address;
430                 return 0;
431         }
432
433         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
434             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
435                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
436                 __FILE__, __LINE__, __func__);
437                 return -ENXIO;
438         }
439
440         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
441         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
442                 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
443                 return 0;
444         }
445
446         /* we hit this becuase the given parent handle doesn't exist */
447         if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
448                 return -ENXIO;
449
450         /* else error case */
451         pr_err(MPT3SAS_FMT
452                 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
453                 ioc->name, handle, ioc_status,
454              __FILE__, __LINE__, __func__);
455         return -EIO;
456 }
457
458 /**
459  * _scsih_determine_boot_device - determine boot device.
460  * @ioc: per adapter object
461  * @device: either sas_device or raid_device object
462  * @is_raid: [flag] 1 = raid object, 0 = sas object
463  *
464  * Determines whether this device should be first reported device to
465  * to scsi-ml or sas transport, this purpose is for persistent boot device.
466  * There are primary, alternate, and current entries in bios page 2. The order
467  * priority is primary, alternate, then current.  This routine saves
468  * the corresponding device object and is_raid flag in the ioc object.
469  * The saved data to be used later in _scsih_probe_boot_devices().
470  */
471 static void
472 _scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
473         void *device, u8 is_raid)
474 {
475         struct _sas_device *sas_device;
476         struct _raid_device *raid_device;
477         u64 sas_address;
478         u64 device_name;
479         u64 enclosure_logical_id;
480         u16 slot;
481
482          /* only process this function when driver loads */
483         if (!ioc->is_driver_loading)
484                 return;
485
486          /* no Bios, return immediately */
487         if (!ioc->bios_pg3.BiosVersion)
488                 return;
489
490         if (!is_raid) {
491                 sas_device = device;
492                 sas_address = sas_device->sas_address;
493                 device_name = sas_device->device_name;
494                 enclosure_logical_id = sas_device->enclosure_logical_id;
495                 slot = sas_device->slot;
496         } else {
497                 raid_device = device;
498                 sas_address = raid_device->wwid;
499                 device_name = 0;
500                 enclosure_logical_id = 0;
501                 slot = 0;
502         }
503
504         if (!ioc->req_boot_device.device) {
505                 if (_scsih_is_boot_device(sas_address, device_name,
506                     enclosure_logical_id, slot,
507                     (ioc->bios_pg2.ReqBootDeviceForm &
508                     MPI2_BIOSPAGE2_FORM_MASK),
509                     &ioc->bios_pg2.RequestedBootDevice)) {
510                         dinitprintk(ioc, pr_info(MPT3SAS_FMT
511                            "%s: req_boot_device(0x%016llx)\n",
512                             ioc->name, __func__,
513                             (unsigned long long)sas_address));
514                         ioc->req_boot_device.device = device;
515                         ioc->req_boot_device.is_raid = is_raid;
516                 }
517         }
518
519         if (!ioc->req_alt_boot_device.device) {
520                 if (_scsih_is_boot_device(sas_address, device_name,
521                     enclosure_logical_id, slot,
522                     (ioc->bios_pg2.ReqAltBootDeviceForm &
523                     MPI2_BIOSPAGE2_FORM_MASK),
524                     &ioc->bios_pg2.RequestedAltBootDevice)) {
525                         dinitprintk(ioc, pr_info(MPT3SAS_FMT
526                            "%s: req_alt_boot_device(0x%016llx)\n",
527                             ioc->name, __func__,
528                             (unsigned long long)sas_address));
529                         ioc->req_alt_boot_device.device = device;
530                         ioc->req_alt_boot_device.is_raid = is_raid;
531                 }
532         }
533
534         if (!ioc->current_boot_device.device) {
535                 if (_scsih_is_boot_device(sas_address, device_name,
536                     enclosure_logical_id, slot,
537                     (ioc->bios_pg2.CurrentBootDeviceForm &
538                     MPI2_BIOSPAGE2_FORM_MASK),
539                     &ioc->bios_pg2.CurrentBootDevice)) {
540                         dinitprintk(ioc, pr_info(MPT3SAS_FMT
541                            "%s: current_boot_device(0x%016llx)\n",
542                             ioc->name, __func__,
543                             (unsigned long long)sas_address));
544                         ioc->current_boot_device.device = device;
545                         ioc->current_boot_device.is_raid = is_raid;
546                 }
547         }
548 }
549
550 static struct _sas_device *
551 __mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
552         struct MPT3SAS_TARGET *tgt_priv)
553 {
554         struct _sas_device *ret;
555
556         assert_spin_locked(&ioc->sas_device_lock);
557
558         ret = tgt_priv->sdev;
559         if (ret)
560                 sas_device_get(ret);
561
562         return ret;
563 }
564
565 static struct _sas_device *
566 mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
567         struct MPT3SAS_TARGET *tgt_priv)
568 {
569         struct _sas_device *ret;
570         unsigned long flags;
571
572         spin_lock_irqsave(&ioc->sas_device_lock, flags);
573         ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
574         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
575
576         return ret;
577 }
578
579 struct _sas_device *
580 __mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
581         u64 sas_address)
582 {
583         struct _sas_device *sas_device;
584
585         assert_spin_locked(&ioc->sas_device_lock);
586
587         list_for_each_entry(sas_device, &ioc->sas_device_list, list)
588                 if (sas_device->sas_address == sas_address)
589                         goto found_device;
590
591         list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
592                 if (sas_device->sas_address == sas_address)
593                         goto found_device;
594
595         return NULL;
596
597 found_device:
598         sas_device_get(sas_device);
599         return sas_device;
600 }
601
602
603 /**
604  * mpt3sas_get_sdev_by_addr - sas device search
605  * @ioc: per adapter object
606  * @sas_address: sas address
607  * Context: Calling function should acquire ioc->sas_device_lock
608  *
609  * This searches for sas_device based on sas_address, then return sas_device
610  * object.
611  */
612 struct _sas_device *
613 mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
614         u64 sas_address)
615 {
616         struct _sas_device *sas_device;
617         unsigned long flags;
618
619         spin_lock_irqsave(&ioc->sas_device_lock, flags);
620         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
621                         sas_address);
622         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
623
624         return sas_device;
625 }
626
627 static struct _sas_device *
628 __mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
629 {
630         struct _sas_device *sas_device;
631
632         assert_spin_locked(&ioc->sas_device_lock);
633
634         list_for_each_entry(sas_device, &ioc->sas_device_list, list)
635                 if (sas_device->handle == handle)
636                         goto found_device;
637
638         list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
639                 if (sas_device->handle == handle)
640                         goto found_device;
641
642         return NULL;
643
644 found_device:
645         sas_device_get(sas_device);
646         return sas_device;
647 }
648
649 /**
650  * mpt3sas_get_sdev_by_handle - sas device search
651  * @ioc: per adapter object
652  * @handle: sas device handle (assigned by firmware)
653  * Context: Calling function should acquire ioc->sas_device_lock
654  *
655  * This searches for sas_device based on sas_address, then return sas_device
656  * object.
657  */
658 static struct _sas_device *
659 mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
660 {
661         struct _sas_device *sas_device;
662         unsigned long flags;
663
664         spin_lock_irqsave(&ioc->sas_device_lock, flags);
665         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
666         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
667
668         return sas_device;
669 }
670
671 /**
672  * _scsih_sas_device_remove - remove sas_device from list.
673  * @ioc: per adapter object
674  * @sas_device: the sas_device object
675  * Context: This function will acquire ioc->sas_device_lock.
676  *
677  * If sas_device is on the list, remove it and decrement its reference count.
678  */
679 static void
680 _scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
681         struct _sas_device *sas_device)
682 {
683         unsigned long flags;
684
685         if (!sas_device)
686                 return;
687         pr_info(MPT3SAS_FMT
688             "removing handle(0x%04x), sas_addr(0x%016llx)\n",
689             ioc->name, sas_device->handle,
690             (unsigned long long) sas_device->sas_address);
691
692         if (sas_device->enclosure_handle != 0)
693                 pr_info(MPT3SAS_FMT
694                    "removing enclosure logical id(0x%016llx), slot(%d)\n",
695                    ioc->name, (unsigned long long)
696                    sas_device->enclosure_logical_id, sas_device->slot);
697
698         if (sas_device->connector_name[0] != '\0')
699                 pr_info(MPT3SAS_FMT
700                    "removing enclosure level(0x%04x), connector name( %s)\n",
701                    ioc->name, sas_device->enclosure_level,
702                    sas_device->connector_name);
703         /*
704          * The lock serializes access to the list, but we still need to verify
705          * that nobody removed the entry while we were waiting on the lock.
706          */
707         spin_lock_irqsave(&ioc->sas_device_lock, flags);
708         if (!list_empty(&sas_device->list)) {
709                 list_del_init(&sas_device->list);
710                 sas_device_put(sas_device);
711         }
712         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
713 }
714
715 /**
716  * _scsih_device_remove_by_handle - removing device object by handle
717  * @ioc: per adapter object
718  * @handle: device handle
719  *
720  * Return nothing.
721  */
722 static void
723 _scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
724 {
725         struct _sas_device *sas_device;
726         unsigned long flags;
727
728         if (ioc->shost_recovery)
729                 return;
730
731         spin_lock_irqsave(&ioc->sas_device_lock, flags);
732         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
733         if (sas_device) {
734                 list_del_init(&sas_device->list);
735                 sas_device_put(sas_device);
736         }
737         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
738
739         if (sas_device) {
740                 _scsih_remove_device(ioc, sas_device);
741                 sas_device_put(sas_device);
742         }
743 }
744
745 /**
746  * mpt3sas_device_remove_by_sas_address - removing device object by sas address
747  * @ioc: per adapter object
748  * @sas_address: device sas_address
749  *
750  * Return nothing.
751  */
752 void
753 mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
754         u64 sas_address)
755 {
756         struct _sas_device *sas_device;
757         unsigned long flags;
758
759         if (ioc->shost_recovery)
760                 return;
761
762         spin_lock_irqsave(&ioc->sas_device_lock, flags);
763         sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
764         if (sas_device) {
765                 list_del_init(&sas_device->list);
766                 sas_device_put(sas_device);
767         }
768         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
769
770         if (sas_device) {
771                 _scsih_remove_device(ioc, sas_device);
772                 sas_device_put(sas_device);
773         }
774 }
775
776 /**
777  * _scsih_sas_device_add - insert sas_device to the list.
778  * @ioc: per adapter object
779  * @sas_device: the sas_device object
780  * Context: This function will acquire ioc->sas_device_lock.
781  *
782  * Adding new object to the ioc->sas_device_list.
783  */
784 static void
785 _scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
786         struct _sas_device *sas_device)
787 {
788         unsigned long flags;
789
790         dewtprintk(ioc, pr_info(MPT3SAS_FMT
791                 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
792                 ioc->name, __func__, sas_device->handle,
793                 (unsigned long long)sas_device->sas_address));
794
795         if (sas_device->enclosure_handle != 0)
796                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
797                     "%s: enclosure logical id(0x%016llx), slot( %d)\n",
798                     ioc->name, __func__, (unsigned long long)
799                     sas_device->enclosure_logical_id, sas_device->slot));
800
801         if (sas_device->connector_name[0] != '\0')
802                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
803                     "%s: enclosure level(0x%04x), connector name( %s)\n",
804                     ioc->name, __func__,
805                     sas_device->enclosure_level, sas_device->connector_name));
806
807         spin_lock_irqsave(&ioc->sas_device_lock, flags);
808         sas_device_get(sas_device);
809         list_add_tail(&sas_device->list, &ioc->sas_device_list);
810         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
811
812         if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
813              sas_device->sas_address_parent)) {
814                 _scsih_sas_device_remove(ioc, sas_device);
815         } else if (!sas_device->starget) {
816                 /*
817                  * When asyn scanning is enabled, its not possible to remove
818                  * devices while scanning is turned on due to an oops in
819                  * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
820                  */
821                 if (!ioc->is_driver_loading) {
822                         mpt3sas_transport_port_remove(ioc,
823                             sas_device->sas_address,
824                             sas_device->sas_address_parent);
825                         _scsih_sas_device_remove(ioc, sas_device);
826                 }
827         }
828 }
829
830 /**
831  * _scsih_sas_device_init_add - insert sas_device to the list.
832  * @ioc: per adapter object
833  * @sas_device: the sas_device object
834  * Context: This function will acquire ioc->sas_device_lock.
835  *
836  * Adding new object at driver load time to the ioc->sas_device_init_list.
837  */
838 static void
839 _scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
840         struct _sas_device *sas_device)
841 {
842         unsigned long flags;
843
844         dewtprintk(ioc, pr_info(MPT3SAS_FMT
845                 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
846                 __func__, sas_device->handle,
847                 (unsigned long long)sas_device->sas_address));
848
849         if (sas_device->enclosure_handle != 0)
850                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
851                     "%s: enclosure logical id(0x%016llx), slot( %d)\n",
852                     ioc->name, __func__, (unsigned long long)
853                     sas_device->enclosure_logical_id, sas_device->slot));
854
855         if (sas_device->connector_name[0] != '\0')
856                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
857                     "%s: enclosure level(0x%04x), connector name( %s)\n",
858                     ioc->name, __func__, sas_device->enclosure_level,
859                     sas_device->connector_name));
860
861         spin_lock_irqsave(&ioc->sas_device_lock, flags);
862         sas_device_get(sas_device);
863         list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
864         _scsih_determine_boot_device(ioc, sas_device, 0);
865         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
866 }
867
868 /**
869  * _scsih_raid_device_find_by_id - raid device search
870  * @ioc: per adapter object
871  * @id: sas device target id
872  * @channel: sas device channel
873  * Context: Calling function should acquire ioc->raid_device_lock
874  *
875  * This searches for raid_device based on target id, then return raid_device
876  * object.
877  */
878 static struct _raid_device *
879 _scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
880 {
881         struct _raid_device *raid_device, *r;
882
883         r = NULL;
884         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
885                 if (raid_device->id == id && raid_device->channel == channel) {
886                         r = raid_device;
887                         goto out;
888                 }
889         }
890
891  out:
892         return r;
893 }
894
895 /**
896  * _scsih_raid_device_find_by_handle - raid device search
897  * @ioc: per adapter object
898  * @handle: sas device handle (assigned by firmware)
899  * Context: Calling function should acquire ioc->raid_device_lock
900  *
901  * This searches for raid_device based on handle, then return raid_device
902  * object.
903  */
904 static struct _raid_device *
905 _scsih_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
906 {
907         struct _raid_device *raid_device, *r;
908
909         r = NULL;
910         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
911                 if (raid_device->handle != handle)
912                         continue;
913                 r = raid_device;
914                 goto out;
915         }
916
917  out:
918         return r;
919 }
920
921 /**
922  * _scsih_raid_device_find_by_wwid - raid device search
923  * @ioc: per adapter object
924  * @handle: sas device handle (assigned by firmware)
925  * Context: Calling function should acquire ioc->raid_device_lock
926  *
927  * This searches for raid_device based on wwid, then return raid_device
928  * object.
929  */
930 static struct _raid_device *
931 _scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
932 {
933         struct _raid_device *raid_device, *r;
934
935         r = NULL;
936         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
937                 if (raid_device->wwid != wwid)
938                         continue;
939                 r = raid_device;
940                 goto out;
941         }
942
943  out:
944         return r;
945 }
946
947 /**
948  * _scsih_raid_device_add - add raid_device object
949  * @ioc: per adapter object
950  * @raid_device: raid_device object
951  *
952  * This is added to the raid_device_list link list.
953  */
954 static void
955 _scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
956         struct _raid_device *raid_device)
957 {
958         unsigned long flags;
959
960         dewtprintk(ioc, pr_info(MPT3SAS_FMT
961                 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
962             raid_device->handle, (unsigned long long)raid_device->wwid));
963
964         spin_lock_irqsave(&ioc->raid_device_lock, flags);
965         list_add_tail(&raid_device->list, &ioc->raid_device_list);
966         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
967 }
968
969 /**
970  * _scsih_raid_device_remove - delete raid_device object
971  * @ioc: per adapter object
972  * @raid_device: raid_device object
973  *
974  */
975 static void
976 _scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
977         struct _raid_device *raid_device)
978 {
979         unsigned long flags;
980
981         spin_lock_irqsave(&ioc->raid_device_lock, flags);
982         list_del(&raid_device->list);
983         kfree(raid_device);
984         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
985 }
986
987 /**
988  * mpt3sas_scsih_expander_find_by_handle - expander device search
989  * @ioc: per adapter object
990  * @handle: expander handle (assigned by firmware)
991  * Context: Calling function should acquire ioc->sas_device_lock
992  *
993  * This searches for expander device based on handle, then returns the
994  * sas_node object.
995  */
996 struct _sas_node *
997 mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
998 {
999         struct _sas_node *sas_expander, *r;
1000
1001         r = NULL;
1002         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1003                 if (sas_expander->handle != handle)
1004                         continue;
1005                 r = sas_expander;
1006                 goto out;
1007         }
1008  out:
1009         return r;
1010 }
1011
1012 /**
1013  * mpt3sas_scsih_expander_find_by_sas_address - expander device search
1014  * @ioc: per adapter object
1015  * @sas_address: sas address
1016  * Context: Calling function should acquire ioc->sas_node_lock.
1017  *
1018  * This searches for expander device based on sas_address, then returns the
1019  * sas_node object.
1020  */
1021 struct _sas_node *
1022 mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1023         u64 sas_address)
1024 {
1025         struct _sas_node *sas_expander, *r;
1026
1027         r = NULL;
1028         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1029                 if (sas_expander->sas_address != sas_address)
1030                         continue;
1031                 r = sas_expander;
1032                 goto out;
1033         }
1034  out:
1035         return r;
1036 }
1037
1038 /**
1039  * _scsih_expander_node_add - insert expander device to the list.
1040  * @ioc: per adapter object
1041  * @sas_expander: the sas_device object
1042  * Context: This function will acquire ioc->sas_node_lock.
1043  *
1044  * Adding new object to the ioc->sas_expander_list.
1045  *
1046  * Return nothing.
1047  */
1048 static void
1049 _scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1050         struct _sas_node *sas_expander)
1051 {
1052         unsigned long flags;
1053
1054         spin_lock_irqsave(&ioc->sas_node_lock, flags);
1055         list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1056         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1057 }
1058
1059 /**
1060  * _scsih_is_end_device - determines if device is an end device
1061  * @device_info: bitfield providing information about the device.
1062  * Context: none
1063  *
1064  * Returns 1 if end device.
1065  */
1066 static int
1067 _scsih_is_end_device(u32 device_info)
1068 {
1069         if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1070                 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1071                 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1072                 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1073                 return 1;
1074         else
1075                 return 0;
1076 }
1077
1078 /**
1079  * _scsih_scsi_lookup_get - returns scmd entry
1080  * @ioc: per adapter object
1081  * @smid: system request message index
1082  *
1083  * Returns the smid stored scmd pointer.
1084  */
1085 static struct scsi_cmnd *
1086 _scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1087 {
1088         return ioc->scsi_lookup[smid - 1].scmd;
1089 }
1090
1091 /**
1092  * _scsih_scsi_lookup_get_clear - returns scmd entry
1093  * @ioc: per adapter object
1094  * @smid: system request message index
1095  *
1096  * Returns the smid stored scmd pointer.
1097  * Then will derefrence the stored scmd pointer.
1098  */
1099 static inline struct scsi_cmnd *
1100 _scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1101 {
1102         unsigned long flags;
1103         struct scsi_cmnd *scmd;
1104
1105         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1106         scmd = ioc->scsi_lookup[smid - 1].scmd;
1107         ioc->scsi_lookup[smid - 1].scmd = NULL;
1108         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1109
1110         return scmd;
1111 }
1112
1113 /**
1114  * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1115  * @ioc: per adapter object
1116  * @smid: system request message index
1117  * @scmd: pointer to scsi command object
1118  * Context: This function will acquire ioc->scsi_lookup_lock.
1119  *
1120  * This will search for a scmd pointer in the scsi_lookup array,
1121  * returning the revelent smid.  A returned value of zero means invalid.
1122  */
1123 static u16
1124 _scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1125         *scmd)
1126 {
1127         u16 smid;
1128         unsigned long   flags;
1129         int i;
1130
1131         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1132         smid = 0;
1133         for (i = 0; i < ioc->scsiio_depth; i++) {
1134                 if (ioc->scsi_lookup[i].scmd == scmd) {
1135                         smid = ioc->scsi_lookup[i].smid;
1136                         goto out;
1137                 }
1138         }
1139  out:
1140         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1141         return smid;
1142 }
1143
1144 /**
1145  * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1146  * @ioc: per adapter object
1147  * @id: target id
1148  * @channel: channel
1149  * Context: This function will acquire ioc->scsi_lookup_lock.
1150  *
1151  * This will search for a matching channel:id in the scsi_lookup array,
1152  * returning 1 if found.
1153  */
1154 static u8
1155 _scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1156         int channel)
1157 {
1158         u8 found;
1159         unsigned long   flags;
1160         int i;
1161
1162         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1163         found = 0;
1164         for (i = 0 ; i < ioc->scsiio_depth; i++) {
1165                 if (ioc->scsi_lookup[i].scmd &&
1166                     (ioc->scsi_lookup[i].scmd->device->id == id &&
1167                     ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1168                         found = 1;
1169                         goto out;
1170                 }
1171         }
1172  out:
1173         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1174         return found;
1175 }
1176
1177 /**
1178  * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1179  * @ioc: per adapter object
1180  * @id: target id
1181  * @lun: lun number
1182  * @channel: channel
1183  * Context: This function will acquire ioc->scsi_lookup_lock.
1184  *
1185  * This will search for a matching channel:id:lun in the scsi_lookup array,
1186  * returning 1 if found.
1187  */
1188 static u8
1189 _scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1190         unsigned int lun, int channel)
1191 {
1192         u8 found;
1193         unsigned long   flags;
1194         int i;
1195
1196         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1197         found = 0;
1198         for (i = 0 ; i < ioc->scsiio_depth; i++) {
1199                 if (ioc->scsi_lookup[i].scmd &&
1200                     (ioc->scsi_lookup[i].scmd->device->id == id &&
1201                     ioc->scsi_lookup[i].scmd->device->channel == channel &&
1202                     ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1203                         found = 1;
1204                         goto out;
1205                 }
1206         }
1207  out:
1208         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1209         return found;
1210 }
1211
1212 /**
1213  * _scsih_change_queue_depth - setting device queue depth
1214  * @sdev: scsi device struct
1215  * @qdepth: requested queue depth
1216  *
1217  * Returns queue depth.
1218  */
1219 static int
1220 _scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1221 {
1222         struct Scsi_Host *shost = sdev->host;
1223         int max_depth;
1224         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1225         struct MPT3SAS_DEVICE *sas_device_priv_data;
1226         struct MPT3SAS_TARGET *sas_target_priv_data;
1227         struct _sas_device *sas_device;
1228         unsigned long flags;
1229
1230         max_depth = shost->can_queue;
1231
1232         /* limit max device queue for SATA to 32 */
1233         sas_device_priv_data = sdev->hostdata;
1234         if (!sas_device_priv_data)
1235                 goto not_sata;
1236         sas_target_priv_data = sas_device_priv_data->sas_target;
1237         if (!sas_target_priv_data)
1238                 goto not_sata;
1239         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1240                 goto not_sata;
1241
1242         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1243         sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1244         if (sas_device) {
1245                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1246                         max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1247
1248                 sas_device_put(sas_device);
1249         }
1250         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1251
1252  not_sata:
1253
1254         if (!sdev->tagged_supported)
1255                 max_depth = 1;
1256         if (qdepth > max_depth)
1257                 qdepth = max_depth;
1258         return scsi_change_queue_depth(sdev, qdepth);
1259 }
1260
1261 /**
1262  * _scsih_target_alloc - target add routine
1263  * @starget: scsi target struct
1264  *
1265  * Returns 0 if ok. Any other return is assumed to be an error and
1266  * the device is ignored.
1267  */
1268 static int
1269 _scsih_target_alloc(struct scsi_target *starget)
1270 {
1271         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1272         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1273         struct MPT3SAS_TARGET *sas_target_priv_data;
1274         struct _sas_device *sas_device;
1275         struct _raid_device *raid_device;
1276         unsigned long flags;
1277         struct sas_rphy *rphy;
1278
1279         sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1280                                        GFP_KERNEL);
1281         if (!sas_target_priv_data)
1282                 return -ENOMEM;
1283
1284         starget->hostdata = sas_target_priv_data;
1285         sas_target_priv_data->starget = starget;
1286         sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1287
1288         /* RAID volumes */
1289         if (starget->channel == RAID_CHANNEL) {
1290                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1291                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1292                     starget->channel);
1293                 if (raid_device) {
1294                         sas_target_priv_data->handle = raid_device->handle;
1295                         sas_target_priv_data->sas_address = raid_device->wwid;
1296                         sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1297                         raid_device->starget = starget;
1298                 }
1299                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1300                 return 0;
1301         }
1302
1303         /* sas/sata devices */
1304         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1305         rphy = dev_to_rphy(starget->dev.parent);
1306         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1307            rphy->identify.sas_address);
1308
1309         if (sas_device) {
1310                 sas_target_priv_data->handle = sas_device->handle;
1311                 sas_target_priv_data->sas_address = sas_device->sas_address;
1312                 sas_target_priv_data->sdev = sas_device;
1313                 sas_device->starget = starget;
1314                 sas_device->id = starget->id;
1315                 sas_device->channel = starget->channel;
1316                 if (test_bit(sas_device->handle, ioc->pd_handles))
1317                         sas_target_priv_data->flags |=
1318                             MPT_TARGET_FLAGS_RAID_COMPONENT;
1319                 if (sas_device->fast_path)
1320                         sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1321         }
1322         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1323
1324         return 0;
1325 }
1326
1327 /**
1328  * _scsih_target_destroy - target destroy routine
1329  * @starget: scsi target struct
1330  *
1331  * Returns nothing.
1332  */
1333 static void
1334 _scsih_target_destroy(struct scsi_target *starget)
1335 {
1336         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1337         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1338         struct MPT3SAS_TARGET *sas_target_priv_data;
1339         struct _sas_device *sas_device;
1340         struct _raid_device *raid_device;
1341         unsigned long flags;
1342         struct sas_rphy *rphy;
1343
1344         sas_target_priv_data = starget->hostdata;
1345         if (!sas_target_priv_data)
1346                 return;
1347
1348         if (starget->channel == RAID_CHANNEL) {
1349                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1350                 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1351                     starget->channel);
1352                 if (raid_device) {
1353                         raid_device->starget = NULL;
1354                         raid_device->sdev = NULL;
1355                 }
1356                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1357                 goto out;
1358         }
1359
1360         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1361         rphy = dev_to_rphy(starget->dev.parent);
1362         sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1363         if (sas_device && (sas_device->starget == starget) &&
1364             (sas_device->id == starget->id) &&
1365             (sas_device->channel == starget->channel))
1366                 sas_device->starget = NULL;
1367
1368         if (sas_device) {
1369                 /*
1370                  * Corresponding get() is in _scsih_target_alloc()
1371                  */
1372                 sas_target_priv_data->sdev = NULL;
1373                 sas_device_put(sas_device);
1374
1375                 sas_device_put(sas_device);
1376         }
1377         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1378
1379  out:
1380         kfree(sas_target_priv_data);
1381         starget->hostdata = NULL;
1382 }
1383
1384 /**
1385  * _scsih_slave_alloc - device add routine
1386  * @sdev: scsi device struct
1387  *
1388  * Returns 0 if ok. Any other return is assumed to be an error and
1389  * the device is ignored.
1390  */
1391 static int
1392 _scsih_slave_alloc(struct scsi_device *sdev)
1393 {
1394         struct Scsi_Host *shost;
1395         struct MPT3SAS_ADAPTER *ioc;
1396         struct MPT3SAS_TARGET *sas_target_priv_data;
1397         struct MPT3SAS_DEVICE *sas_device_priv_data;
1398         struct scsi_target *starget;
1399         struct _raid_device *raid_device;
1400         struct _sas_device *sas_device;
1401         unsigned long flags;
1402
1403         sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1404                                        GFP_KERNEL);
1405         if (!sas_device_priv_data)
1406                 return -ENOMEM;
1407
1408         sas_device_priv_data->lun = sdev->lun;
1409         sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1410
1411         starget = scsi_target(sdev);
1412         sas_target_priv_data = starget->hostdata;
1413         sas_target_priv_data->num_luns++;
1414         sas_device_priv_data->sas_target = sas_target_priv_data;
1415         sdev->hostdata = sas_device_priv_data;
1416         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1417                 sdev->no_uld_attach = 1;
1418
1419         shost = dev_to_shost(&starget->dev);
1420         ioc = shost_priv(shost);
1421         if (starget->channel == RAID_CHANNEL) {
1422                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1423                 raid_device = _scsih_raid_device_find_by_id(ioc,
1424                     starget->id, starget->channel);
1425                 if (raid_device)
1426                         raid_device->sdev = sdev; /* raid is single lun */
1427                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1428         }
1429
1430         if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1431                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1432                 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1433                                         sas_target_priv_data->sas_address);
1434                 if (sas_device && (sas_device->starget == NULL)) {
1435                         sdev_printk(KERN_INFO, sdev,
1436                         "%s : sas_device->starget set to starget @ %d\n",
1437                                 __func__, __LINE__);
1438                         sas_device->starget = starget;
1439                 }
1440
1441                 if (sas_device)
1442                         sas_device_put(sas_device);
1443
1444                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1445         }
1446
1447         return 0;
1448 }
1449
1450 /**
1451  * _scsih_slave_destroy - device destroy routine
1452  * @sdev: scsi device struct
1453  *
1454  * Returns nothing.
1455  */
1456 static void
1457 _scsih_slave_destroy(struct scsi_device *sdev)
1458 {
1459         struct MPT3SAS_TARGET *sas_target_priv_data;
1460         struct scsi_target *starget;
1461         struct Scsi_Host *shost;
1462         struct MPT3SAS_ADAPTER *ioc;
1463         struct _sas_device *sas_device;
1464         unsigned long flags;
1465
1466         if (!sdev->hostdata)
1467                 return;
1468
1469         starget = scsi_target(sdev);
1470         sas_target_priv_data = starget->hostdata;
1471         sas_target_priv_data->num_luns--;
1472
1473         shost = dev_to_shost(&starget->dev);
1474         ioc = shost_priv(shost);
1475
1476         if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1477                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1478                 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1479                                 sas_target_priv_data);
1480                 if (sas_device && !sas_target_priv_data->num_luns)
1481                         sas_device->starget = NULL;
1482
1483                 if (sas_device)
1484                         sas_device_put(sas_device);
1485
1486                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1487         }
1488
1489         kfree(sdev->hostdata);
1490         sdev->hostdata = NULL;
1491 }
1492
1493 /**
1494  * _scsih_display_sata_capabilities - sata capabilities
1495  * @ioc: per adapter object
1496  * @handle: device handle
1497  * @sdev: scsi device struct
1498  */
1499 static void
1500 _scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1501         u16 handle, struct scsi_device *sdev)
1502 {
1503         Mpi2ConfigReply_t mpi_reply;
1504         Mpi2SasDevicePage0_t sas_device_pg0;
1505         u32 ioc_status;
1506         u16 flags;
1507         u32 device_info;
1508
1509         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1510             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1511                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1512                     ioc->name, __FILE__, __LINE__, __func__);
1513                 return;
1514         }
1515
1516         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1517             MPI2_IOCSTATUS_MASK;
1518         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1519                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1520                     ioc->name, __FILE__, __LINE__, __func__);
1521                 return;
1522         }
1523
1524         flags = le16_to_cpu(sas_device_pg0.Flags);
1525         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1526
1527         sdev_printk(KERN_INFO, sdev,
1528             "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1529             "sw_preserve(%s)\n",
1530             (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1531             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1532             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1533             "n",
1534             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1535             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1536             (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1537 }
1538
1539 /*
1540  * raid transport support -
1541  * Enabled for SLES11 and newer, in older kernels the driver will panic when
1542  * unloading the driver followed by a load - I beleive that the subroutine
1543  * raid_class_release() is not cleaning up properly.
1544  */
1545
1546 /**
1547  * _scsih_is_raid - return boolean indicating device is raid volume
1548  * @dev the device struct object
1549  */
1550 static int
1551 _scsih_is_raid(struct device *dev)
1552 {
1553         struct scsi_device *sdev = to_scsi_device(dev);
1554
1555         return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1556 }
1557
1558 /**
1559  * _scsih_get_resync - get raid volume resync percent complete
1560  * @dev the device struct object
1561  */
1562 static void
1563 _scsih_get_resync(struct device *dev)
1564 {
1565         struct scsi_device *sdev = to_scsi_device(dev);
1566         struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1567         static struct _raid_device *raid_device;
1568         unsigned long flags;
1569         Mpi2RaidVolPage0_t vol_pg0;
1570         Mpi2ConfigReply_t mpi_reply;
1571         u32 volume_status_flags;
1572         u8 percent_complete;
1573         u16 handle;
1574
1575         percent_complete = 0;
1576         handle = 0;
1577         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1578         raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1579             sdev->channel);
1580         if (raid_device) {
1581                 handle = raid_device->handle;
1582                 percent_complete = raid_device->percent_complete;
1583         }
1584         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1585
1586         if (!handle)
1587                 goto out;
1588
1589         if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1590              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1591              sizeof(Mpi2RaidVolPage0_t))) {
1592                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1593                     ioc->name, __FILE__, __LINE__, __func__);
1594                 percent_complete = 0;
1595                 goto out;
1596         }
1597
1598         volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1599         if (!(volume_status_flags &
1600             MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1601                 percent_complete = 0;
1602
1603  out:
1604         raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1605 }
1606
1607 /**
1608  * _scsih_get_state - get raid volume level
1609  * @dev the device struct object
1610  */
1611 static void
1612 _scsih_get_state(struct device *dev)
1613 {
1614         struct scsi_device *sdev = to_scsi_device(dev);
1615         struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1616         static struct _raid_device *raid_device;
1617         unsigned long flags;
1618         Mpi2RaidVolPage0_t vol_pg0;
1619         Mpi2ConfigReply_t mpi_reply;
1620         u32 volstate;
1621         enum raid_state state = RAID_STATE_UNKNOWN;
1622         u16 handle = 0;
1623
1624         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1625         raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1626             sdev->channel);
1627         if (raid_device)
1628                 handle = raid_device->handle;
1629         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1630
1631         if (!raid_device)
1632                 goto out;
1633
1634         if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1635              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1636              sizeof(Mpi2RaidVolPage0_t))) {
1637                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1638                     ioc->name, __FILE__, __LINE__, __func__);
1639                 goto out;
1640         }
1641
1642         volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1643         if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1644                 state = RAID_STATE_RESYNCING;
1645                 goto out;
1646         }
1647
1648         switch (vol_pg0.VolumeState) {
1649         case MPI2_RAID_VOL_STATE_OPTIMAL:
1650         case MPI2_RAID_VOL_STATE_ONLINE:
1651                 state = RAID_STATE_ACTIVE;
1652                 break;
1653         case  MPI2_RAID_VOL_STATE_DEGRADED:
1654                 state = RAID_STATE_DEGRADED;
1655                 break;
1656         case MPI2_RAID_VOL_STATE_FAILED:
1657         case MPI2_RAID_VOL_STATE_MISSING:
1658                 state = RAID_STATE_OFFLINE;
1659                 break;
1660         }
1661  out:
1662         raid_set_state(mpt3sas_raid_template, dev, state);
1663 }
1664
1665 /**
1666  * _scsih_set_level - set raid level
1667  * @sdev: scsi device struct
1668  * @volume_type: volume type
1669  */
1670 static void
1671 _scsih_set_level(struct scsi_device *sdev, u8 volume_type)
1672 {
1673         enum raid_level level = RAID_LEVEL_UNKNOWN;
1674
1675         switch (volume_type) {
1676         case MPI2_RAID_VOL_TYPE_RAID0:
1677                 level = RAID_LEVEL_0;
1678                 break;
1679         case MPI2_RAID_VOL_TYPE_RAID10:
1680                 level = RAID_LEVEL_10;
1681                 break;
1682         case MPI2_RAID_VOL_TYPE_RAID1E:
1683                 level = RAID_LEVEL_1E;
1684                 break;
1685         case MPI2_RAID_VOL_TYPE_RAID1:
1686                 level = RAID_LEVEL_1;
1687                 break;
1688         }
1689
1690         raid_set_level(mpt3sas_raid_template, &sdev->sdev_gendev, level);
1691 }
1692
1693
1694 /**
1695  * _scsih_get_volume_capabilities - volume capabilities
1696  * @ioc: per adapter object
1697  * @sas_device: the raid_device object
1698  *
1699  * Returns 0 for success, else 1
1700  */
1701 static int
1702 _scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1703         struct _raid_device *raid_device)
1704 {
1705         Mpi2RaidVolPage0_t *vol_pg0;
1706         Mpi2RaidPhysDiskPage0_t pd_pg0;
1707         Mpi2SasDevicePage0_t sas_device_pg0;
1708         Mpi2ConfigReply_t mpi_reply;
1709         u16 sz;
1710         u8 num_pds;
1711
1712         if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1713             &num_pds)) || !num_pds) {
1714                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1715                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1716                     __func__));
1717                 return 1;
1718         }
1719
1720         raid_device->num_pds = num_pds;
1721         sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1722             sizeof(Mpi2RaidVol0PhysDisk_t));
1723         vol_pg0 = kzalloc(sz, GFP_KERNEL);
1724         if (!vol_pg0) {
1725                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1726                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1727                     __func__));
1728                 return 1;
1729         }
1730
1731         if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1732              MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1733                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1734                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1735                     __func__));
1736                 kfree(vol_pg0);
1737                 return 1;
1738         }
1739
1740         raid_device->volume_type = vol_pg0->VolumeType;
1741
1742         /* figure out what the underlying devices are by
1743          * obtaining the device_info bits for the 1st device
1744          */
1745         if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1746             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1747             vol_pg0->PhysDisk[0].PhysDiskNum))) {
1748                 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1749                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1750                     le16_to_cpu(pd_pg0.DevHandle)))) {
1751                         raid_device->device_info =
1752                             le32_to_cpu(sas_device_pg0.DeviceInfo);
1753                 }
1754         }
1755
1756         kfree(vol_pg0);
1757         return 0;
1758 }
1759
1760
1761
1762 /**
1763  * _scsih_enable_tlr - setting TLR flags
1764  * @ioc: per adapter object
1765  * @sdev: scsi device struct
1766  *
1767  * Enabling Transaction Layer Retries for tape devices when
1768  * vpd page 0x90 is present
1769  *
1770  */
1771 static void
1772 _scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1773 {
1774
1775         /* only for TAPE */
1776         if (sdev->type != TYPE_TAPE)
1777                 return;
1778
1779         if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1780                 return;
1781
1782         sas_enable_tlr(sdev);
1783         sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1784             sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1785         return;
1786
1787 }
1788
1789 /**
1790  * _scsih_slave_configure - device configure routine.
1791  * @sdev: scsi device struct
1792  *
1793  * Returns 0 if ok. Any other return is assumed to be an error and
1794  * the device is ignored.
1795  */
1796 static int
1797 _scsih_slave_configure(struct scsi_device *sdev)
1798 {
1799         struct Scsi_Host *shost = sdev->host;
1800         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1801         struct MPT3SAS_DEVICE *sas_device_priv_data;
1802         struct MPT3SAS_TARGET *sas_target_priv_data;
1803         struct _sas_device *sas_device;
1804         struct _raid_device *raid_device;
1805         unsigned long flags;
1806         int qdepth;
1807         u8 ssp_target = 0;
1808         char *ds = "";
1809         char *r_level = "";
1810         u16 handle, volume_handle = 0;
1811         u64 volume_wwid = 0;
1812
1813         qdepth = 1;
1814         sas_device_priv_data = sdev->hostdata;
1815         sas_device_priv_data->configured_lun = 1;
1816         sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1817         sas_target_priv_data = sas_device_priv_data->sas_target;
1818         handle = sas_target_priv_data->handle;
1819
1820         /* raid volume handling */
1821         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1822
1823                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1824                 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1825                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1826                 if (!raid_device) {
1827                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1828                             "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1829                             __LINE__, __func__));
1830                         return 1;
1831                 }
1832
1833                 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1834                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1835                             "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1836                             __LINE__, __func__));
1837                         return 1;
1838                 }
1839
1840
1841                 /* RAID Queue Depth Support
1842                  * IS volume = underlying qdepth of drive type, either
1843                  *    MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1844                  * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1845                  */
1846                 if (raid_device->device_info &
1847                     MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1848                         qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1849                         ds = "SSP";
1850                 } else {
1851                         qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1852                          if (raid_device->device_info &
1853                             MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1854                                 ds = "SATA";
1855                         else
1856                                 ds = "STP";
1857                 }
1858
1859                 switch (raid_device->volume_type) {
1860                 case MPI2_RAID_VOL_TYPE_RAID0:
1861                         r_level = "RAID0";
1862                         break;
1863                 case MPI2_RAID_VOL_TYPE_RAID1E:
1864                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1865                         if (ioc->manu_pg10.OEMIdentifier &&
1866                             (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1867                             MFG10_GF0_R10_DISPLAY) &&
1868                             !(raid_device->num_pds % 2))
1869                                 r_level = "RAID10";
1870                         else
1871                                 r_level = "RAID1E";
1872                         break;
1873                 case MPI2_RAID_VOL_TYPE_RAID1:
1874                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1875                         r_level = "RAID1";
1876                         break;
1877                 case MPI2_RAID_VOL_TYPE_RAID10:
1878                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1879                         r_level = "RAID10";
1880                         break;
1881                 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1882                 default:
1883                         qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1884                         r_level = "RAIDX";
1885                         break;
1886                 }
1887
1888                 sdev_printk(KERN_INFO, sdev,
1889                         "%s: handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1890                          r_level, raid_device->handle,
1891                          (unsigned long long)raid_device->wwid,
1892                          raid_device->num_pds, ds);
1893
1894
1895                 _scsih_change_queue_depth(sdev, qdepth);
1896
1897 /* raid transport support */
1898                 _scsih_set_level(sdev, raid_device->volume_type);
1899                 return 0;
1900         }
1901
1902         /* non-raid handling */
1903         if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1904                 if (mpt3sas_config_get_volume_handle(ioc, handle,
1905                     &volume_handle)) {
1906                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1907                             "failure at %s:%d/%s()!\n", ioc->name,
1908                             __FILE__, __LINE__, __func__));
1909                         return 1;
1910                 }
1911                 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1912                     volume_handle, &volume_wwid)) {
1913                         dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1914                             "failure at %s:%d/%s()!\n", ioc->name,
1915                             __FILE__, __LINE__, __func__));
1916                         return 1;
1917                 }
1918         }
1919
1920         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1921         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
1922            sas_device_priv_data->sas_target->sas_address);
1923         if (!sas_device) {
1924                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1925                 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1926                     "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1927                     __func__));
1928                 return 1;
1929         }
1930
1931         sas_device->volume_handle = volume_handle;
1932         sas_device->volume_wwid = volume_wwid;
1933         if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1934                 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1935                 ssp_target = 1;
1936                 ds = "SSP";
1937         } else {
1938                 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1939                 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1940                         ds = "STP";
1941                 else if (sas_device->device_info &
1942                     MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1943                         ds = "SATA";
1944         }
1945
1946         sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1947             "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1948             ds, handle, (unsigned long long)sas_device->sas_address,
1949             sas_device->phy, (unsigned long long)sas_device->device_name);
1950         if (sas_device->enclosure_handle != 0)
1951                 sdev_printk(KERN_INFO, sdev,
1952                      "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1953                      ds, (unsigned long long)
1954                      sas_device->enclosure_logical_id, sas_device->slot);
1955         if (sas_device->connector_name[0] != '\0')
1956                 sdev_printk(KERN_INFO, sdev,
1957                      "%s: enclosure level(0x%04x), connector name( %s)\n",
1958                      ds, sas_device->enclosure_level,
1959                      sas_device->connector_name);
1960
1961         sas_device_put(sas_device);
1962         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1963
1964         if (!ssp_target)
1965                 _scsih_display_sata_capabilities(ioc, handle, sdev);
1966
1967         _scsih_change_queue_depth(sdev, qdepth);
1968
1969         if (ssp_target) {
1970                 sas_read_port_mode_page(sdev);
1971                 _scsih_enable_tlr(ioc, sdev);
1972         }
1973
1974         return 0;
1975 }
1976
1977 /**
1978  * _scsih_bios_param - fetch head, sector, cylinder info for a disk
1979  * @sdev: scsi device struct
1980  * @bdev: pointer to block device context
1981  * @capacity: device size (in 512 byte sectors)
1982  * @params: three element array to place output:
1983  *              params[0] number of heads (max 255)
1984  *              params[1] number of sectors (max 63)
1985  *              params[2] number of cylinders
1986  *
1987  * Return nothing.
1988  */
1989 static int
1990 _scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1991         sector_t capacity, int params[])
1992 {
1993         int             heads;
1994         int             sectors;
1995         sector_t        cylinders;
1996         ulong           dummy;
1997
1998         heads = 64;
1999         sectors = 32;
2000
2001         dummy = heads * sectors;
2002         cylinders = capacity;
2003         sector_div(cylinders, dummy);
2004
2005         /*
2006          * Handle extended translation size for logical drives
2007          * > 1Gb
2008          */
2009         if ((ulong)capacity >= 0x200000) {
2010                 heads = 255;
2011                 sectors = 63;
2012                 dummy = heads * sectors;
2013                 cylinders = capacity;
2014                 sector_div(cylinders, dummy);
2015         }
2016
2017         /* return result */
2018         params[0] = heads;
2019         params[1] = sectors;
2020         params[2] = cylinders;
2021
2022         return 0;
2023 }
2024
2025 /**
2026  * _scsih_response_code - translation of device response code
2027  * @ioc: per adapter object
2028  * @response_code: response code returned by the device
2029  *
2030  * Return nothing.
2031  */
2032 static void
2033 _scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2034 {
2035         char *desc;
2036
2037         switch (response_code) {
2038         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2039                 desc = "task management request completed";
2040                 break;
2041         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2042                 desc = "invalid frame";
2043                 break;
2044         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2045                 desc = "task management request not supported";
2046                 break;
2047         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2048                 desc = "task management request failed";
2049                 break;
2050         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2051                 desc = "task management request succeeded";
2052                 break;
2053         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2054                 desc = "invalid lun";
2055                 break;
2056         case 0xA:
2057                 desc = "overlapped tag attempted";
2058                 break;
2059         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2060                 desc = "task queued, however not sent to target";
2061                 break;
2062         default:
2063                 desc = "unknown";
2064                 break;
2065         }
2066         pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2067                 ioc->name, response_code, desc);
2068 }
2069
2070 /**
2071  * _scsih_tm_done - tm completion routine
2072  * @ioc: per adapter object
2073  * @smid: system request message index
2074  * @msix_index: MSIX table index supplied by the OS
2075  * @reply: reply message frame(lower 32bit addr)
2076  * Context: none.
2077  *
2078  * The callback handler when using scsih_issue_tm.
2079  *
2080  * Return 1 meaning mf should be freed from _base_interrupt
2081  *        0 means the mf is freed from this function.
2082  */
2083 static u8
2084 _scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2085 {
2086         MPI2DefaultReply_t *mpi_reply;
2087
2088         if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2089                 return 1;
2090         if (ioc->tm_cmds.smid != smid)
2091                 return 1;
2092         mpt3sas_base_flush_reply_queues(ioc);
2093         ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2094         mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
2095         if (mpi_reply) {
2096                 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2097                 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2098         }
2099         ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2100         complete(&ioc->tm_cmds.done);
2101         return 1;
2102 }
2103
2104 /**
2105  * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2106  * @ioc: per adapter object
2107  * @handle: device handle
2108  *
2109  * During taskmangement request, we need to freeze the device queue.
2110  */
2111 void
2112 mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2113 {
2114         struct MPT3SAS_DEVICE *sas_device_priv_data;
2115         struct scsi_device *sdev;
2116         u8 skip = 0;
2117
2118         shost_for_each_device(sdev, ioc->shost) {
2119                 if (skip)
2120                         continue;
2121                 sas_device_priv_data = sdev->hostdata;
2122                 if (!sas_device_priv_data)
2123                         continue;
2124                 if (sas_device_priv_data->sas_target->handle == handle) {
2125                         sas_device_priv_data->sas_target->tm_busy = 1;
2126                         skip = 1;
2127                         ioc->ignore_loginfos = 1;
2128                 }
2129         }
2130 }
2131
2132 /**
2133  * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2134  * @ioc: per adapter object
2135  * @handle: device handle
2136  *
2137  * During taskmangement request, we need to freeze the device queue.
2138  */
2139 void
2140 mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2141 {
2142         struct MPT3SAS_DEVICE *sas_device_priv_data;
2143         struct scsi_device *sdev;
2144         u8 skip = 0;
2145
2146         shost_for_each_device(sdev, ioc->shost) {
2147                 if (skip)
2148                         continue;
2149                 sas_device_priv_data = sdev->hostdata;
2150                 if (!sas_device_priv_data)
2151                         continue;
2152                 if (sas_device_priv_data->sas_target->handle == handle) {
2153                         sas_device_priv_data->sas_target->tm_busy = 0;
2154                         skip = 1;
2155                         ioc->ignore_loginfos = 0;
2156                 }
2157         }
2158 }
2159
2160 /**
2161  * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2162  * @ioc: per adapter struct
2163  * @device_handle: device handle
2164  * @channel: the channel assigned by the OS
2165  * @id: the id assigned by the OS
2166  * @lun: lun number
2167  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2168  * @smid_task: smid assigned to the task
2169  * @timeout: timeout in seconds
2170  * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2171  * Context: user
2172  *
2173  * A generic API for sending task management requests to firmware.
2174  *
2175  * The callback index is set inside `ioc->tm_cb_idx`.
2176  *
2177  * Return SUCCESS or FAILED.
2178  */
2179 int
2180 mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2181         uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2182         enum mutex_type m_type)
2183 {
2184         Mpi2SCSITaskManagementRequest_t *mpi_request;
2185         Mpi2SCSITaskManagementReply_t *mpi_reply;
2186         u16 smid = 0;
2187         u32 ioc_state;
2188         unsigned long timeleft;
2189         struct scsiio_tracker *scsi_lookup = NULL;
2190         int rc;
2191
2192         if (m_type == TM_MUTEX_ON)
2193                 mutex_lock(&ioc->tm_cmds.mutex);
2194         if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2195                 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2196                     __func__, ioc->name);
2197                 rc = FAILED;
2198                 goto err_out;
2199         }
2200
2201         if (ioc->shost_recovery || ioc->remove_host ||
2202             ioc->pci_error_recovery) {
2203                 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2204                     __func__, ioc->name);
2205                 rc = FAILED;
2206                 goto err_out;
2207         }
2208
2209         ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2210         if (ioc_state & MPI2_DOORBELL_USED) {
2211                 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2212                         "unexpected doorbell active!\n", ioc->name));
2213                 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2214                     FORCE_BIG_HAMMER);
2215                 rc = (!rc) ? SUCCESS : FAILED;
2216                 goto err_out;
2217         }
2218
2219         if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2220                 mpt3sas_base_fault_info(ioc, ioc_state &
2221                     MPI2_DOORBELL_DATA_MASK);
2222                 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2223                     FORCE_BIG_HAMMER);
2224                 rc = (!rc) ? SUCCESS : FAILED;
2225                 goto err_out;
2226         }
2227
2228         smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2229         if (!smid) {
2230                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2231                     ioc->name, __func__);
2232                 rc = FAILED;
2233                 goto err_out;
2234         }
2235
2236         if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2237                 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2238
2239         dtmprintk(ioc, pr_info(MPT3SAS_FMT
2240                 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2241                 ioc->name, handle, type, smid_task));
2242         ioc->tm_cmds.status = MPT3_CMD_PENDING;
2243         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2244         ioc->tm_cmds.smid = smid;
2245         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2246         memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2247         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2248         mpi_request->DevHandle = cpu_to_le16(handle);
2249         mpi_request->TaskType = type;
2250         mpi_request->TaskMID = cpu_to_le16(smid_task);
2251         int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2252         mpt3sas_scsih_set_tm_flag(ioc, handle);
2253         init_completion(&ioc->tm_cmds.done);
2254         mpt3sas_base_put_smid_hi_priority(ioc, smid);
2255         timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2256         if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2257                 pr_err(MPT3SAS_FMT "%s: timeout\n",
2258                     ioc->name, __func__);
2259                 _debug_dump_mf(mpi_request,
2260                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2261                 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2262                         rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2263                             FORCE_BIG_HAMMER);
2264                         rc = (!rc) ? SUCCESS : FAILED;
2265                         ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2266                         mpt3sas_scsih_clear_tm_flag(ioc, handle);
2267                         goto err_out;
2268                 }
2269         }
2270
2271         if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2272                 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2273                 mpi_reply = ioc->tm_cmds.reply;
2274                 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2275                     "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2276                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2277                     le32_to_cpu(mpi_reply->IOCLogInfo),
2278                     le32_to_cpu(mpi_reply->TerminationCount)));
2279                 if (ioc->logging_level & MPT_DEBUG_TM) {
2280                         _scsih_response_code(ioc, mpi_reply->ResponseCode);
2281                         if (mpi_reply->IOCStatus)
2282                                 _debug_dump_mf(mpi_request,
2283                                     sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2284                 }
2285         }
2286
2287         switch (type) {
2288         case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2289                 rc = SUCCESS;
2290                 if (scsi_lookup->scmd == NULL)
2291                         break;
2292                 rc = FAILED;
2293                 break;
2294
2295         case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2296                 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2297                         rc = FAILED;
2298                 else
2299                         rc = SUCCESS;
2300                 break;
2301         case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2302         case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2303                 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2304                         rc = FAILED;
2305                 else
2306                         rc = SUCCESS;
2307                 break;
2308         case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2309                 rc = SUCCESS;
2310                 break;
2311         default:
2312                 rc = FAILED;
2313                 break;
2314         }
2315
2316         mpt3sas_scsih_clear_tm_flag(ioc, handle);
2317         ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2318         if (m_type == TM_MUTEX_ON)
2319                 mutex_unlock(&ioc->tm_cmds.mutex);
2320
2321         return rc;
2322
2323  err_out:
2324         if (m_type == TM_MUTEX_ON)
2325                 mutex_unlock(&ioc->tm_cmds.mutex);
2326         return rc;
2327 }
2328
2329 /**
2330  * _scsih_tm_display_info - displays info about the device
2331  * @ioc: per adapter struct
2332  * @scmd: pointer to scsi command object
2333  *
2334  * Called by task management callback handlers.
2335  */
2336 static void
2337 _scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2338 {
2339         struct scsi_target *starget = scmd->device->sdev_target;
2340         struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2341         struct _sas_device *sas_device = NULL;
2342         unsigned long flags;
2343         char *device_str = NULL;
2344
2345         if (!priv_target)
2346                 return;
2347         device_str = "volume";
2348
2349         scsi_print_command(scmd);
2350         if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2351                 starget_printk(KERN_INFO, starget,
2352                         "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2353                         device_str, priv_target->handle,
2354                     device_str, (unsigned long long)priv_target->sas_address);
2355         } else {
2356                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2357                 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
2358                 if (sas_device) {
2359                         if (priv_target->flags &
2360                             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2361                                 starget_printk(KERN_INFO, starget,
2362                                     "volume handle(0x%04x), "
2363                                     "volume wwid(0x%016llx)\n",
2364                                     sas_device->volume_handle,
2365                                    (unsigned long long)sas_device->volume_wwid);
2366                         }
2367                         starget_printk(KERN_INFO, starget,
2368                             "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2369                             sas_device->handle,
2370                             (unsigned long long)sas_device->sas_address,
2371                             sas_device->phy);
2372                         if (sas_device->enclosure_handle != 0)
2373                                 starget_printk(KERN_INFO, starget,
2374                                  "enclosure_logical_id(0x%016llx), slot(%d)\n",
2375                                  (unsigned long long)
2376                                  sas_device->enclosure_logical_id,
2377                                  sas_device->slot);
2378                         if (sas_device->connector_name)
2379                                 starget_printk(KERN_INFO, starget,
2380                                 "enclosure level(0x%04x),connector name(%s)\n",
2381                                  sas_device->enclosure_level,
2382                                  sas_device->connector_name);
2383
2384                         sas_device_put(sas_device);
2385                 }
2386         }
2387 }
2388
2389 /**
2390  * _scsih_abort - eh threads main abort routine
2391  * @scmd: pointer to scsi command object
2392  *
2393  * Returns SUCCESS if command aborted else FAILED
2394  */
2395 static int
2396 _scsih_abort(struct scsi_cmnd *scmd)
2397 {
2398         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2399         struct MPT3SAS_DEVICE *sas_device_priv_data;
2400         u16 smid;
2401         u16 handle;
2402         int r;
2403
2404         sdev_printk(KERN_INFO, scmd->device,
2405                 "attempting task abort! scmd(%p)\n", scmd);
2406         _scsih_tm_display_info(ioc, scmd);
2407
2408         sas_device_priv_data = scmd->device->hostdata;
2409         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2410                 sdev_printk(KERN_INFO, scmd->device,
2411                         "device been deleted! scmd(%p)\n", scmd);
2412                 scmd->result = DID_NO_CONNECT << 16;
2413                 scmd->scsi_done(scmd);
2414                 r = SUCCESS;
2415                 goto out;
2416         }
2417
2418         /* search for the command */
2419         smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2420         if (!smid) {
2421                 scmd->result = DID_RESET << 16;
2422                 r = SUCCESS;
2423                 goto out;
2424         }
2425
2426         /* for hidden raid components and volumes this is not supported */
2427         if (sas_device_priv_data->sas_target->flags &
2428             MPT_TARGET_FLAGS_RAID_COMPONENT ||
2429             sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2430                 scmd->result = DID_RESET << 16;
2431                 r = FAILED;
2432                 goto out;
2433         }
2434
2435         mpt3sas_halt_firmware(ioc);
2436
2437         handle = sas_device_priv_data->sas_target->handle;
2438         r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2439             scmd->device->id, scmd->device->lun,
2440             MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
2441
2442  out:
2443         sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2444             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2445         return r;
2446 }
2447
2448 /**
2449  * _scsih_dev_reset - eh threads main device reset routine
2450  * @scmd: pointer to scsi command object
2451  *
2452  * Returns SUCCESS if command aborted else FAILED
2453  */
2454 static int
2455 _scsih_dev_reset(struct scsi_cmnd *scmd)
2456 {
2457         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2458         struct MPT3SAS_DEVICE *sas_device_priv_data;
2459         struct _sas_device *sas_device = NULL;
2460         u16     handle;
2461         int r;
2462         struct scsi_target *starget = scmd->device->sdev_target;
2463         struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2464
2465         sdev_printk(KERN_INFO, scmd->device,
2466                 "attempting device reset! scmd(%p)\n", scmd);
2467         _scsih_tm_display_info(ioc, scmd);
2468
2469         sas_device_priv_data = scmd->device->hostdata;
2470         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2471                 sdev_printk(KERN_INFO, scmd->device,
2472                         "device been deleted! scmd(%p)\n", scmd);
2473                 scmd->result = DID_NO_CONNECT << 16;
2474                 scmd->scsi_done(scmd);
2475                 r = SUCCESS;
2476                 goto out;
2477         }
2478
2479         /* for hidden raid components obtain the volume_handle */
2480         handle = 0;
2481         if (sas_device_priv_data->sas_target->flags &
2482             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2483                 sas_device = mpt3sas_get_sdev_from_target(ioc,
2484                                 target_priv_data);
2485                 if (sas_device)
2486                         handle = sas_device->volume_handle;
2487         } else
2488                 handle = sas_device_priv_data->sas_target->handle;
2489
2490         if (!handle) {
2491                 scmd->result = DID_RESET << 16;
2492                 r = FAILED;
2493                 goto out;
2494         }
2495
2496         r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2497             scmd->device->id, scmd->device->lun,
2498             MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
2499
2500  out:
2501         sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2502             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2503
2504         if (sas_device)
2505                 sas_device_put(sas_device);
2506
2507         return r;
2508 }
2509
2510 /**
2511  * _scsih_target_reset - eh threads main target reset routine
2512  * @scmd: pointer to scsi command object
2513  *
2514  * Returns SUCCESS if command aborted else FAILED
2515  */
2516 static int
2517 _scsih_target_reset(struct scsi_cmnd *scmd)
2518 {
2519         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2520         struct MPT3SAS_DEVICE *sas_device_priv_data;
2521         struct _sas_device *sas_device = NULL;
2522         u16     handle;
2523         int r;
2524         struct scsi_target *starget = scmd->device->sdev_target;
2525         struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2526
2527         starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2528                 scmd);
2529         _scsih_tm_display_info(ioc, scmd);
2530
2531         sas_device_priv_data = scmd->device->hostdata;
2532         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2533                 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2534                         scmd);
2535                 scmd->result = DID_NO_CONNECT << 16;
2536                 scmd->scsi_done(scmd);
2537                 r = SUCCESS;
2538                 goto out;
2539         }
2540
2541         /* for hidden raid components obtain the volume_handle */
2542         handle = 0;
2543         if (sas_device_priv_data->sas_target->flags &
2544             MPT_TARGET_FLAGS_RAID_COMPONENT) {
2545                 sas_device = mpt3sas_get_sdev_from_target(ioc,
2546                                 target_priv_data);
2547                 if (sas_device)
2548                         handle = sas_device->volume_handle;
2549         } else
2550                 handle = sas_device_priv_data->sas_target->handle;
2551
2552         if (!handle) {
2553                 scmd->result = DID_RESET << 16;
2554                 r = FAILED;
2555                 goto out;
2556         }
2557
2558         r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2559             scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2560             30, TM_MUTEX_ON);
2561
2562  out:
2563         starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2564             ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2565
2566         if (sas_device)
2567                 sas_device_put(sas_device);
2568
2569         return r;
2570 }
2571
2572
2573 /**
2574  * _scsih_host_reset - eh threads main host reset routine
2575  * @scmd: pointer to scsi command object
2576  *
2577  * Returns SUCCESS if command aborted else FAILED
2578  */
2579 static int
2580 _scsih_host_reset(struct scsi_cmnd *scmd)
2581 {
2582         struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2583         int r, retval;
2584
2585         pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2586             ioc->name, scmd);
2587         scsi_print_command(scmd);
2588
2589         if (ioc->is_driver_loading) {
2590                 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2591                     ioc->name);
2592                 r = FAILED;
2593                 goto out;
2594         }
2595
2596         retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2597             FORCE_BIG_HAMMER);
2598         r = (retval < 0) ? FAILED : SUCCESS;
2599 out:
2600         pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2601             ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2602
2603         return r;
2604 }
2605
2606 /**
2607  * _scsih_fw_event_add - insert and queue up fw_event
2608  * @ioc: per adapter object
2609  * @fw_event: object describing the event
2610  * Context: This function will acquire ioc->fw_event_lock.
2611  *
2612  * This adds the firmware event object into link list, then queues it up to
2613  * be processed from user context.
2614  *
2615  * Return nothing.
2616  */
2617 static void
2618 _scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2619 {
2620         unsigned long flags;
2621
2622         if (ioc->firmware_event_thread == NULL)
2623                 return;
2624
2625         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2626         fw_event_work_get(fw_event);
2627         INIT_LIST_HEAD(&fw_event->list);
2628         list_add_tail(&fw_event->list, &ioc->fw_event_list);
2629         INIT_WORK(&fw_event->work, _firmware_event_work);
2630         fw_event_work_get(fw_event);
2631         queue_work(ioc->firmware_event_thread, &fw_event->work);
2632         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2633 }
2634
2635 /**
2636  * _scsih_fw_event_del_from_list - delete fw_event from the list
2637  * @ioc: per adapter object
2638  * @fw_event: object describing the event
2639  * Context: This function will acquire ioc->fw_event_lock.
2640  *
2641  * If the fw_event is on the fw_event_list, remove it and do a put.
2642  *
2643  * Return nothing.
2644  */
2645 static void
2646 _scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2647         *fw_event)
2648 {
2649         unsigned long flags;
2650
2651         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2652         if (!list_empty(&fw_event->list)) {
2653                 list_del_init(&fw_event->list);
2654                 fw_event_work_put(fw_event);
2655         }
2656         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2657 }
2658
2659
2660  /**
2661  * mpt3sas_send_trigger_data_event - send event for processing trigger data
2662  * @ioc: per adapter object
2663  * @event_data: trigger event data
2664  *
2665  * Return nothing.
2666  */
2667 void
2668 mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2669         struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2670 {
2671         struct fw_event_work *fw_event;
2672
2673         if (ioc->is_driver_loading)
2674                 return;
2675         fw_event = alloc_fw_event_work(sizeof(*event_data));
2676         if (!fw_event)
2677                 return;
2678         fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2679         fw_event->ioc = ioc;
2680         memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2681         _scsih_fw_event_add(ioc, fw_event);
2682         fw_event_work_put(fw_event);
2683 }
2684
2685 /**
2686  * _scsih_error_recovery_delete_devices - remove devices not responding
2687  * @ioc: per adapter object
2688  *
2689  * Return nothing.
2690  */
2691 static void
2692 _scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2693 {
2694         struct fw_event_work *fw_event;
2695
2696         if (ioc->is_driver_loading)
2697                 return;
2698         fw_event = alloc_fw_event_work(0);
2699         if (!fw_event)
2700                 return;
2701         fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2702         fw_event->ioc = ioc;
2703         _scsih_fw_event_add(ioc, fw_event);
2704         fw_event_work_put(fw_event);
2705 }
2706
2707 /**
2708  * mpt3sas_port_enable_complete - port enable completed (fake event)
2709  * @ioc: per adapter object
2710  *
2711  * Return nothing.
2712  */
2713 void
2714 mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2715 {
2716         struct fw_event_work *fw_event;
2717
2718         fw_event = alloc_fw_event_work(0);
2719         if (!fw_event)
2720                 return;
2721         fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2722         fw_event->ioc = ioc;
2723         _scsih_fw_event_add(ioc, fw_event);
2724         fw_event_work_put(fw_event);
2725 }
2726
2727 static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2728 {
2729         unsigned long flags;
2730         struct fw_event_work *fw_event = NULL;
2731
2732         spin_lock_irqsave(&ioc->fw_event_lock, flags);
2733         if (!list_empty(&ioc->fw_event_list)) {
2734                 fw_event = list_first_entry(&ioc->fw_event_list,
2735                                 struct fw_event_work, list);
2736                 list_del_init(&fw_event->list);
2737         }
2738         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2739
2740         return fw_event;
2741 }
2742
2743 /**
2744  * _scsih_fw_event_cleanup_queue - cleanup event queue
2745  * @ioc: per adapter object
2746  *
2747  * Walk the firmware event queue, either killing timers, or waiting
2748  * for outstanding events to complete
2749  *
2750  * Return nothing.
2751  */
2752 static void
2753 _scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2754 {
2755         struct fw_event_work *fw_event;
2756
2757         if (list_empty(&ioc->fw_event_list) ||
2758              !ioc->firmware_event_thread || in_interrupt())
2759                 return;
2760
2761         while ((fw_event = dequeue_next_fw_event(ioc))) {
2762                 /*
2763                  * Wait on the fw_event to complete. If this returns 1, then
2764                  * the event was never executed, and we need a put for the
2765                  * reference the delayed_work had on the fw_event.
2766                  *
2767                  * If it did execute, we wait for it to finish, and the put will
2768                  * happen from _firmware_event_work()
2769                  */
2770                 if (cancel_work_sync(&fw_event->work))
2771                         fw_event_work_put(fw_event);
2772
2773                 fw_event_work_put(fw_event);
2774         }
2775 }
2776
2777 /**
2778  * _scsih_internal_device_block - block the sdev device
2779  * @sdev: per device object
2780  * @sas_device_priv_data : per device driver private data
2781  *
2782  * make sure device is blocked without error, if not
2783  * print an error
2784  */
2785 static void
2786 _scsih_internal_device_block(struct scsi_device *sdev,
2787                         struct MPT3SAS_DEVICE *sas_device_priv_data)
2788 {
2789         int r = 0;
2790
2791         sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2792             sas_device_priv_data->sas_target->handle);
2793         sas_device_priv_data->block = 1;
2794
2795         r = scsi_internal_device_block(sdev);
2796         if (r == -EINVAL)
2797                 sdev_printk(KERN_WARNING, sdev,
2798                     "device_block failed with return(%d) for handle(0x%04x)\n",
2799                     sas_device_priv_data->sas_target->handle, r);
2800 }
2801
2802 /**
2803  * _scsih_internal_device_unblock - unblock the sdev device
2804  * @sdev: per device object
2805  * @sas_device_priv_data : per device driver private data
2806  * make sure device is unblocked without error, if not retry
2807  * by blocking and then unblocking
2808  */
2809
2810 static void
2811 _scsih_internal_device_unblock(struct scsi_device *sdev,
2812                         struct MPT3SAS_DEVICE *sas_device_priv_data)
2813 {
2814         int r = 0;
2815
2816         sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2817             "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2818         sas_device_priv_data->block = 0;
2819         r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2820         if (r == -EINVAL) {
2821                 /* The device has been set to SDEV_RUNNING by SD layer during
2822                  * device addition but the request queue is still stopped by
2823                  * our earlier block call. We need to perform a block again
2824                  * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2825
2826                 sdev_printk(KERN_WARNING, sdev,
2827                     "device_unblock failed with return(%d) for handle(0x%04x) "
2828                     "performing a block followed by an unblock\n",
2829                     sas_device_priv_data->sas_target->handle, r);
2830                 sas_device_priv_data->block = 1;
2831                 r = scsi_internal_device_block(sdev);
2832                 if (r)
2833                         sdev_printk(KERN_WARNING, sdev, "retried device_block "
2834                             "failed with return(%d) for handle(0x%04x)\n",
2835                             sas_device_priv_data->sas_target->handle, r);
2836
2837                 sas_device_priv_data->block = 0;
2838                 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2839                 if (r)
2840                         sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2841                             " failed with return(%d) for handle(0x%04x)\n",
2842                             sas_device_priv_data->sas_target->handle, r);
2843         }
2844 }
2845
2846 /**
2847  * _scsih_ublock_io_all_device - unblock every device
2848  * @ioc: per adapter object
2849  *
2850  * change the device state from block to running
2851  */
2852 static void
2853 _scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2854 {
2855         struct MPT3SAS_DEVICE *sas_device_priv_data;
2856         struct scsi_device *sdev;
2857
2858         shost_for_each_device(sdev, ioc->shost) {
2859                 sas_device_priv_data = sdev->hostdata;
2860                 if (!sas_device_priv_data)
2861                         continue;
2862                 if (!sas_device_priv_data->block)
2863                         continue;
2864
2865                 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2866                         "device_running, handle(0x%04x)\n",
2867                     sas_device_priv_data->sas_target->handle));
2868                 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
2869         }
2870 }
2871
2872
2873 /**
2874  * _scsih_ublock_io_device - prepare device to be deleted
2875  * @ioc: per adapter object
2876  * @sas_addr: sas address
2877  *
2878  * unblock then put device in offline state
2879  */
2880 static void
2881 _scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2882 {
2883         struct MPT3SAS_DEVICE *sas_device_priv_data;
2884         struct scsi_device *sdev;
2885
2886         shost_for_each_device(sdev, ioc->shost) {
2887                 sas_device_priv_data = sdev->hostdata;
2888                 if (!sas_device_priv_data)
2889                         continue;
2890                 if (sas_device_priv_data->sas_target->sas_address
2891                     != sas_address)
2892                         continue;
2893                 if (sas_device_priv_data->block)
2894                         _scsih_internal_device_unblock(sdev,
2895                                 sas_device_priv_data);
2896         }
2897 }
2898
2899 /**
2900  * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2901  * @ioc: per adapter object
2902  * @handle: device handle
2903  *
2904  * During device pull we need to appropiately set the sdev state.
2905  */
2906 static void
2907 _scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2908 {
2909         struct MPT3SAS_DEVICE *sas_device_priv_data;
2910         struct scsi_device *sdev;
2911
2912         shost_for_each_device(sdev, ioc->shost) {
2913                 sas_device_priv_data = sdev->hostdata;
2914                 if (!sas_device_priv_data)
2915                         continue;
2916                 if (sas_device_priv_data->block)
2917                         continue;
2918                 _scsih_internal_device_block(sdev, sas_device_priv_data);
2919         }
2920 }
2921
2922 /**
2923  * _scsih_block_io_device - set the device state to SDEV_BLOCK
2924  * @ioc: per adapter object
2925  * @handle: device handle
2926  *
2927  * During device pull we need to appropiately set the sdev state.
2928  */
2929 static void
2930 _scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2931 {
2932         struct MPT3SAS_DEVICE *sas_device_priv_data;
2933         struct scsi_device *sdev;
2934         struct _sas_device *sas_device;
2935
2936         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
2937         if (!sas_device)
2938                 return;
2939
2940         shost_for_each_device(sdev, ioc->shost) {
2941                 sas_device_priv_data = sdev->hostdata;
2942                 if (!sas_device_priv_data)
2943                         continue;
2944                 if (sas_device_priv_data->sas_target->handle != handle)
2945                         continue;
2946                 if (sas_device_priv_data->block)
2947                         continue;
2948                 if (sas_device->pend_sas_rphy_add)
2949                         continue;
2950                 _scsih_internal_device_block(sdev, sas_device_priv_data);
2951         }
2952
2953         sas_device_put(sas_device);
2954 }
2955
2956 /**
2957  * _scsih_block_io_to_children_attached_to_ex
2958  * @ioc: per adapter object
2959  * @sas_expander: the sas_device object
2960  *
2961  * This routine set sdev state to SDEV_BLOCK for all devices
2962  * attached to this expander. This function called when expander is
2963  * pulled.
2964  */
2965 static void
2966 _scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
2967         struct _sas_node *sas_expander)
2968 {
2969         struct _sas_port *mpt3sas_port;
2970         struct _sas_device *sas_device;
2971         struct _sas_node *expander_sibling;
2972         unsigned long flags;
2973
2974         if (!sas_expander)
2975                 return;
2976
2977         list_for_each_entry(mpt3sas_port,
2978            &sas_expander->sas_port_list, port_list) {
2979                 if (mpt3sas_port->remote_identify.device_type == SAS_END_DEVICE) {
2980                         spin_lock_irqsave(&ioc->sas_device_lock, flags);
2981                         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
2982                                         mpt3sas_port->remote_identify.sas_address);
2983                         if (sas_device) {
2984                                 set_bit(sas_device->handle,
2985                                         ioc->blocking_handles);
2986                                 sas_device_put(sas_device);
2987                         }
2988                         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2989                 }
2990         }
2991
2992         list_for_each_entry(mpt3sas_port,
2993            &sas_expander->sas_port_list, port_list) {
2994
2995                 if (mpt3sas_port->remote_identify.device_type ==
2996                     SAS_EDGE_EXPANDER_DEVICE ||
2997                     mpt3sas_port->remote_identify.device_type ==
2998                     SAS_FANOUT_EXPANDER_DEVICE) {
2999                         expander_sibling =
3000                             mpt3sas_scsih_expander_find_by_sas_address(
3001                             ioc, mpt3sas_port->remote_identify.sas_address);
3002                         _scsih_block_io_to_children_attached_to_ex(ioc,
3003                             expander_sibling);
3004                 }
3005         }
3006 }
3007
3008 /**
3009  * _scsih_block_io_to_children_attached_directly
3010  * @ioc: per adapter object
3011  * @event_data: topology change event data
3012  *
3013  * This routine set sdev state to SDEV_BLOCK for all devices
3014  * direct attached during device pull.
3015  */
3016 static void
3017 _scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3018         Mpi2EventDataSasTopologyChangeList_t *event_data)
3019 {
3020         int i;
3021         u16 handle;
3022         u16 reason_code;
3023
3024         for (i = 0; i < event_data->NumEntries; i++) {
3025                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3026                 if (!handle)
3027                         continue;
3028                 reason_code = event_data->PHY[i].PhyStatus &
3029                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3030                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3031                         _scsih_block_io_device(ioc, handle);
3032         }
3033 }
3034
3035 /**
3036  * _scsih_tm_tr_send - send task management request
3037  * @ioc: per adapter object
3038  * @handle: device handle
3039  * Context: interrupt time.
3040  *
3041  * This code is to initiate the device removal handshake protocol
3042  * with controller firmware.  This function will issue target reset
3043  * using high priority request queue.  It will send a sas iounit
3044  * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3045  *
3046  * This is designed to send muliple task management request at the same
3047  * time to the fifo. If the fifo is full, we will append the request,
3048  * and process it in a future completion.
3049  */
3050 static void
3051 _scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3052 {
3053         Mpi2SCSITaskManagementRequest_t *mpi_request;
3054         u16 smid;
3055         struct _sas_device *sas_device = NULL;
3056         struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3057         u64 sas_address = 0;
3058         unsigned long flags;
3059         struct _tr_list *delayed_tr;
3060         u32 ioc_state;
3061
3062         if (ioc->remove_host) {
3063                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3064                         "%s: host has been removed: handle(0x%04x)\n",
3065                         __func__, ioc->name, handle));
3066                 return;
3067         } else if (ioc->pci_error_recovery) {
3068                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3069                         "%s: host in pci error recovery: handle(0x%04x)\n",
3070                         __func__, ioc->name,
3071                     handle));
3072                 return;
3073         }
3074         ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3075         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3076                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3077                         "%s: host is not operational: handle(0x%04x)\n",
3078                         __func__, ioc->name,
3079                    handle));
3080                 return;
3081         }
3082
3083         /* if PD, then return */
3084         if (test_bit(handle, ioc->pd_handles))
3085                 return;
3086
3087         spin_lock_irqsave(&ioc->sas_device_lock, flags);
3088         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
3089         if (sas_device && sas_device->starget &&
3090             sas_device->starget->hostdata) {
3091                 sas_target_priv_data = sas_device->starget->hostdata;
3092                 sas_target_priv_data->deleted = 1;
3093                 sas_address = sas_device->sas_address;
3094         }
3095         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3096
3097         if (sas_target_priv_data) {
3098                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3099                         "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3100                         ioc->name, handle,
3101                     (unsigned long long)sas_address));
3102                 if (sas_device->enclosure_handle != 0)
3103                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3104                          "setting delete flag:enclosure logical id(0x%016llx),"
3105                          " slot(%d)\n", ioc->name, (unsigned long long)
3106                           sas_device->enclosure_logical_id,
3107                           sas_device->slot));
3108                 if (sas_device->connector_name)
3109                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3110                          "setting delete flag: enclosure level(0x%04x),"
3111                          " connector name( %s)\n", ioc->name,
3112                           sas_device->enclosure_level,
3113                           sas_device->connector_name));
3114                 _scsih_ublock_io_device(ioc, sas_address);
3115                 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3116         }
3117
3118         smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3119         if (!smid) {
3120                 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3121                 if (!delayed_tr)
3122                         goto out;
3123                 INIT_LIST_HEAD(&delayed_tr->list);
3124                 delayed_tr->handle = handle;
3125                 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3126                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3127                     "DELAYED:tr:handle(0x%04x), (open)\n",
3128                     ioc->name, handle));
3129                 goto out;
3130         }
3131
3132         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3133                 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3134                 ioc->name, handle, smid,
3135             ioc->tm_tr_cb_idx));
3136         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3137         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3138         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3139         mpi_request->DevHandle = cpu_to_le16(handle);
3140         mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3141         mpt3sas_base_put_smid_hi_priority(ioc, smid);
3142         mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
3143 out:
3144         if (sas_device)
3145                 sas_device_put(sas_device);
3146 }
3147
3148 /**
3149  * _scsih_tm_tr_complete -
3150  * @ioc: per adapter object
3151  * @smid: system request message index
3152  * @msix_index: MSIX table index supplied by the OS
3153  * @reply: reply message frame(lower 32bit addr)
3154  * Context: interrupt time.
3155  *
3156  * This is the target reset completion routine.
3157  * This code is part of the code to initiate the device removal
3158  * handshake protocol with controller firmware.
3159  * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3160  *
3161  * Return 1 meaning mf should be freed from _base_interrupt
3162  *        0 means the mf is freed from this function.
3163  */
3164 static u8
3165 _scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3166         u32 reply)
3167 {
3168         u16 handle;
3169         Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3170         Mpi2SCSITaskManagementReply_t *mpi_reply =
3171             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3172         Mpi2SasIoUnitControlRequest_t *mpi_request;
3173         u16 smid_sas_ctrl;
3174         u32 ioc_state;
3175
3176         if (ioc->remove_host) {
3177                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3178                         "%s: host has been removed\n", __func__, ioc->name));
3179                 return 1;
3180         } else if (ioc->pci_error_recovery) {
3181                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3182                         "%s: host in pci error recovery\n", __func__,
3183                         ioc->name));
3184                 return 1;
3185         }
3186         ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3187         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3188                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3189                         "%s: host is not operational\n", __func__, ioc->name));
3190                 return 1;
3191         }
3192         if (unlikely(!mpi_reply)) {
3193                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3194                     ioc->name, __FILE__, __LINE__, __func__);
3195                 return 1;
3196         }
3197         mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3198         handle = le16_to_cpu(mpi_request_tm->DevHandle);
3199         if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3200                 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3201                         "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3202                         ioc->name, handle,
3203                     le16_to_cpu(mpi_reply->DevHandle), smid));
3204                 return 0;
3205         }
3206
3207         mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3208         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3209             "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3210             "loginfo(0x%08x), completed(%d)\n", ioc->name,
3211             handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3212             le32_to_cpu(mpi_reply->IOCLogInfo),
3213             le32_to_cpu(mpi_reply->TerminationCount)));
3214
3215         smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3216         if (!smid_sas_ctrl) {
3217                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3218                     ioc->name, __func__);
3219                 return 1;
3220         }
3221
3222         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3223                 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3224                 ioc->name, handle, smid_sas_ctrl,
3225             ioc->tm_sas_control_cb_idx));
3226         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3227         memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3228         mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3229         mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3230         mpi_request->DevHandle = mpi_request_tm->DevHandle;
3231         mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3232
3233         return _scsih_check_for_pending_tm(ioc, smid);
3234 }
3235
3236
3237 /**
3238  * _scsih_sas_control_complete - completion routine
3239  * @ioc: per adapter object
3240  * @smid: system request message index
3241  * @msix_index: MSIX table index supplied by the OS
3242  * @reply: reply message frame(lower 32bit addr)
3243  * Context: interrupt time.
3244  *
3245  * This is the sas iounit control completion routine.
3246  * This code is part of the code to initiate the device removal
3247  * handshake protocol with controller firmware.
3248  *
3249  * Return 1 meaning mf should be freed from _base_interrupt
3250  *        0 means the mf is freed from this function.
3251  */
3252 static u8
3253 _scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3254         u8 msix_index, u32 reply)
3255 {
3256         Mpi2SasIoUnitControlReply_t *mpi_reply =
3257             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3258
3259         if (likely(mpi_reply)) {
3260                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3261                 "sc_complete:handle(0x%04x), (open) "
3262                 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3263                 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3264                 le16_to_cpu(mpi_reply->IOCStatus),
3265                 le32_to_cpu(mpi_reply->IOCLogInfo)));
3266         } else {
3267                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3268                     ioc->name, __FILE__, __LINE__, __func__);
3269         }
3270         return 1;
3271 }
3272
3273 /**
3274  * _scsih_tm_tr_volume_send - send target reset request for volumes
3275  * @ioc: per adapter object
3276  * @handle: device handle
3277  * Context: interrupt time.
3278  *
3279  * This is designed to send muliple task management request at the same
3280  * time to the fifo. If the fifo is full, we will append the request,
3281  * and process it in a future completion.
3282  */
3283 static void
3284 _scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3285 {
3286         Mpi2SCSITaskManagementRequest_t *mpi_request;
3287         u16 smid;
3288         struct _tr_list *delayed_tr;
3289
3290         if (ioc->shost_recovery || ioc->remove_host ||
3291             ioc->pci_error_recovery) {
3292                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3293                         "%s: host reset in progress!\n",
3294                         __func__, ioc->name));
3295                 return;
3296         }
3297
3298         smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3299         if (!smid) {
3300                 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3301                 if (!delayed_tr)
3302                         return;
3303                 INIT_LIST_HEAD(&delayed_tr->list);
3304                 delayed_tr->handle = handle;
3305                 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3306                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3307                     "DELAYED:tr:handle(0x%04x), (open)\n",
3308                     ioc->name, handle));
3309                 return;
3310         }
3311
3312         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3313                 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3314                 ioc->name, handle, smid,
3315             ioc->tm_tr_volume_cb_idx));
3316         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3317         memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3318         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3319         mpi_request->DevHandle = cpu_to_le16(handle);
3320         mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3321         mpt3sas_base_put_smid_hi_priority(ioc, smid);
3322 }
3323
3324 /**
3325  * _scsih_tm_volume_tr_complete - target reset completion
3326  * @ioc: per adapter object
3327  * @smid: system request message index
3328  * @msix_index: MSIX table index supplied by the OS
3329  * @reply: reply message frame(lower 32bit addr)
3330  * Context: interrupt time.
3331  *
3332  * Return 1 meaning mf should be freed from _base_interrupt
3333  *        0 means the mf is freed from this function.
3334  */
3335 static u8
3336 _scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3337         u8 msix_index, u32 reply)
3338 {
3339         u16 handle;
3340         Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3341         Mpi2SCSITaskManagementReply_t *mpi_reply =
3342             mpt3sas_base_get_reply_virt_addr(ioc, reply);
3343
3344         if (ioc->shost_recovery || ioc->remove_host ||
3345             ioc->pci_error_recovery) {
3346                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3347                         "%s: host reset in progress!\n",
3348                         __func__, ioc->name));
3349                 return 1;
3350         }
3351         if (unlikely(!mpi_reply)) {
3352                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3353                     ioc->name, __FILE__, __LINE__, __func__);
3354                 return 1;
3355         }
3356
3357         mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3358         handle = le16_to_cpu(mpi_request_tm->DevHandle);
3359         if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3360                 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3361                         "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3362                         ioc->name, handle,
3363                     le16_to_cpu(mpi_reply->DevHandle), smid));
3364                 return 0;
3365         }
3366
3367         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3368             "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3369             "loginfo(0x%08x), completed(%d)\n", ioc->name,
3370             handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3371             le32_to_cpu(mpi_reply->IOCLogInfo),
3372             le32_to_cpu(mpi_reply->TerminationCount)));
3373
3374         return _scsih_check_for_pending_tm(ioc, smid);
3375 }
3376
3377
3378 /**
3379  * _scsih_check_for_pending_tm - check for pending task management
3380  * @ioc: per adapter object
3381  * @smid: system request message index
3382  *
3383  * This will check delayed target reset list, and feed the
3384  * next reqeust.
3385  *
3386  * Return 1 meaning mf should be freed from _base_interrupt
3387  *        0 means the mf is freed from this function.
3388  */
3389 static u8
3390 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3391 {
3392         struct _tr_list *delayed_tr;
3393
3394         if (!list_empty(&ioc->delayed_tr_volume_list)) {
3395                 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3396                     struct _tr_list, list);
3397                 mpt3sas_base_free_smid(ioc, smid);
3398                 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3399                 list_del(&delayed_tr->list);
3400                 kfree(delayed_tr);
3401                 return 0;
3402         }
3403
3404         if (!list_empty(&ioc->delayed_tr_list)) {
3405                 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3406                     struct _tr_list, list);
3407                 mpt3sas_base_free_smid(ioc, smid);
3408                 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3409                 list_del(&delayed_tr->list);
3410                 kfree(delayed_tr);
3411                 return 0;
3412         }
3413
3414         return 1;
3415 }
3416
3417 /**
3418  * _scsih_check_topo_delete_events - sanity check on topo events
3419  * @ioc: per adapter object
3420  * @event_data: the event data payload
3421  *
3422  * This routine added to better handle cable breaker.
3423  *
3424  * This handles the case where driver receives multiple expander
3425  * add and delete events in a single shot.  When there is a delete event
3426  * the routine will void any pending add events waiting in the event queue.
3427  *
3428  * Return nothing.
3429  */
3430 static void
3431 _scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3432         Mpi2EventDataSasTopologyChangeList_t *event_data)
3433 {
3434         struct fw_event_work *fw_event;
3435         Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3436         u16 expander_handle;
3437         struct _sas_node *sas_expander;
3438         unsigned long flags;
3439         int i, reason_code;
3440         u16 handle;
3441
3442         for (i = 0 ; i < event_data->NumEntries; i++) {
3443                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3444                 if (!handle)
3445                         continue;
3446                 reason_code = event_data->PHY[i].PhyStatus &
3447                     MPI2_EVENT_SAS_TOPO_RC_MASK;
3448                 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3449                         _scsih_tm_tr_send(ioc, handle);
3450         }
3451
3452         expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3453         if (expander_handle < ioc->sas_hba.num_phys) {
3454                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3455                 return;
3456         }
3457         if (event_data->ExpStatus ==
3458             MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3459                 /* put expander attached devices into blocking state */
3460                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3461                 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3462                     expander_handle);
3463                 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3464                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3465                 do {
3466                         handle = find_first_bit(ioc->blocking_handles,
3467                             ioc->facts.MaxDevHandle);
3468                         if (handle < ioc->facts.MaxDevHandle)
3469                                 _scsih_block_io_device(ioc, handle);
3470                 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3471         } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3472                 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3473
3474         if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3475                 return;
3476
3477         /* mark ignore flag for pending events */
3478         spin_lock_irqsave(&ioc->fw_event_lock, flags);
3479         list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3480                 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3481                     fw_event->ignore)
3482                         continue;
3483                 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3484                                    fw_event->event_data;
3485                 if (local_event_data->ExpStatus ==
3486                     MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3487                     local_event_data->ExpStatus ==
3488                     MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3489                         if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3490                             expander_handle) {
3491                                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3492                                     "setting ignoring flag\n", ioc->name));
3493                                 fw_event->ignore = 1;
3494                         }
3495                 }
3496         }
3497         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3498 }
3499
3500 /**
3501  * _scsih_set_volume_delete_flag - setting volume delete flag
3502  * @ioc: per adapter object
3503  * @handle: device handle
3504  *
3505  * This returns nothing.
3506  */
3507 static void
3508 _scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3509 {
3510         struct _raid_device *raid_device;
3511         struct MPT3SAS_TARGET *sas_target_priv_data;
3512         unsigned long flags;
3513
3514         spin_lock_irqsave(&ioc->raid_device_lock, flags);
3515         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3516         if (raid_device && raid_device->starget &&
3517             raid_device->starget->hostdata) {
3518                 sas_target_priv_data =
3519                     raid_device->starget->hostdata;
3520                 sas_target_priv_data->deleted = 1;
3521                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3522                     "setting delete flag: handle(0x%04x), "
3523                     "wwid(0x%016llx)\n", ioc->name, handle,
3524                     (unsigned long long) raid_device->wwid));
3525         }
3526         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3527 }
3528
3529 /**
3530  * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3531  * @handle: input handle
3532  * @a: handle for volume a
3533  * @b: handle for volume b
3534  *
3535  * IR firmware only supports two raid volumes.  The purpose of this
3536  * routine is to set the volume handle in either a or b. When the given
3537  * input handle is non-zero, or when a and b have not been set before.
3538  */
3539 static void
3540 _scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3541 {
3542         if (!handle || handle == *a || handle == *b)
3543                 return;
3544         if (!*a)
3545                 *a = handle;
3546         else if (!*b)
3547                 *b = handle;
3548 }
3549
3550 /**
3551  * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3552  * @ioc: per adapter object
3553  * @event_data: the event data payload
3554  * Context: interrupt time.
3555  *
3556  * This routine will send target reset to volume, followed by target
3557  * resets to the PDs. This is called when a PD has been removed, or
3558  * volume has been deleted or removed. When the target reset is sent
3559  * to volume, the PD target resets need to be queued to start upon
3560  * completion of the volume target reset.
3561  *
3562  * Return nothing.
3563  */
3564 static void
3565 _scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3566         Mpi2EventDataIrConfigChangeList_t *event_data)
3567 {
3568         Mpi2EventIrConfigElement_t *element;
3569         int i;
3570         u16 handle, volume_handle, a, b;
3571         struct _tr_list *delayed_tr;
3572
3573         a = 0;
3574         b = 0;
3575
3576         /* Volume Resets for Deleted or Removed */
3577         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3578         for (i = 0; i < event_data->NumElements; i++, element++) {
3579                 if (le32_to_cpu(event_data->Flags) &
3580                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3581                         continue;
3582                 if (element->ReasonCode ==
3583                     MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3584                     element->ReasonCode ==
3585                     MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3586                         volume_handle = le16_to_cpu(element->VolDevHandle);
3587                         _scsih_set_volume_delete_flag(ioc, volume_handle);
3588                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3589                 }
3590         }
3591
3592         /* Volume Resets for UNHIDE events */
3593         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3594         for (i = 0; i < event_data->NumElements; i++, element++) {
3595                 if (le32_to_cpu(event_data->Flags) &
3596                     MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3597                         continue;
3598                 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3599                         volume_handle = le16_to_cpu(element->VolDevHandle);
3600                         _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3601                 }
3602         }
3603
3604         if (a)
3605                 _scsih_tm_tr_volume_send(ioc, a);
3606         if (b)
3607                 _scsih_tm_tr_volume_send(ioc, b);
3608
3609         /* PD target resets */
3610         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3611         for (i = 0; i < event_data->NumElements; i++, element++) {
3612                 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3613                         continue;
3614                 handle = le16_to_cpu(element->PhysDiskDevHandle);
3615                 volume_handle = le16_to_cpu(element->VolDevHandle);
3616                 clear_bit(handle, ioc->pd_handles);
3617                 if (!volume_handle)
3618                         _scsih_tm_tr_send(ioc, handle);
3619                 else if (volume_handle == a || volume_handle == b) {
3620                         delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3621                         BUG_ON(!delayed_tr);
3622                         INIT_LIST_HEAD(&delayed_tr->list);
3623                         delayed_tr->handle = handle;
3624                         list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3625                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
3626                             "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3627                             handle));
3628                 } else
3629                         _scsih_tm_tr_send(ioc, handle);
3630         }
3631 }
3632
3633
3634 /**
3635  * _scsih_check_volume_delete_events - set delete flag for volumes
3636  * @ioc: per adapter object
3637  * @event_data: the event data payload
3638  * Context: interrupt time.
3639  *
3640  * This will handle the case when the cable connected to entire volume is
3641  * pulled. We will take care of setting the deleted flag so normal IO will
3642  * not be sent.
3643  *
3644  * Return nothing.
3645  */
3646 static void
3647 _scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3648         Mpi2EventDataIrVolume_t *event_data)
3649 {
3650         u32 state;
3651
3652         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3653                 return;
3654         state = le32_to_cpu(event_data->NewValue);
3655         if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3656             MPI2_RAID_VOL_STATE_FAILED)
3657                 _scsih_set_volume_delete_flag(ioc,
3658                     le16_to_cpu(event_data->VolDevHandle));
3659 }
3660
3661 /**
3662  * _scsih_temp_threshold_events - display temperature threshold exceeded events
3663  * @ioc: per adapter object
3664  * @event_data: the temp threshold event data
3665  * Context: interrupt time.
3666  *
3667  * Return nothing.
3668  */
3669 static void
3670 _scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3671         Mpi2EventDataTemperature_t *event_data)
3672 {
3673         if (ioc->temp_sensors_count >= event_data->SensorNum) {
3674                 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3675                   " exceeded for Sensor: %d !!!\n", ioc->name,
3676                   ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3677                   ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3678                   ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3679                   ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3680                   event_data->SensorNum);
3681                 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3682                         ioc->name, event_data->CurrentTemperature);
3683         }
3684 }
3685
3686 /**
3687  * _scsih_flush_running_cmds - completing outstanding commands.
3688  * @ioc: per adapter object
3689  *
3690  * The flushing out of all pending scmd commands following host reset,
3691  * where all IO is dropped to the floor.
3692  *
3693  * Return nothing.
3694  */
3695 static void
3696 _scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3697 {
3698         struct scsi_cmnd *scmd;
3699         u16 smid;
3700         u16 count = 0;
3701
3702         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3703                 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3704                 if (!scmd)
3705                         continue;
3706                 count++;
3707                 mpt3sas_base_free_smid(ioc, smid);
3708                 scsi_dma_unmap(scmd);
3709                 if (ioc->pci_error_recovery)
3710                         scmd->result = DID_NO_CONNECT << 16;
3711                 else
3712                         scmd->result = DID_RESET << 16;
3713                 scmd->scsi_done(scmd);
3714         }
3715         dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3716             ioc->name, count));
3717 }
3718
3719 /**
3720  * _scsih_setup_eedp - setup MPI request for EEDP transfer
3721  * @ioc: per adapter object
3722  * @scmd: pointer to scsi command object
3723  * @mpi_request: pointer to the SCSI_IO reqest message frame
3724  *
3725  * Supporting protection 1 and 3.
3726  *
3727  * Returns nothing
3728  */
3729 static void
3730 _scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3731         Mpi2SCSIIORequest_t *mpi_request)
3732 {
3733         u16 eedp_flags;
3734         unsigned char prot_op = scsi_get_prot_op(scmd);
3735         unsigned char prot_type = scsi_get_prot_type(scmd);
3736         Mpi25SCSIIORequest_t *mpi_request_3v =
3737            (Mpi25SCSIIORequest_t *)mpi_request;
3738
3739         if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3740                 return;
3741
3742         if (prot_op ==  SCSI_PROT_READ_STRIP)
3743                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3744         else if (prot_op ==  SCSI_PROT_WRITE_INSERT)
3745                 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3746         else
3747                 return;
3748
3749         switch (prot_type) {
3750         case SCSI_PROT_DIF_TYPE1:
3751         case SCSI_PROT_DIF_TYPE2:
3752
3753                 /*
3754                 * enable ref/guard checking
3755                 * auto increment ref tag
3756                 */
3757                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3758                     MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3759                     MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3760                 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3761                     cpu_to_be32(scsi_get_lba(scmd));
3762                 break;
3763
3764         case SCSI_PROT_DIF_TYPE3:
3765
3766                 /*
3767                 * enable guard checking
3768                 */
3769                 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3770
3771                 break;
3772         }
3773
3774         mpi_request_3v->EEDPBlockSize =
3775             cpu_to_le16(scmd->device->sector_size);
3776         mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3777 }
3778
3779 /**
3780  * _scsih_eedp_error_handling - return sense code for EEDP errors
3781  * @scmd: pointer to scsi command object
3782  * @ioc_status: ioc status
3783  *
3784  * Returns nothing
3785  */
3786 static void
3787 _scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3788 {
3789         u8 ascq;
3790
3791         switch (ioc_status) {
3792         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3793                 ascq = 0x01;
3794                 break;
3795         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3796                 ascq = 0x02;
3797                 break;
3798         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3799                 ascq = 0x03;
3800                 break;
3801         default:
3802                 ascq = 0x00;
3803                 break;
3804         }
3805         scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
3806             ascq);
3807         scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3808             SAM_STAT_CHECK_CONDITION;
3809 }
3810
3811
3812 /**
3813  * _scsih_qcmd - main scsi request entry point
3814  * @scmd: pointer to scsi command object
3815  * @done: function pointer to be invoked on completion
3816  *
3817  * The callback index is set inside `ioc->scsi_io_cb_idx`.
3818  *
3819  * Returns 0 on success.  If there's a failure, return either:
3820  * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3821  * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3822  */
3823 static int
3824 _scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
3825 {
3826         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3827         struct MPT3SAS_DEVICE *sas_device_priv_data;
3828         struct MPT3SAS_TARGET *sas_target_priv_data;
3829         Mpi2SCSIIORequest_t *mpi_request;
3830         u32 mpi_control;
3831         u16 smid;
3832         u16 handle;
3833
3834 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3835         if (ioc->logging_level & MPT_DEBUG_SCSI)
3836                 scsi_print_command(scmd);
3837 #endif
3838
3839         sas_device_priv_data = scmd->device->hostdata;
3840         if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3841                 scmd->result = DID_NO_CONNECT << 16;
3842                 scmd->scsi_done(scmd);
3843                 return 0;
3844         }
3845
3846         if (ioc->pci_error_recovery || ioc->remove_host) {
3847                 scmd->result = DID_NO_CONNECT << 16;
3848                 scmd->scsi_done(scmd);
3849                 return 0;
3850         }
3851
3852         sas_target_priv_data = sas_device_priv_data->sas_target;
3853
3854         /* invalid device handle */
3855         handle = sas_target_priv_data->handle;
3856         if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
3857                 scmd->result = DID_NO_CONNECT << 16;
3858                 scmd->scsi_done(scmd);
3859                 return 0;
3860         }
3861
3862
3863         /* host recovery or link resets sent via IOCTLs */
3864         if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3865                 return SCSI_MLQUEUE_HOST_BUSY;
3866
3867         /* device has been deleted */
3868         else if (sas_target_priv_data->deleted) {
3869                 scmd->result = DID_NO_CONNECT << 16;
3870                 scmd->scsi_done(scmd);
3871                 return 0;
3872         /* device busy with task managment */
3873         } else if (sas_target_priv_data->tm_busy ||
3874             sas_device_priv_data->block)
3875                 return SCSI_MLQUEUE_DEVICE_BUSY;
3876
3877         if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3878                 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3879         else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3880                 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3881         else
3882                 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3883
3884         /* set tags */
3885         mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3886
3887         if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3888             scmd->cmd_len != 32)
3889                 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3890
3891         smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3892         if (!smid) {
3893                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3894                     ioc->name, __func__);
3895                 goto out;
3896         }
3897         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3898         memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3899         _scsih_setup_eedp(ioc, scmd, mpi_request);
3900
3901         if (scmd->cmd_len == 32)
3902                 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3903         mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3904         if (sas_device_priv_data->sas_target->flags &
3905             MPT_TARGET_FLAGS_RAID_COMPONENT)
3906                 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3907         else
3908                 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3909         mpi_request->DevHandle = cpu_to_le16(handle);
3910         mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3911         mpi_request->Control = cpu_to_le32(mpi_control);
3912         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3913         mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3914         mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3915         mpi_request->SenseBufferLowAddress =
3916             mpt3sas_base_get_sense_buffer_dma(ioc, smid);
3917         mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3918         int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3919             mpi_request->LUN);
3920         memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3921
3922         if (mpi_request->DataLength) {
3923                 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
3924                         mpt3sas_base_free_smid(ioc, smid);
3925                         goto out;
3926                 }
3927         } else
3928                 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
3929
3930         if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
3931                 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
3932                         mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
3933                             MPI25_SCSIIO_IOFLAGS_FAST_PATH);
3934                         mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
3935                 } else
3936                         mpt3sas_base_put_smid_scsi_io(ioc, smid, handle);
3937         } else
3938                 mpt3sas_base_put_smid_default(ioc, smid);
3939         return 0;
3940
3941  out:
3942         return SCSI_MLQUEUE_HOST_BUSY;
3943 }
3944
3945 /**
3946  * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3947  * @sense_buffer: sense data returned by target
3948  * @data: normalized skey/asc/ascq
3949  *
3950  * Return nothing.
3951  */
3952 static void
3953 _scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3954 {
3955         if ((sense_buffer[0] & 0x7F) >= 0x72) {
3956                 /* descriptor format */
3957                 data->skey = sense_buffer[1] & 0x0F;
3958                 data->asc = sense_buffer[2];
3959                 data->ascq = sense_buffer[3];
3960         } else {
3961                 /* fixed format */
3962                 data->skey = sense_buffer[2] & 0x0F;
3963                 data->asc = sense_buffer[12];
3964                 data->ascq = sense_buffer[13];
3965         }
3966 }
3967
3968 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3969 /**
3970  * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
3971  * @ioc: per adapter object
3972  * @scmd: pointer to scsi command object
3973  * @mpi_reply: reply mf payload returned from firmware
3974  *
3975  * scsi_status - SCSI Status code returned from target device
3976  * scsi_state - state info associated with SCSI_IO determined by ioc
3977  * ioc_status - ioc supplied status info
3978  *
3979  * Return nothing.
3980  */
3981 static void
3982 _scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3983         Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3984 {
3985         u32 response_info;
3986         u8 *response_bytes;
3987         u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3988             MPI2_IOCSTATUS_MASK;
3989         u8 scsi_state = mpi_reply->SCSIState;
3990         u8 scsi_status = mpi_reply->SCSIStatus;
3991         char *desc_ioc_state = NULL;
3992         char *desc_scsi_status = NULL;
3993         char *desc_scsi_state = ioc->tmp_string;
3994         u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3995         struct _sas_device *sas_device = NULL;
3996         struct scsi_target *starget = scmd->device->sdev_target;
3997         struct MPT3SAS_TARGET *priv_target = starget->hostdata;
3998         char *device_str = NULL;
3999
4000         if (!priv_target)
4001                 return;
4002         device_str = "volume";
4003
4004         if (log_info == 0x31170000)
4005                 return;
4006
4007         switch (ioc_status) {
4008         case MPI2_IOCSTATUS_SUCCESS:
4009                 desc_ioc_state = "success";
4010                 break;
4011         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4012                 desc_ioc_state = "invalid function";
4013                 break;
4014         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4015                 desc_ioc_state = "scsi recovered error";
4016                 break;
4017         case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4018                 desc_ioc_state = "scsi invalid dev handle";
4019                 break;
4020         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4021                 desc_ioc_state = "scsi device not there";
4022                 break;
4023         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4024                 desc_ioc_state = "scsi data overrun";
4025                 break;
4026         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4027                 desc_ioc_state = "scsi data underrun";
4028                 break;
4029         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4030                 desc_ioc_state = "scsi io data error";
4031                 break;
4032         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4033                 desc_ioc_state = "scsi protocol error";
4034                 break;
4035         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4036                 desc_ioc_state = "scsi task terminated";
4037                 break;
4038         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4039                 desc_ioc_state = "scsi residual mismatch";
4040                 break;
4041         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4042                 desc_ioc_state = "scsi task mgmt failed";
4043                 break;
4044         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4045                 desc_ioc_state = "scsi ioc terminated";
4046                 break;
4047         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4048                 desc_ioc_state = "scsi ext terminated";
4049                 break;
4050         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4051                 desc_ioc_state = "eedp guard error";
4052                 break;
4053         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4054                 desc_ioc_state = "eedp ref tag error";
4055                 break;
4056         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4057                 desc_ioc_state = "eedp app tag error";
4058                 break;
4059         default:
4060                 desc_ioc_state = "unknown";
4061                 break;
4062         }
4063
4064         switch (scsi_status) {
4065         case MPI2_SCSI_STATUS_GOOD:
4066                 desc_scsi_status = "good";
4067                 break;
4068         case MPI2_SCSI_STATUS_CHECK_CONDITION:
4069                 desc_scsi_status = "check condition";
4070                 break;
4071         case MPI2_SCSI_STATUS_CONDITION_MET:
4072                 desc_scsi_status = "condition met";
4073                 break;
4074         case MPI2_SCSI_STATUS_BUSY:
4075                 desc_scsi_status = "busy";
4076                 break;
4077         case MPI2_SCSI_STATUS_INTERMEDIATE:
4078                 desc_scsi_status = "intermediate";
4079                 break;
4080         case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4081                 desc_scsi_status = "intermediate condmet";
4082                 break;
4083         case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4084                 desc_scsi_status = "reservation conflict";
4085                 break;
4086         case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4087                 desc_scsi_status = "command terminated";
4088                 break;
4089         case MPI2_SCSI_STATUS_TASK_SET_FULL:
4090                 desc_scsi_status = "task set full";
4091                 break;
4092         case MPI2_SCSI_STATUS_ACA_ACTIVE:
4093                 desc_scsi_status = "aca active";
4094                 break;
4095         case MPI2_SCSI_STATUS_TASK_ABORTED:
4096                 desc_scsi_status = "task aborted";
4097                 break;
4098         default:
4099                 desc_scsi_status = "unknown";
4100                 break;
4101         }
4102
4103         desc_scsi_state[0] = '\0';
4104         if (!scsi_state)
4105                 desc_scsi_state = " ";
4106         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4107                 strcat(desc_scsi_state, "response info ");
4108         if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4109                 strcat(desc_scsi_state, "state terminated ");
4110         if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4111                 strcat(desc_scsi_state, "no status ");
4112         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4113                 strcat(desc_scsi_state, "autosense failed ");
4114         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4115                 strcat(desc_scsi_state, "autosense valid ");
4116
4117         scsi_print_command(scmd);
4118
4119         if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4120                 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4121                     device_str, (unsigned long long)priv_target->sas_address);
4122         } else {
4123                 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
4124                 if (sas_device) {
4125                         pr_warn(MPT3SAS_FMT
4126                                 "\tsas_address(0x%016llx), phy(%d)\n",
4127                                 ioc->name, (unsigned long long)
4128                             sas_device->sas_address, sas_device->phy);
4129                         if (sas_device->enclosure_handle != 0)
4130                                 pr_warn(MPT3SAS_FMT
4131                                   "\tenclosure_logical_id(0x%016llx),"
4132                                   "slot(%d)\n", ioc->name,
4133                                   (unsigned long long)
4134                                   sas_device->enclosure_logical_id,
4135                                   sas_device->slot);
4136                         if (sas_device->connector_name[0])
4137                                 pr_warn(MPT3SAS_FMT
4138                                   "\tenclosure level(0x%04x),"
4139                                   " connector name( %s)\n", ioc->name,
4140                                   sas_device->enclosure_level,
4141                                   sas_device->connector_name);
4142
4143                         sas_device_put(sas_device);
4144                 }
4145         }
4146
4147         pr_warn(MPT3SAS_FMT
4148                 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4149                 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4150             desc_ioc_state, ioc_status, smid);
4151         pr_warn(MPT3SAS_FMT
4152                 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4153                 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4154             scsi_get_resid(scmd));
4155         pr_warn(MPT3SAS_FMT
4156                 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4157                 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4158             le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4159         pr_warn(MPT3SAS_FMT
4160                 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4161                 ioc->name, desc_scsi_status,
4162             scsi_status, desc_scsi_state, scsi_state);
4163
4164         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4165                 struct sense_info data;
4166                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4167                 pr_warn(MPT3SAS_FMT
4168                         "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4169                         ioc->name, data.skey,
4170                     data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4171         }
4172
4173         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4174                 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4175                 response_bytes = (u8 *)&response_info;
4176                 _scsih_response_code(ioc, response_bytes[0]);
4177         }
4178 }
4179 #endif
4180
4181 /**
4182  * _scsih_turn_on_pfa_led - illuminate PFA LED
4183  * @ioc: per adapter object
4184  * @handle: device handle
4185  * Context: process
4186  *
4187  * Return nothing.
4188  */
4189 static void
4190 _scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4191 {
4192         Mpi2SepReply_t mpi_reply;
4193         Mpi2SepRequest_t mpi_request;
4194         struct _sas_device *sas_device;
4195
4196         sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
4197         if (!sas_device)
4198                 return;
4199
4200         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4201         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4202         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4203         mpi_request.SlotStatus =
4204             cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4205         mpi_request.DevHandle = cpu_to_le16(handle);
4206         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4207         if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4208             &mpi_request)) != 0) {
4209                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4210                 __FILE__, __LINE__, __func__);
4211                 goto out;
4212         }
4213         sas_device->pfa_led_on = 1;
4214
4215         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4216                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4217                         "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4218                         ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4219                     le32_to_cpu(mpi_reply.IOCLogInfo)));
4220                 goto out;
4221         }
4222 out:
4223         sas_device_put(sas_device);
4224 }
4225 /**
4226  * _scsih_turn_off_pfa_led - turn off Fault LED
4227  * @ioc: per adapter object
4228  * @sas_device: sas device whose PFA LED has to turned off
4229  * Context: process
4230  *
4231  * Return nothing.
4232  */
4233 static void
4234 _scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4235         struct _sas_device *sas_device)
4236 {
4237         Mpi2SepReply_t mpi_reply;
4238         Mpi2SepRequest_t mpi_request;
4239
4240         memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4241         mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4242         mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4243         mpi_request.SlotStatus = 0;
4244         mpi_request.Slot = cpu_to_le16(sas_device->slot);
4245         mpi_request.DevHandle = 0;
4246         mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4247         mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4248         if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4249                 &mpi_request)) != 0) {
4250                 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4251                 __FILE__, __LINE__, __func__);
4252                 return;
4253         }
4254
4255         if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4256                 dewtprintk(ioc, printk(MPT3SAS_FMT
4257                  "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4258                  ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4259                  le32_to_cpu(mpi_reply.IOCLogInfo)));
4260                 return;
4261         }
4262 }
4263 /**
4264  * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
4265  * @ioc: per adapter object
4266  * @handle: device handle
4267  * Context: interrupt.
4268  *
4269  * Return nothing.
4270  */
4271 static void
4272 _scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4273 {
4274         struct fw_event_work *fw_event;
4275
4276         fw_event = alloc_fw_event_work(0);
4277         if (!fw_event)
4278                 return;
4279         fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
4280         fw_event->device_handle = handle;
4281         fw_event->ioc = ioc;
4282         _scsih_fw_event_add(ioc, fw_event);
4283         fw_event_work_put(fw_event);
4284 }
4285
4286 /**
4287  * _scsih_smart_predicted_fault - process smart errors
4288  * @ioc: per adapter object
4289  * @handle: device handle
4290  * Context: interrupt.
4291  *
4292  * Return nothing.
4293  */
4294 static void
4295 _scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4296 {
4297         struct scsi_target *starget;
4298         struct MPT3SAS_TARGET *sas_target_priv_data;
4299         Mpi2EventNotificationReply_t *event_reply;
4300         Mpi2EventDataSasDeviceStatusChange_t *event_data;
4301         struct _sas_device *sas_device;
4302         ssize_t sz;
4303         unsigned long flags;
4304
4305         /* only handle non-raid devices */
4306         spin_lock_irqsave(&ioc->sas_device_lock, flags);
4307         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4308         if (!sas_device) {
4309                 goto out_unlock;
4310         }
4311         starget = sas_device->starget;
4312         sas_target_priv_data = starget->hostdata;
4313
4314         if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4315            ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4316                 goto out_unlock;
4317
4318         if (sas_device->enclosure_handle != 0)
4319                 starget_printk(KERN_INFO, starget, "predicted fault, "
4320                         "enclosure logical id(0x%016llx), slot(%d)\n",
4321                         (unsigned long long)sas_device->enclosure_logical_id,
4322                         sas_device->slot);
4323         if (sas_device->connector_name[0] != '\0')
4324                 starget_printk(KERN_WARNING, starget, "predicted fault, "
4325                         "enclosure level(0x%04x), connector name( %s)\n",
4326                         sas_device->enclosure_level,
4327                         sas_device->connector_name);
4328         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4329
4330         if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4331                 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
4332
4333         /* insert into event log */
4334         sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4335              sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4336         event_reply = kzalloc(sz, GFP_KERNEL);
4337         if (!event_reply) {
4338                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4339                     ioc->name, __FILE__, __LINE__, __func__);
4340                 goto out;
4341         }
4342
4343         event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4344         event_reply->Event =
4345             cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4346         event_reply->MsgLength = sz/4;
4347         event_reply->EventDataLength =
4348             cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4349         event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4350             event_reply->EventData;
4351         event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4352         event_data->ASC = 0x5D;
4353         event_data->DevHandle = cpu_to_le16(handle);
4354         event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4355         mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4356         kfree(event_reply);
4357 out:
4358         if (sas_device)
4359                 sas_device_put(sas_device);
4360         return;
4361
4362 out_unlock:
4363         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4364         goto out;
4365 }
4366
4367 /**
4368  * _scsih_io_done - scsi request callback
4369  * @ioc: per adapter object
4370  * @smid: system request message index
4371  * @msix_index: MSIX table index supplied by the OS
4372  * @reply: reply message frame(lower 32bit addr)
4373  *
4374  * Callback handler when using _scsih_qcmd.
4375  *
4376  * Return 1 meaning mf should be freed from _base_interrupt
4377  *        0 means the mf is freed from this function.
4378  */
4379 static u8
4380 _scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4381 {
4382         Mpi2SCSIIORequest_t *mpi_request;
4383         Mpi2SCSIIOReply_t *mpi_reply;
4384         struct scsi_cmnd *scmd;
4385         u16 ioc_status;
4386         u32 xfer_cnt;
4387         u8 scsi_state;
4388         u8 scsi_status;
4389         u32 log_info;
4390         struct MPT3SAS_DEVICE *sas_device_priv_data;
4391         u32 response_code = 0;
4392
4393         mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4394         scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4395         if (scmd == NULL)
4396                 return 1;
4397
4398         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4399
4400         if (mpi_reply == NULL) {
4401                 scmd->result = DID_OK << 16;
4402                 goto out;
4403         }
4404
4405         sas_device_priv_data = scmd->device->hostdata;
4406         if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4407              sas_device_priv_data->sas_target->deleted) {
4408                 scmd->result = DID_NO_CONNECT << 16;
4409                 goto out;
4410         }
4411         ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4412
4413         /* turning off TLR */
4414         scsi_state = mpi_reply->SCSIState;
4415         if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4416                 response_code =
4417                     le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4418         if (!sas_device_priv_data->tlr_snoop_check) {
4419                 sas_device_priv_data->tlr_snoop_check++;
4420                 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
4421                     response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
4422                         sas_device_priv_data->flags &=
4423                             ~MPT_DEVICE_TLR_ON;
4424         }
4425
4426         xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4427         scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4428         if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4429                 log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
4430         else
4431                 log_info = 0;
4432         ioc_status &= MPI2_IOCSTATUS_MASK;
4433         scsi_status = mpi_reply->SCSIStatus;
4434
4435         if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4436             (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4437              scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4438              scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4439                 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4440         }
4441
4442         if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4443                 struct sense_info data;
4444                 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4445                     smid);
4446                 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4447                     le32_to_cpu(mpi_reply->SenseCount));
4448                 memcpy(scmd->sense_buffer, sense_data, sz);
4449                 _scsih_normalize_sense(scmd->sense_buffer, &data);
4450                 /* failure prediction threshold exceeded */
4451                 if (data.asc == 0x5D)
4452                         _scsih_smart_predicted_fault(ioc,
4453                             le16_to_cpu(mpi_reply->DevHandle));
4454                 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
4455
4456 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4457                 if (!(ioc->logging_level & MPT_DEBUG_REPLY) &&
4458                      ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4459                      (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4460                      (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4461                         _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4462 #endif
4463         }
4464         switch (ioc_status) {
4465         case MPI2_IOCSTATUS_BUSY:
4466         case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4467                 scmd->result = SAM_STAT_BUSY;
4468                 break;
4469
4470         case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4471                 scmd->result = DID_NO_CONNECT << 16;
4472                 break;
4473
4474         case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4475                 if (sas_device_priv_data->block) {
4476                         scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4477                         goto out;
4478                 }
4479                 if (log_info == 0x31110630) {
4480                         if (scmd->retries > 2) {
4481                                 scmd->result = DID_NO_CONNECT << 16;
4482                                 scsi_device_set_state(scmd->device,
4483                                     SDEV_OFFLINE);
4484                         } else {
4485                                 scmd->result = DID_SOFT_ERROR << 16;
4486                                 scmd->device->expecting_cc_ua = 1;
4487                         }
4488                         break;
4489                 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4490                         scmd->result = DID_RESET << 16;
4491                         break;
4492                 }
4493                 scmd->result = DID_SOFT_ERROR << 16;
4494                 break;
4495         case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4496         case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4497                 scmd->result = DID_RESET << 16;
4498                 break;
4499
4500         case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4501                 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4502                         scmd->result = DID_SOFT_ERROR << 16;
4503                 else
4504                         scmd->result = (DID_OK << 16) | scsi_status;
4505                 break;
4506
4507         case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4508                 scmd->result = (DID_OK << 16) | scsi_status;
4509
4510                 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4511                         break;
4512
4513                 if (xfer_cnt < scmd->underflow) {
4514                         if (scsi_status == SAM_STAT_BUSY)
4515                                 scmd->result = SAM_STAT_BUSY;
4516                         else
4517                                 scmd->result = DID_SOFT_ERROR << 16;
4518                 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4519                      MPI2_SCSI_STATE_NO_SCSI_STATUS))
4520                         scmd->result = DID_SOFT_ERROR << 16;
4521                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4522                         scmd->result = DID_RESET << 16;
4523                 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4524                         mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4525                         mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4526                         scmd->result = (DRIVER_SENSE << 24) |
4527                             SAM_STAT_CHECK_CONDITION;
4528                         scmd->sense_buffer[0] = 0x70;
4529                         scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4530                         scmd->sense_buffer[12] = 0x20;
4531                         scmd->sense_buffer[13] = 0;
4532                 }
4533                 break;
4534
4535         case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4536                 scsi_set_resid(scmd, 0);
4537         case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4538         case MPI2_IOCSTATUS_SUCCESS:
4539                 scmd->result = (DID_OK << 16) | scsi_status;
4540                 if (response_code ==
4541                     MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4542                     (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4543                      MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4544                         scmd->result = DID_SOFT_ERROR << 16;
4545                 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4546                         scmd->result = DID_RESET << 16;
4547                 break;
4548
4549         case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4550         case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4551         case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4552                 _scsih_eedp_error_handling(scmd, ioc_status);
4553                 break;
4554
4555         case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4556         case MPI2_IOCSTATUS_INVALID_FUNCTION:
4557         case MPI2_IOCSTATUS_INVALID_SGL:
4558         case MPI2_IOCSTATUS_INTERNAL_ERROR:
4559         case MPI2_IOCSTATUS_INVALID_FIELD:
4560         case MPI2_IOCSTATUS_INVALID_STATE:
4561         case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4562         case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4563         default:
4564                 scmd->result = DID_SOFT_ERROR << 16;
4565                 break;
4566
4567         }
4568
4569 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4570         if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4571                 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4572 #endif
4573
4574  out:
4575
4576         scsi_dma_unmap(scmd);
4577
4578         scmd->scsi_done(scmd);
4579         return 1;
4580 }
4581
4582 /**
4583  * _scsih_sas_host_refresh - refreshing sas host object contents
4584  * @ioc: per adapter object
4585  * Context: user
4586  *
4587  * During port enable, fw will send topology events for every device. Its
4588  * possible that the handles may change from the previous setting, so this
4589  * code keeping handles updating if changed.
4590  *
4591  * Return nothing.
4592  */
4593 static void
4594 _scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4595 {
4596         u16 sz;
4597         u16 ioc_status;
4598         int i;
4599         Mpi2ConfigReply_t mpi_reply;
4600         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4601         u16 attached_handle;
4602         u8 link_rate;
4603
4604         dtmprintk(ioc, pr_info(MPT3SAS_FMT
4605             "updating handles for sas_host(0x%016llx)\n",
4606             ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4607
4608         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4609             * sizeof(Mpi2SasIOUnit0PhyData_t));
4610         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4611         if (!sas_iounit_pg0) {
4612                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4613                     ioc->name, __FILE__, __LINE__, __func__);
4614                 return;
4615         }
4616
4617         if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4618             sas_iounit_pg0, sz)) != 0)
4619                 goto out;
4620         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4621         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4622                 goto out;
4623         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4624                 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4625                 if (i == 0)
4626                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4627                             PhyData[0].ControllerDevHandle);
4628                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4629                 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4630                     AttachedDevHandle);
4631                 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4632                         link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4633                 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4634                     attached_handle, i, link_rate);
4635         }
4636  out:
4637         kfree(sas_iounit_pg0);
4638 }
4639
4640 /**
4641  * _scsih_sas_host_add - create sas host object
4642  * @ioc: per adapter object
4643  *
4644  * Creating host side data object, stored in ioc->sas_hba
4645  *
4646  * Return nothing.
4647  */
4648 static void
4649 _scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4650 {
4651         int i;
4652         Mpi2ConfigReply_t mpi_reply;
4653         Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4654         Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4655         Mpi2SasPhyPage0_t phy_pg0;
4656         Mpi2SasDevicePage0_t sas_device_pg0;
4657         Mpi2SasEnclosurePage0_t enclosure_pg0;
4658         u16 ioc_status;
4659         u16 sz;
4660         u8 device_missing_delay;
4661
4662         mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4663         if (!ioc->sas_hba.num_phys) {
4664                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4665                     ioc->name, __FILE__, __LINE__, __func__);
4666                 return;
4667         }
4668
4669         /* sas_iounit page 0 */
4670         sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4671             sizeof(Mpi2SasIOUnit0PhyData_t));
4672         sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4673         if (!sas_iounit_pg0) {
4674                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4675                     ioc->name, __FILE__, __LINE__, __func__);
4676                 return;
4677         }
4678         if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4679             sas_iounit_pg0, sz))) {
4680                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4681                     ioc->name, __FILE__, __LINE__, __func__);
4682                 goto out;
4683         }
4684         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4685             MPI2_IOCSTATUS_MASK;
4686         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4687                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4688                     ioc->name, __FILE__, __LINE__, __func__);
4689                 goto out;
4690         }
4691
4692         /* sas_iounit page 1 */
4693         sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4694             sizeof(Mpi2SasIOUnit1PhyData_t));
4695         sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4696         if (!sas_iounit_pg1) {
4697                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4698                     ioc->name, __FILE__, __LINE__, __func__);
4699                 goto out;
4700         }
4701         if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4702             sas_iounit_pg1, sz))) {
4703                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4704                     ioc->name, __FILE__, __LINE__, __func__);
4705                 goto out;
4706         }
4707         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4708             MPI2_IOCSTATUS_MASK;
4709         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4710                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4711                     ioc->name, __FILE__, __LINE__, __func__);
4712                 goto out;
4713         }
4714
4715         ioc->io_missing_delay =
4716             sas_iounit_pg1->IODeviceMissingDelay;
4717         device_missing_delay =
4718             sas_iounit_pg1->ReportDeviceMissingDelay;
4719         if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4720                 ioc->device_missing_delay = (device_missing_delay &
4721                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4722         else
4723                 ioc->device_missing_delay = device_missing_delay &
4724                     MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4725
4726         ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4727         ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4728             sizeof(struct _sas_phy), GFP_KERNEL);
4729         if (!ioc->sas_hba.phy) {
4730                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4731                     ioc->name, __FILE__, __LINE__, __func__);
4732                 goto out;
4733         }
4734         for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4735                 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4736                     i))) {
4737                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4738                             ioc->name, __FILE__, __LINE__, __func__);
4739                         goto out;
4740                 }
4741                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4742                     MPI2_IOCSTATUS_MASK;
4743                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4744                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4745                             ioc->name, __FILE__, __LINE__, __func__);
4746                         goto out;
4747                 }
4748
4749                 if (i == 0)
4750                         ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4751                             PhyData[0].ControllerDevHandle);
4752                 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4753                 ioc->sas_hba.phy[i].phy_id = i;
4754                 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4755                     phy_pg0, ioc->sas_hba.parent_dev);
4756         }
4757         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4758             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4759                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4760                     ioc->name, __FILE__, __LINE__, __func__);
4761                 goto out;
4762         }
4763         ioc->sas_hba.enclosure_handle =
4764             le16_to_cpu(sas_device_pg0.EnclosureHandle);
4765         ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4766         pr_info(MPT3SAS_FMT
4767                 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4768                 ioc->name, ioc->sas_hba.handle,
4769             (unsigned long long) ioc->sas_hba.sas_address,
4770             ioc->sas_hba.num_phys) ;
4771
4772         if (ioc->sas_hba.enclosure_handle) {
4773                 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4774                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4775                    ioc->sas_hba.enclosure_handle)))
4776                         ioc->sas_hba.enclosure_logical_id =
4777                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4778         }
4779
4780  out:
4781         kfree(sas_iounit_pg1);
4782         kfree(sas_iounit_pg0);
4783 }
4784
4785 /**
4786  * _scsih_expander_add -  creating expander object
4787  * @ioc: per adapter object
4788  * @handle: expander handle
4789  *
4790  * Creating expander object, stored in ioc->sas_expander_list.
4791  *
4792  * Return 0 for success, else error.
4793  */
4794 static int
4795 _scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4796 {
4797         struct _sas_node *sas_expander;
4798         Mpi2ConfigReply_t mpi_reply;
4799         Mpi2ExpanderPage0_t expander_pg0;
4800         Mpi2ExpanderPage1_t expander_pg1;
4801         Mpi2SasEnclosurePage0_t enclosure_pg0;
4802         u32 ioc_status;
4803         u16 parent_handle;
4804         u64 sas_address, sas_address_parent = 0;
4805         int i;
4806         unsigned long flags;
4807         struct _sas_port *mpt3sas_port = NULL;
4808
4809         int rc = 0;
4810
4811         if (!handle)
4812                 return -1;
4813
4814         if (ioc->shost_recovery || ioc->pci_error_recovery)
4815                 return -1;
4816
4817         if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4818             MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4819                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4820                     ioc->name, __FILE__, __LINE__, __func__);
4821                 return -1;
4822         }
4823
4824         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4825             MPI2_IOCSTATUS_MASK;
4826         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4827                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4828                     ioc->name, __FILE__, __LINE__, __func__);
4829                 return -1;
4830         }
4831
4832         /* handle out of order topology events */
4833         parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4834         if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4835             != 0) {
4836                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4837                     ioc->name, __FILE__, __LINE__, __func__);
4838                 return -1;
4839         }
4840         if (sas_address_parent != ioc->sas_hba.sas_address) {
4841                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4842                 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4843                     sas_address_parent);
4844                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4845                 if (!sas_expander) {
4846                         rc = _scsih_expander_add(ioc, parent_handle);
4847                         if (rc != 0)
4848                                 return rc;
4849                 }
4850         }
4851
4852         spin_lock_irqsave(&ioc->sas_node_lock, flags);
4853         sas_address = le64_to_cpu(expander_pg0.SASAddress);
4854         sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4855             sas_address);
4856         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4857
4858         if (sas_expander)
4859                 return 0;
4860
4861         sas_expander = kzalloc(sizeof(struct _sas_node),
4862             GFP_KERNEL);
4863         if (!sas_expander) {
4864                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4865                     ioc->name, __FILE__, __LINE__, __func__);
4866                 return -1;
4867         }
4868
4869         sas_expander->handle = handle;
4870         sas_expander->num_phys = expander_pg0.NumPhys;
4871         sas_expander->sas_address_parent = sas_address_parent;
4872         sas_expander->sas_address = sas_address;
4873
4874         pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
4875             " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4876             handle, parent_handle, (unsigned long long)
4877             sas_expander->sas_address, sas_expander->num_phys);
4878
4879         if (!sas_expander->num_phys)
4880                 goto out_fail;
4881         sas_expander->phy = kcalloc(sas_expander->num_phys,
4882             sizeof(struct _sas_phy), GFP_KERNEL);
4883         if (!sas_expander->phy) {
4884                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4885                     ioc->name, __FILE__, __LINE__, __func__);
4886                 rc = -1;
4887                 goto out_fail;
4888         }
4889
4890         INIT_LIST_HEAD(&sas_expander->sas_port_list);
4891         mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
4892             sas_address_parent);
4893         if (!mpt3sas_port) {
4894                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4895                     ioc->name, __FILE__, __LINE__, __func__);
4896                 rc = -1;
4897                 goto out_fail;
4898         }
4899         sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
4900
4901         for (i = 0 ; i < sas_expander->num_phys ; i++) {
4902                 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
4903                     &expander_pg1, i, handle))) {
4904                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4905                             ioc->name, __FILE__, __LINE__, __func__);
4906                         rc = -1;
4907                         goto out_fail;
4908                 }
4909                 sas_expander->phy[i].handle = handle;
4910                 sas_expander->phy[i].phy_id = i;
4911
4912                 if ((mpt3sas_transport_add_expander_phy(ioc,
4913                     &sas_expander->phy[i], expander_pg1,
4914                     sas_expander->parent_dev))) {
4915                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4916                             ioc->name, __FILE__, __LINE__, __func__);
4917                         rc = -1;
4918                         goto out_fail;
4919                 }
4920         }
4921
4922         if (sas_expander->enclosure_handle) {
4923                 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4924                     &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4925                    sas_expander->enclosure_handle)))
4926                         sas_expander->enclosure_logical_id =
4927                             le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4928         }
4929
4930         _scsih_expander_node_add(ioc, sas_expander);
4931          return 0;
4932
4933  out_fail:
4934
4935         if (mpt3sas_port)
4936                 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
4937                     sas_address_parent);
4938         kfree(sas_expander);
4939         return rc;
4940 }
4941
4942 /**
4943  * mpt3sas_expander_remove - removing expander object
4944  * @ioc: per adapter object
4945  * @sas_address: expander sas_address
4946  *
4947  * Return nothing.
4948  */
4949 void
4950 mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
4951 {
4952         struct _sas_node *sas_expander;
4953         unsigned long flags;
4954
4955         if (ioc->shost_recovery)
4956                 return;
4957
4958         spin_lock_irqsave(&ioc->sas_node_lock, flags);
4959         sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4960             sas_address);
4961         if (sas_expander)
4962                 list_del(&sas_expander->list);
4963         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4964         if (sas_expander)
4965                 _scsih_expander_node_remove(ioc, sas_expander);
4966 }
4967
4968 /**
4969  * _scsih_done -  internal SCSI_IO callback handler.
4970  * @ioc: per adapter object
4971  * @smid: system request message index
4972  * @msix_index: MSIX table index supplied by the OS
4973  * @reply: reply message frame(lower 32bit addr)
4974  *
4975  * Callback handler when sending internal generated SCSI_IO.
4976  * The callback index passed is `ioc->scsih_cb_idx`
4977  *
4978  * Return 1 meaning mf should be freed from _base_interrupt
4979  *        0 means the mf is freed from this function.
4980  */
4981 static u8
4982 _scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4983 {
4984         MPI2DefaultReply_t *mpi_reply;
4985
4986         mpi_reply =  mpt3sas_base_get_reply_virt_addr(ioc, reply);
4987         if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
4988                 return 1;
4989         if (ioc->scsih_cmds.smid != smid)
4990                 return 1;
4991         ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
4992         if (mpi_reply) {
4993                 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4994                     mpi_reply->MsgLength*4);
4995                 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
4996         }
4997         ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
4998         complete(&ioc->scsih_cmds.done);
4999         return 1;
5000 }
5001
5002
5003
5004
5005 #define MPT3_MAX_LUNS (255)
5006
5007
5008 /**
5009  * _scsih_check_access_status - check access flags
5010  * @ioc: per adapter object
5011  * @sas_address: sas address
5012  * @handle: sas device handle
5013  * @access_flags: errors returned during discovery of the device
5014  *
5015  * Return 0 for success, else failure
5016  */
5017 static u8
5018 _scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5019         u16 handle, u8 access_status)
5020 {
5021         u8 rc = 1;
5022         char *desc = NULL;
5023
5024         switch (access_status) {
5025         case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5026         case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5027                 rc = 0;
5028                 break;
5029         case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5030                 desc = "sata capability failed";
5031                 break;
5032         case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5033                 desc = "sata affiliation conflict";
5034                 break;
5035         case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5036                 desc = "route not addressable";
5037                 break;
5038         case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5039                 desc = "smp error not addressable";
5040                 break;
5041         case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5042                 desc = "device blocked";
5043                 break;
5044         case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5045         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5046         case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5047         case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5048         case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5049         case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5050         case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5051         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5052         case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5053         case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5054         case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5055         case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5056                 desc = "sata initialization failed";
5057                 break;
5058         default:
5059                 desc = "unknown";
5060                 break;
5061         }
5062
5063         if (!rc)
5064                 return 0;
5065
5066         pr_err(MPT3SAS_FMT
5067                 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5068                 ioc->name, desc, (unsigned long long)sas_address, handle);
5069         return rc;
5070 }
5071
5072 /**
5073  * _scsih_check_device - checking device responsiveness
5074  * @ioc: per adapter object
5075  * @parent_sas_address: sas address of parent expander or sas host
5076  * @handle: attached device handle
5077  * @phy_numberv: phy number
5078  * @link_rate: new link rate
5079  *
5080  * Returns nothing.
5081  */
5082 static void
5083 _scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5084         u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5085 {
5086         Mpi2ConfigReply_t mpi_reply;
5087         Mpi2SasDevicePage0_t sas_device_pg0;
5088         struct _sas_device *sas_device;
5089         u32 ioc_status;
5090         unsigned long flags;
5091         u64 sas_address;
5092         struct scsi_target *starget;
5093         struct MPT3SAS_TARGET *sas_target_priv_data;
5094         u32 device_info;
5095
5096
5097         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5098             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5099                 return;
5100
5101         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5102         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5103                 return;
5104
5105         /* wide port handling ~ we need only handle device once for the phy that
5106          * is matched in sas device page zero
5107          */
5108         if (phy_number != sas_device_pg0.PhyNum)
5109                 return;
5110
5111         /* check if this is end device */
5112         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5113         if (!(_scsih_is_end_device(device_info)))
5114                 return;
5115
5116         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5117         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5118         sas_device = __mpt3sas_get_sdev_by_addr(ioc,
5119             sas_address);
5120
5121         if (!sas_device)
5122                 goto out_unlock;
5123
5124         if (unlikely(sas_device->handle != handle)) {
5125                 starget = sas_device->starget;
5126                 sas_target_priv_data = starget->hostdata;
5127                 starget_printk(KERN_INFO, starget,
5128                         "handle changed from(0x%04x) to (0x%04x)!!!\n",
5129                         sas_device->handle, handle);
5130                 sas_target_priv_data->handle = handle;
5131                 sas_device->handle = handle;
5132                 if (sas_device_pg0.Flags &
5133                      MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5134                         sas_device->enclosure_level =
5135                                 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5136                         memcpy(&sas_device->connector_name[0],
5137                                 &sas_device_pg0.ConnectorName[0], 4);
5138                 } else {
5139                         sas_device->enclosure_level = 0;
5140                         sas_device->connector_name[0] = '\0';
5141                 }
5142         }
5143
5144         /* check if device is present */
5145         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5146             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5147                 pr_err(MPT3SAS_FMT
5148                         "device is not present handle(0x%04x), flags!!!\n",
5149                         ioc->name, handle);
5150                 goto out_unlock;
5151         }
5152
5153         /* check if there were any issues with discovery */
5154         if (_scsih_check_access_status(ioc, sas_address, handle,
5155             sas_device_pg0.AccessStatus))
5156                 goto out_unlock;
5157
5158         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5159         _scsih_ublock_io_device(ioc, sas_address);
5160         if (sas_device)
5161                 sas_device_put(sas_device);
5162         return;
5163
5164 out_unlock:
5165         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5166         if (sas_device)
5167                 sas_device_put(sas_device);
5168 }
5169
5170 /**
5171  * _scsih_add_device -  creating sas device object
5172  * @ioc: per adapter object
5173  * @handle: sas device handle
5174  * @phy_num: phy number end device attached to
5175  * @is_pd: is this hidden raid component
5176  *
5177  * Creating end device object, stored in ioc->sas_device_list.
5178  *
5179  * Returns 0 for success, non-zero for failure.
5180  */
5181 static int
5182 _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5183         u8 is_pd)
5184 {
5185         Mpi2ConfigReply_t mpi_reply;
5186         Mpi2SasDevicePage0_t sas_device_pg0;
5187         Mpi2SasEnclosurePage0_t enclosure_pg0;
5188         struct _sas_device *sas_device;
5189         u32 ioc_status;
5190         u64 sas_address;
5191         u32 device_info;
5192
5193         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5194             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5195                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5196                     ioc->name, __FILE__, __LINE__, __func__);
5197                 return -1;
5198         }
5199
5200         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5201             MPI2_IOCSTATUS_MASK;
5202         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5203                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5204                     ioc->name, __FILE__, __LINE__, __func__);
5205                 return -1;
5206         }
5207
5208         /* check if this is end device */
5209         device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5210         if (!(_scsih_is_end_device(device_info)))
5211                 return -1;
5212         sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5213
5214         /* check if device is present */
5215         if (!(le16_to_cpu(sas_device_pg0.Flags) &
5216             MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5217                 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5218                         ioc->name, handle);
5219                 return -1;
5220         }
5221
5222         /* check if there were any issues with discovery */
5223         if (_scsih_check_access_status(ioc, sas_address, handle,
5224             sas_device_pg0.AccessStatus))
5225                 return -1;
5226
5227         sas_device = mpt3sas_get_sdev_by_addr(ioc, sas_address);
5228         if (sas_device) {
5229                 sas_device_put(sas_device);
5230                 return -1;
5231         }
5232
5233         sas_device = kzalloc(sizeof(struct _sas_device),
5234             GFP_KERNEL);
5235         if (!sas_device) {
5236                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5237                     ioc->name, __FILE__, __LINE__, __func__);
5238                 return 0;
5239         }
5240
5241         kref_init(&sas_device->refcount);
5242         sas_device->handle = handle;
5243         if (_scsih_get_sas_address(ioc,
5244             le16_to_cpu(sas_device_pg0.ParentDevHandle),
5245             &sas_device->sas_address_parent) != 0)
5246                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5247                     ioc->name, __FILE__, __LINE__, __func__);
5248         sas_device->enclosure_handle =
5249             le16_to_cpu(sas_device_pg0.EnclosureHandle);
5250         if (sas_device->enclosure_handle != 0)
5251                 sas_device->slot =
5252                     le16_to_cpu(sas_device_pg0.Slot);
5253         sas_device->device_info = device_info;
5254         sas_device->sas_address = sas_address;
5255         sas_device->phy = sas_device_pg0.PhyNum;
5256         sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5257             MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5258
5259         if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5260                 sas_device->enclosure_level =
5261                         le16_to_cpu(sas_device_pg0.EnclosureLevel);
5262                 memcpy(&sas_device->connector_name[0],
5263                         &sas_device_pg0.ConnectorName[0], 4);
5264         } else {
5265                 sas_device->enclosure_level = 0;
5266                 sas_device->connector_name[0] = '\0';
5267         }
5268         /* get enclosure_logical_id */
5269         if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5270            ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5271            sas_device->enclosure_handle)))
5272                 sas_device->enclosure_logical_id =
5273                     le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5274
5275         /* get device name */
5276         sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5277
5278         if (ioc->wait_for_discovery_to_complete)
5279                 _scsih_sas_device_init_add(ioc, sas_device);
5280         else
5281                 _scsih_sas_device_add(ioc, sas_device);
5282
5283         sas_device_put(sas_device);
5284         return 0;
5285 }
5286
5287 /**
5288  * _scsih_remove_device -  removing sas device object
5289  * @ioc: per adapter object
5290  * @sas_device_delete: the sas_device object
5291  *
5292  * Return nothing.
5293  */
5294 static void
5295 _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5296         struct _sas_device *sas_device)
5297 {
5298         struct MPT3SAS_TARGET *sas_target_priv_data;
5299
5300         if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5301              (sas_device->pfa_led_on)) {
5302                 _scsih_turn_off_pfa_led(ioc, sas_device);
5303                 sas_device->pfa_led_on = 0;
5304         }
5305         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5306                 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5307                 ioc->name, __func__,
5308             sas_device->handle, (unsigned long long)
5309             sas_device->sas_address));
5310         if (sas_device->enclosure_handle != 0)
5311                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5312                     "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5313                     ioc->name, __func__,
5314                     (unsigned long long)sas_device->enclosure_logical_id,
5315                     sas_device->slot));
5316         if (sas_device->connector_name[0] != '\0')
5317                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5318                   "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5319                   ioc->name, __func__,
5320                   sas_device->enclosure_level,
5321                   sas_device->connector_name));
5322
5323         if (sas_device->starget && sas_device->starget->hostdata) {
5324                 sas_target_priv_data = sas_device->starget->hostdata;
5325                 sas_target_priv_data->deleted = 1;
5326                 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5327                 sas_target_priv_data->handle =
5328                      MPT3SAS_INVALID_DEVICE_HANDLE;
5329         }
5330         mpt3sas_transport_port_remove(ioc,
5331                     sas_device->sas_address,
5332                     sas_device->sas_address_parent);
5333
5334         pr_info(MPT3SAS_FMT
5335                 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5336                 ioc->name, sas_device->handle,
5337             (unsigned long long) sas_device->sas_address);
5338         if (sas_device->enclosure_handle != 0)
5339                 pr_info(MPT3SAS_FMT
5340                   "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5341                   ioc->name,
5342                   (unsigned long long)sas_device->enclosure_logical_id,
5343                   sas_device->slot);
5344         if (sas_device->connector_name[0] != '\0')
5345                 pr_info(MPT3SAS_FMT
5346                   "removing enclosure level(0x%04x), connector name( %s)\n",
5347                   ioc->name, sas_device->enclosure_level,
5348                   sas_device->connector_name);
5349
5350         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5351                 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5352                 ioc->name, __func__,
5353                 sas_device->handle, (unsigned long long)
5354                 sas_device->sas_address));
5355         if (sas_device->enclosure_handle != 0)
5356                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5357                     "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5358                     ioc->name, __func__,
5359                     (unsigned long long)sas_device->enclosure_logical_id,
5360                     sas_device->slot));
5361         if (sas_device->connector_name[0] != '\0')
5362                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5363                     "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5364                     ioc->name, __func__, sas_device->enclosure_level,
5365                     sas_device->connector_name));
5366
5367         kfree(sas_device);
5368 }
5369
5370 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5371 /**
5372  * _scsih_sas_topology_change_event_debug - debug for topology event
5373  * @ioc: per adapter object
5374  * @event_data: event data payload
5375  * Context: user.
5376  */
5377 static void
5378 _scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5379         Mpi2EventDataSasTopologyChangeList_t *event_data)
5380 {
5381         int i;
5382         u16 handle;
5383         u16 reason_code;
5384         u8 phy_number;
5385         char *status_str = NULL;
5386         u8 link_rate, prev_link_rate;
5387
5388         switch (event_data->ExpStatus) {
5389         case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5390                 status_str = "add";
5391                 break;
5392         case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5393                 status_str = "remove";
5394                 break;
5395         case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5396         case 0:
5397                 status_str =  "responding";
5398                 break;
5399         case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5400                 status_str = "remove delay";
5401                 break;
5402         default:
5403                 status_str = "unknown status";
5404                 break;
5405         }
5406         pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5407             ioc->name, status_str);
5408         pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5409             "start_phy(%02d), count(%d)\n",
5410             le16_to_cpu(event_data->ExpanderDevHandle),
5411             le16_to_cpu(event_data->EnclosureHandle),
5412             event_data->StartPhyNum, event_data->NumEntries);
5413         for (i = 0; i < event_data->NumEntries; i++) {
5414                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5415                 if (!handle)
5416                         continue;
5417                 phy_number = event_data->StartPhyNum + i;
5418                 reason_code = event_data->PHY[i].PhyStatus &
5419                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5420                 switch (reason_code) {
5421                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5422                         status_str = "target add";
5423                         break;
5424                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5425                         status_str = "target remove";
5426                         break;
5427                 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5428                         status_str = "delay target remove";
5429                         break;
5430                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5431                         status_str = "link rate change";
5432                         break;
5433                 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5434                         status_str = "target responding";
5435                         break;
5436                 default:
5437                         status_str = "unknown";
5438                         break;
5439                 }
5440                 link_rate = event_data->PHY[i].LinkRate >> 4;
5441                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5442                 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5443                     " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5444                     handle, status_str, link_rate, prev_link_rate);
5445
5446         }
5447 }
5448 #endif
5449
5450 /**
5451  * _scsih_sas_topology_change_event - handle topology changes
5452  * @ioc: per adapter object
5453  * @fw_event: The fw_event_work object
5454  * Context: user.
5455  *
5456  */
5457 static int
5458 _scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5459         struct fw_event_work *fw_event)
5460 {
5461         int i;
5462         u16 parent_handle, handle;
5463         u16 reason_code;
5464         u8 phy_number, max_phys;
5465         struct _sas_node *sas_expander;
5466         u64 sas_address;
5467         unsigned long flags;
5468         u8 link_rate, prev_link_rate;
5469         Mpi2EventDataSasTopologyChangeList_t *event_data =
5470                 (Mpi2EventDataSasTopologyChangeList_t *)
5471                 fw_event->event_data;
5472
5473 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5474         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5475                 _scsih_sas_topology_change_event_debug(ioc, event_data);
5476 #endif
5477
5478         if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5479                 return 0;
5480
5481         if (!ioc->sas_hba.num_phys)
5482                 _scsih_sas_host_add(ioc);
5483         else
5484                 _scsih_sas_host_refresh(ioc);
5485
5486         if (fw_event->ignore) {
5487                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5488                         "ignoring expander event\n", ioc->name));
5489                 return 0;
5490         }
5491
5492         parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5493
5494         /* handle expander add */
5495         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5496                 if (_scsih_expander_add(ioc, parent_handle) != 0)
5497                         return 0;
5498
5499         spin_lock_irqsave(&ioc->sas_node_lock, flags);
5500         sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5501             parent_handle);
5502         if (sas_expander) {
5503                 sas_address = sas_expander->sas_address;
5504                 max_phys = sas_expander->num_phys;
5505         } else if (parent_handle < ioc->sas_hba.num_phys) {
5506                 sas_address = ioc->sas_hba.sas_address;
5507                 max_phys = ioc->sas_hba.num_phys;
5508         } else {
5509                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5510                 return 0;
5511         }
5512         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5513
5514         /* handle siblings events */
5515         for (i = 0; i < event_data->NumEntries; i++) {
5516                 if (fw_event->ignore) {
5517                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5518                                 "ignoring expander event\n", ioc->name));
5519                         return 0;
5520                 }
5521                 if (ioc->remove_host || ioc->pci_error_recovery)
5522                         return 0;
5523                 phy_number = event_data->StartPhyNum + i;
5524                 if (phy_number >= max_phys)
5525                         continue;
5526                 reason_code = event_data->PHY[i].PhyStatus &
5527                     MPI2_EVENT_SAS_TOPO_RC_MASK;
5528                 if ((event_data->PHY[i].PhyStatus &
5529                     MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5530                     MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5531                                 continue;
5532                 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5533                 if (!handle)
5534                         continue;
5535                 link_rate = event_data->PHY[i].LinkRate >> 4;
5536                 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5537                 switch (reason_code) {
5538                 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5539
5540                         if (ioc->shost_recovery)
5541                                 break;
5542
5543                         if (link_rate == prev_link_rate)
5544                                 break;
5545
5546                         mpt3sas_transport_update_links(ioc, sas_address,
5547                             handle, phy_number, link_rate);
5548
5549                         if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5550                                 break;
5551
5552                         _scsih_check_device(ioc, sas_address, handle,
5553                             phy_number, link_rate);
5554
5555
5556                 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5557
5558                         if (ioc->shost_recovery)
5559                                 break;
5560
5561                         mpt3sas_transport_update_links(ioc, sas_address,
5562                             handle, phy_number, link_rate);
5563
5564                         _scsih_add_device(ioc, handle, phy_number, 0);
5565
5566                         break;
5567                 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5568
5569                         _scsih_device_remove_by_handle(ioc, handle);
5570                         break;
5571                 }
5572         }
5573
5574         /* handle expander removal */
5575         if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5576             sas_expander)
5577                 mpt3sas_expander_remove(ioc, sas_address);
5578
5579         return 0;
5580 }
5581
5582 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5583 /**
5584  * _scsih_sas_device_status_change_event_debug - debug for device event
5585  * @event_data: event data payload
5586  * Context: user.
5587  *
5588  * Return nothing.
5589  */
5590 static void
5591 _scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5592         Mpi2EventDataSasDeviceStatusChange_t *event_data)
5593 {
5594         char *reason_str = NULL;
5595
5596         switch (event_data->ReasonCode) {
5597         case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5598                 reason_str = "smart data";
5599                 break;
5600         case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5601                 reason_str = "unsupported device discovered";
5602                 break;
5603         case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5604                 reason_str = "internal device reset";
5605                 break;
5606         case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5607                 reason_str = "internal task abort";
5608                 break;
5609         case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5610                 reason_str = "internal task abort set";
5611                 break;
5612         case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5613                 reason_str = "internal clear task set";
5614                 break;
5615         case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5616                 reason_str = "internal query task";
5617                 break;
5618         case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5619                 reason_str = "sata init failure";
5620                 break;
5621         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5622                 reason_str = "internal device reset complete";
5623                 break;
5624         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5625                 reason_str = "internal task abort complete";
5626                 break;
5627         case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5628                 reason_str = "internal async notification";
5629                 break;
5630         case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5631                 reason_str = "expander reduced functionality";
5632                 break;
5633         case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5634                 reason_str = "expander reduced functionality complete";
5635                 break;
5636         default:
5637                 reason_str = "unknown reason";
5638                 break;
5639         }
5640         pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5641             "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5642             ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5643             (unsigned long long)le64_to_cpu(event_data->SASAddress),
5644             le16_to_cpu(event_data->TaskTag));
5645         if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5646                 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5647                     event_data->ASC, event_data->ASCQ);
5648         pr_info("\n");
5649 }
5650 #endif
5651
5652 /**
5653  * _scsih_sas_device_status_change_event - handle device status change
5654  * @ioc: per adapter object
5655  * @fw_event: The fw_event_work object
5656  * Context: user.
5657  *
5658  * Return nothing.
5659  */
5660 static void
5661 _scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5662         struct fw_event_work *fw_event)
5663 {
5664         struct MPT3SAS_TARGET *target_priv_data;
5665         struct _sas_device *sas_device;
5666         u64 sas_address;
5667         unsigned long flags;
5668         Mpi2EventDataSasDeviceStatusChange_t *event_data =
5669                 (Mpi2EventDataSasDeviceStatusChange_t *)
5670                 fw_event->event_data;
5671
5672 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5673         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5674                 _scsih_sas_device_status_change_event_debug(ioc,
5675                      event_data);
5676 #endif
5677
5678         /* In MPI Revision K (0xC), the internal device reset complete was
5679          * implemented, so avoid setting tm_busy flag for older firmware.
5680          */
5681         if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5682                 return;
5683
5684         if (event_data->ReasonCode !=
5685             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5686            event_data->ReasonCode !=
5687             MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5688                 return;
5689
5690         spin_lock_irqsave(&ioc->sas_device_lock, flags);
5691         sas_address = le64_to_cpu(event_data->SASAddress);
5692         sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
5693
5694         if (!sas_device || !sas_device->starget)
5695                 goto out;
5696
5697         target_priv_data = sas_device->starget->hostdata;
5698         if (!target_priv_data)
5699                 goto out;
5700
5701         if (event_data->ReasonCode ==
5702             MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5703                 target_priv_data->tm_busy = 1;
5704         else
5705                 target_priv_data->tm_busy = 0;
5706
5707 out:
5708         if (sas_device)
5709                 sas_device_put(sas_device);
5710
5711         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5712 }
5713
5714 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5715 /**
5716  * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5717  * event
5718  * @ioc: per adapter object
5719  * @event_data: event data payload
5720  * Context: user.
5721  *
5722  * Return nothing.
5723  */
5724 static void
5725 _scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5726         Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5727 {
5728         char *reason_str = NULL;
5729
5730         switch (event_data->ReasonCode) {
5731         case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5732                 reason_str = "enclosure add";
5733                 break;
5734         case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5735                 reason_str = "enclosure remove";
5736                 break;
5737         default:
5738                 reason_str = "unknown reason";
5739                 break;
5740         }
5741
5742         pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5743             "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5744             " number slots(%d)\n", ioc->name, reason_str,
5745             le16_to_cpu(event_data->EnclosureHandle),
5746             (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5747             le16_to_cpu(event_data->StartSlot));
5748 }
5749 #endif
5750
5751 /**
5752  * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5753  * @ioc: per adapter object
5754  * @fw_event: The fw_event_work object
5755  * Context: user.
5756  *
5757  * Return nothing.
5758  */
5759 static void
5760 _scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5761         struct fw_event_work *fw_event)
5762 {
5763 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5764         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5765                 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
5766                      (Mpi2EventDataSasEnclDevStatusChange_t *)
5767                      fw_event->event_data);
5768 #endif
5769 }
5770
5771 /**
5772  * _scsih_sas_broadcast_primitive_event - handle broadcast events
5773  * @ioc: per adapter object
5774  * @fw_event: The fw_event_work object
5775  * Context: user.
5776  *
5777  * Return nothing.
5778  */
5779 static void
5780 _scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
5781         struct fw_event_work *fw_event)
5782 {
5783         struct scsi_cmnd *scmd;
5784         struct scsi_device *sdev;
5785         u16 smid, handle;
5786         u32 lun;
5787         struct MPT3SAS_DEVICE *sas_device_priv_data;
5788         u32 termination_count;
5789         u32 query_count;
5790         Mpi2SCSITaskManagementReply_t *mpi_reply;
5791         Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5792                 (Mpi2EventDataSasBroadcastPrimitive_t *)
5793                 fw_event->event_data;
5794         u16 ioc_status;
5795         unsigned long flags;
5796         int r;
5797         u8 max_retries = 0;
5798         u8 task_abort_retries;
5799
5800         mutex_lock(&ioc->tm_cmds.mutex);
5801         pr_info(MPT3SAS_FMT
5802                 "%s: enter: phy number(%d), width(%d)\n",
5803                 ioc->name, __func__, event_data->PhyNum,
5804              event_data->PortWidth);
5805
5806         _scsih_block_io_all_device(ioc);
5807
5808         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5809         mpi_reply = ioc->tm_cmds.reply;
5810  broadcast_aen_retry:
5811
5812         /* sanity checks for retrying this loop */
5813         if (max_retries++ == 5) {
5814                 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
5815                     ioc->name, __func__));
5816                 goto out;
5817         } else if (max_retries > 1)
5818                 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
5819                     ioc->name, __func__, max_retries - 1));
5820
5821         termination_count = 0;
5822         query_count = 0;
5823         for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5824                 if (ioc->shost_recovery)
5825                         goto out;
5826                 scmd = _scsih_scsi_lookup_get(ioc, smid);
5827                 if (!scmd)
5828                         continue;
5829                 sdev = scmd->device;
5830                 sas_device_priv_data = sdev->hostdata;
5831                 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5832                         continue;
5833                  /* skip hidden raid components */
5834                 if (sas_device_priv_data->sas_target->flags &
5835                     MPT_TARGET_FLAGS_RAID_COMPONENT)
5836                         continue;
5837                  /* skip volumes */
5838                 if (sas_device_priv_data->sas_target->flags &
5839                     MPT_TARGET_FLAGS_VOLUME)
5840                         continue;
5841
5842                 handle = sas_device_priv_data->sas_target->handle;
5843                 lun = sas_device_priv_data->lun;
5844                 query_count++;
5845
5846                 if (ioc->shost_recovery)
5847                         goto out;
5848
5849                 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5850                 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5851                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
5852                     TM_MUTEX_OFF);
5853                 if (r == FAILED) {
5854                         sdev_printk(KERN_WARNING, sdev,
5855                             "mpt3sas_scsih_issue_tm: FAILED when sending "
5856                             "QUERY_TASK: scmd(%p)\n", scmd);
5857                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5858                         goto broadcast_aen_retry;
5859                 }
5860                 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5861                     & MPI2_IOCSTATUS_MASK;
5862                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5863                         sdev_printk(KERN_WARNING, sdev,
5864                                 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
5865                                 ioc_status, scmd);
5866                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5867                         goto broadcast_aen_retry;
5868                 }
5869
5870                 /* see if IO is still owned by IOC and target */
5871                 if (mpi_reply->ResponseCode ==
5872                      MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5873                      mpi_reply->ResponseCode ==
5874                      MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
5875                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5876                         continue;
5877                 }
5878                 task_abort_retries = 0;
5879  tm_retry:
5880                 if (task_abort_retries++ == 60) {
5881                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5882                             "%s: ABORT_TASK: giving up\n", ioc->name,
5883                             __func__));
5884                         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5885                         goto broadcast_aen_retry;
5886                 }
5887
5888                 if (ioc->shost_recovery)
5889                         goto out_no_lock;
5890
5891                 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5892                     sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5893                     TM_MUTEX_OFF);
5894                 if (r == FAILED) {
5895                         sdev_printk(KERN_WARNING, sdev,
5896                             "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
5897                             "scmd(%p)\n", scmd);
5898                         goto tm_retry;
5899                 }
5900
5901                 if (task_abort_retries > 1)
5902                         sdev_printk(KERN_WARNING, sdev,
5903                             "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5904                             " scmd(%p)\n",
5905                             task_abort_retries - 1, scmd);
5906
5907                 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5908                 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5909         }
5910
5911         if (ioc->broadcast_aen_pending) {
5912                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5913                         "%s: loop back due to pending AEN\n",
5914                         ioc->name, __func__));
5915                  ioc->broadcast_aen_pending = 0;
5916                  goto broadcast_aen_retry;
5917         }
5918
5919  out:
5920         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5921  out_no_lock:
5922
5923         dewtprintk(ioc, pr_info(MPT3SAS_FMT
5924             "%s - exit, query_count = %d termination_count = %d\n",
5925             ioc->name, __func__, query_count, termination_count));
5926
5927         ioc->broadcast_aen_busy = 0;
5928         if (!ioc->shost_recovery)
5929                 _scsih_ublock_io_all_device(ioc);
5930         mutex_unlock(&ioc->tm_cmds.mutex);
5931 }
5932
5933 /**
5934  * _scsih_sas_discovery_event - handle discovery events
5935  * @ioc: per adapter object
5936  * @fw_event: The fw_event_work object
5937  * Context: user.
5938  *
5939  * Return nothing.
5940  */
5941 static void
5942 _scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
5943         struct fw_event_work *fw_event)
5944 {
5945         Mpi2EventDataSasDiscovery_t *event_data =
5946                 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
5947
5948 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5949         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5950                 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
5951                     (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5952                     "start" : "stop");
5953         if (event_data->DiscoveryStatus)
5954                 pr_info("discovery_status(0x%08x)",
5955                     le32_to_cpu(event_data->DiscoveryStatus));
5956         pr_info("\n");
5957         }
5958 #endif
5959
5960         if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5961             !ioc->sas_hba.num_phys) {
5962                 if (disable_discovery > 0 && ioc->shost_recovery) {
5963                         /* Wait for the reset to complete */
5964                         while (ioc->shost_recovery)
5965                                 ssleep(1);
5966                 }
5967                 _scsih_sas_host_add(ioc);
5968         }
5969 }
5970
5971 /**
5972  * _scsih_ir_fastpath - turn on fastpath for IR physdisk
5973  * @ioc: per adapter object
5974  * @handle: device handle for physical disk
5975  * @phys_disk_num: physical disk number
5976  *
5977  * Return 0 for success, else failure.
5978  */
5979 static int
5980 _scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
5981 {
5982         Mpi2RaidActionRequest_t *mpi_request;
5983         Mpi2RaidActionReply_t *mpi_reply;
5984         u16 smid;
5985         u8 issue_reset = 0;
5986         int rc = 0;
5987         u16 ioc_status;
5988         u32 log_info;
5989
5990
5991         mutex_lock(&ioc->scsih_cmds.mutex);
5992
5993         if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
5994                 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
5995                     ioc->name, __func__);
5996                 rc = -EAGAIN;
5997                 goto out;
5998         }
5999         ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6000
6001         smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6002         if (!smid) {
6003                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6004                     ioc->name, __func__);
6005                 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6006                 rc = -EAGAIN;
6007                 goto out;
6008         }
6009
6010         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6011         ioc->scsih_cmds.smid = smid;
6012         memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6013
6014         mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6015         mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6016         mpi_request->PhysDiskNum = phys_disk_num;
6017
6018         dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6019             "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6020             handle, phys_disk_num));
6021
6022         init_completion(&ioc->scsih_cmds.done);
6023         mpt3sas_base_put_smid_default(ioc, smid);
6024         wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6025
6026         if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6027                 pr_err(MPT3SAS_FMT "%s: timeout\n",
6028                     ioc->name, __func__);
6029                 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6030                         issue_reset = 1;
6031                 rc = -EFAULT;
6032                 goto out;
6033         }
6034
6035         if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6036
6037                 mpi_reply = ioc->scsih_cmds.reply;
6038                 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6039                 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6040                         log_info =  le32_to_cpu(mpi_reply->IOCLogInfo);
6041                 else
6042                         log_info = 0;
6043                 ioc_status &= MPI2_IOCSTATUS_MASK;
6044                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6045                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6046                             "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6047                             "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6048                             log_info));
6049                         rc = -EFAULT;
6050                 } else
6051                         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6052                             "IR RAID_ACTION: completed successfully\n",
6053                             ioc->name));
6054         }
6055
6056  out:
6057         ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6058         mutex_unlock(&ioc->scsih_cmds.mutex);
6059
6060         if (issue_reset)
6061                 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
6062                     FORCE_BIG_HAMMER);
6063         return rc;
6064 }
6065
6066 /**
6067  * _scsih_reprobe_lun - reprobing lun
6068  * @sdev: scsi device struct
6069  * @no_uld_attach: sdev->no_uld_attach flag setting
6070  *
6071  **/
6072 static void
6073 _scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6074 {
6075         int rc;
6076         sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6077         sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6078             sdev->no_uld_attach ? "hidding" : "exposing");
6079         rc = scsi_device_reprobe(sdev);
6080 }
6081
6082 /**
6083  * _scsih_sas_volume_add - add new volume
6084  * @ioc: per adapter object
6085  * @element: IR config element data
6086  * Context: user.
6087  *
6088  * Return nothing.
6089  */
6090 static void
6091 _scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6092         Mpi2EventIrConfigElement_t *element)
6093 {
6094         struct _raid_device *raid_device;
6095         unsigned long flags;
6096         u64 wwid;
6097         u16 handle = le16_to_cpu(element->VolDevHandle);
6098         int rc;
6099
6100         mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6101         if (!wwid) {
6102                 pr_err(MPT3SAS_FMT
6103                     "failure at %s:%d/%s()!\n", ioc->name,
6104                     __FILE__, __LINE__, __func__);
6105                 return;
6106         }
6107
6108         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6109         raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6110         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6111
6112         if (raid_device)
6113                 return;
6114
6115         raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6116         if (!raid_device) {
6117                 pr_err(MPT3SAS_FMT
6118                     "failure at %s:%d/%s()!\n", ioc->name,
6119                     __FILE__, __LINE__, __func__);
6120                 return;
6121         }
6122
6123         raid_device->id = ioc->sas_id++;
6124         raid_device->channel = RAID_CHANNEL;
6125         raid_device->handle = handle;
6126         raid_device->wwid = wwid;
6127         _scsih_raid_device_add(ioc, raid_device);
6128         if (!ioc->wait_for_discovery_to_complete) {
6129                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6130                     raid_device->id, 0);
6131                 if (rc)
6132                         _scsih_raid_device_remove(ioc, raid_device);
6133         } else {
6134                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6135                 _scsih_determine_boot_device(ioc, raid_device, 1);
6136                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6137         }
6138 }
6139
6140 /**
6141  * _scsih_sas_volume_delete - delete volume
6142  * @ioc: per adapter object
6143  * @handle: volume device handle
6144  * Context: user.
6145  *
6146  * Return nothing.
6147  */
6148 static void
6149 _scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6150 {
6151         struct _raid_device *raid_device;
6152         unsigned long flags;
6153         struct MPT3SAS_TARGET *sas_target_priv_data;
6154         struct scsi_target *starget = NULL;
6155
6156         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6157         raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6158         if (raid_device) {
6159                 if (raid_device->starget) {
6160                         starget = raid_device->starget;
6161                         sas_target_priv_data = starget->hostdata;
6162                         sas_target_priv_data->deleted = 1;
6163                 }
6164                 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6165                         ioc->name,  raid_device->handle,
6166                     (unsigned long long) raid_device->wwid);
6167                 list_del(&raid_device->list);
6168                 kfree(raid_device);
6169         }
6170         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6171         if (starget)
6172                 scsi_remove_target(&starget->dev);
6173 }
6174
6175 /**
6176  * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6177  * @ioc: per adapter object
6178  * @element: IR config element data
6179  * Context: user.
6180  *
6181  * Return nothing.
6182  */
6183 static void
6184 _scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6185         Mpi2EventIrConfigElement_t *element)
6186 {
6187         struct _sas_device *sas_device;
6188         struct scsi_target *starget = NULL;
6189         struct MPT3SAS_TARGET *sas_target_priv_data;
6190         unsigned long flags;
6191         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6192
6193         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6194         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6195         if (sas_device) {
6196                 sas_device->volume_handle = 0;
6197                 sas_device->volume_wwid = 0;
6198                 clear_bit(handle, ioc->pd_handles);
6199                 if (sas_device->starget && sas_device->starget->hostdata) {
6200                         starget = sas_device->starget;
6201                         sas_target_priv_data = starget->hostdata;
6202                         sas_target_priv_data->flags &=
6203                             ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6204                 }
6205         }
6206         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6207         if (!sas_device)
6208                 return;
6209
6210         /* exposing raid component */
6211         if (starget)
6212                 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
6213
6214         sas_device_put(sas_device);
6215 }
6216
6217 /**
6218  * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6219  * @ioc: per adapter object
6220  * @element: IR config element data
6221  * Context: user.
6222  *
6223  * Return nothing.
6224  */
6225 static void
6226 _scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6227         Mpi2EventIrConfigElement_t *element)
6228 {
6229         struct _sas_device *sas_device;
6230         struct scsi_target *starget = NULL;
6231         struct MPT3SAS_TARGET *sas_target_priv_data;
6232         unsigned long flags;
6233         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6234         u16 volume_handle = 0;
6235         u64 volume_wwid = 0;
6236
6237         mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6238         if (volume_handle)
6239                 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6240                     &volume_wwid);
6241
6242         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6243         sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
6244         if (sas_device) {
6245                 set_bit(handle, ioc->pd_handles);
6246                 if (sas_device->starget && sas_device->starget->hostdata) {
6247                         starget = sas_device->starget;
6248                         sas_target_priv_data = starget->hostdata;
6249                         sas_target_priv_data->flags |=
6250                             MPT_TARGET_FLAGS_RAID_COMPONENT;
6251                         sas_device->volume_handle = volume_handle;
6252                         sas_device->volume_wwid = volume_wwid;
6253                 }
6254         }
6255         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6256         if (!sas_device)
6257                 return;
6258
6259         /* hiding raid component */
6260         _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6261         if (starget)
6262                 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6263
6264         sas_device_put(sas_device);
6265 }
6266
6267 /**
6268  * _scsih_sas_pd_delete - delete pd component
6269  * @ioc: per adapter object
6270  * @element: IR config element data
6271  * Context: user.
6272  *
6273  * Return nothing.
6274  */
6275 static void
6276 _scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6277         Mpi2EventIrConfigElement_t *element)
6278 {
6279         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6280
6281         _scsih_device_remove_by_handle(ioc, handle);
6282 }
6283
6284 /**
6285  * _scsih_sas_pd_add - remove pd component
6286  * @ioc: per adapter object
6287  * @element: IR config element data
6288  * Context: user.
6289  *
6290  * Return nothing.
6291  */
6292 static void
6293 _scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6294         Mpi2EventIrConfigElement_t *element)
6295 {
6296         struct _sas_device *sas_device;
6297         u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6298         Mpi2ConfigReply_t mpi_reply;
6299         Mpi2SasDevicePage0_t sas_device_pg0;
6300         u32 ioc_status;
6301         u64 sas_address;
6302         u16 parent_handle;
6303
6304         set_bit(handle, ioc->pd_handles);
6305
6306         sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6307         if (sas_device) {
6308                 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6309                 sas_device_put(sas_device);
6310                 return;
6311         }
6312
6313         if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6314             MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6315                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6316                     ioc->name, __FILE__, __LINE__, __func__);
6317                 return;
6318         }
6319
6320         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6321             MPI2_IOCSTATUS_MASK;
6322         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6323                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6324                     ioc->name, __FILE__, __LINE__, __func__);
6325                 return;
6326         }
6327
6328         parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6329         if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6330                 mpt3sas_transport_update_links(ioc, sas_address, handle,
6331                     sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6332
6333         _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6334         _scsih_add_device(ioc, handle, 0, 1);
6335 }
6336
6337 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6338 /**
6339  * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6340  * @ioc: per adapter object
6341  * @event_data: event data payload
6342  * Context: user.
6343  *
6344  * Return nothing.
6345  */
6346 static void
6347 _scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6348         Mpi2EventDataIrConfigChangeList_t *event_data)
6349 {
6350         Mpi2EventIrConfigElement_t *element;
6351         u8 element_type;
6352         int i;
6353         char *reason_str = NULL, *element_str = NULL;
6354
6355         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6356
6357         pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6358             ioc->name, (le32_to_cpu(event_data->Flags) &
6359             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6360             "foreign" : "native", event_data->NumElements);
6361         for (i = 0; i < event_data->NumElements; i++, element++) {
6362                 switch (element->ReasonCode) {
6363                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6364                         reason_str = "add";
6365                         break;
6366                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6367                         reason_str = "remove";
6368                         break;
6369                 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6370                         reason_str = "no change";
6371                         break;
6372                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6373                         reason_str = "hide";
6374                         break;
6375                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6376                         reason_str = "unhide";
6377                         break;
6378                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6379                         reason_str = "volume_created";
6380                         break;
6381                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6382                         reason_str = "volume_deleted";
6383                         break;
6384                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6385                         reason_str = "pd_created";
6386                         break;
6387                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6388                         reason_str = "pd_deleted";
6389                         break;
6390                 default:
6391                         reason_str = "unknown reason";
6392                         break;
6393                 }
6394                 element_type = le16_to_cpu(element->ElementFlags) &
6395                     MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6396                 switch (element_type) {
6397                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6398                         element_str = "volume";
6399                         break;
6400                 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6401                         element_str = "phys disk";
6402                         break;
6403                 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6404                         element_str = "hot spare";
6405                         break;
6406                 default:
6407                         element_str = "unknown element";
6408                         break;
6409                 }
6410                 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6411                     "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6412                     reason_str, le16_to_cpu(element->VolDevHandle),
6413                     le16_to_cpu(element->PhysDiskDevHandle),
6414                     element->PhysDiskNum);
6415         }
6416 }
6417 #endif
6418
6419 /**
6420  * _scsih_sas_ir_config_change_event - handle ir configuration change events
6421  * @ioc: per adapter object
6422  * @fw_event: The fw_event_work object
6423  * Context: user.
6424  *
6425  * Return nothing.
6426  */
6427 static void
6428 _scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6429         struct fw_event_work *fw_event)
6430 {
6431         Mpi2EventIrConfigElement_t *element;
6432         int i;
6433         u8 foreign_config;
6434         Mpi2EventDataIrConfigChangeList_t *event_data =
6435                 (Mpi2EventDataIrConfigChangeList_t *)
6436                 fw_event->event_data;
6437
6438 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6439         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6440                 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6441
6442 #endif
6443
6444         foreign_config = (le32_to_cpu(event_data->Flags) &
6445             MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6446
6447         element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6448         if (ioc->shost_recovery) {
6449
6450                 for (i = 0; i < event_data->NumElements; i++, element++) {
6451                         if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6452                                 _scsih_ir_fastpath(ioc,
6453                                         le16_to_cpu(element->PhysDiskDevHandle),
6454                                         element->PhysDiskNum);
6455                 }
6456                 return;
6457         }
6458         for (i = 0; i < event_data->NumElements; i++, element++) {
6459
6460                 switch (element->ReasonCode) {
6461                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6462                 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6463                         if (!foreign_config)
6464                                 _scsih_sas_volume_add(ioc, element);
6465                         break;
6466                 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6467                 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6468                         if (!foreign_config)
6469                                 _scsih_sas_volume_delete(ioc,
6470                                     le16_to_cpu(element->VolDevHandle));
6471                         break;
6472                 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6473                         _scsih_sas_pd_hide(ioc, element);
6474                         break;
6475                 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6476                         _scsih_sas_pd_expose(ioc, element);
6477                         break;
6478                 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6479                         _scsih_sas_pd_add(ioc, element);
6480                         break;
6481                 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6482                         _scsih_sas_pd_delete(ioc, element);
6483                         break;
6484                 }
6485         }
6486 }
6487
6488 /**
6489  * _scsih_sas_ir_volume_event - IR volume event
6490  * @ioc: per adapter object
6491  * @fw_event: The fw_event_work object
6492  * Context: user.
6493  *
6494  * Return nothing.
6495  */
6496 static void
6497 _scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6498         struct fw_event_work *fw_event)
6499 {
6500         u64 wwid;
6501         unsigned long flags;
6502         struct _raid_device *raid_device;
6503         u16 handle;
6504         u32 state;
6505         int rc;
6506         Mpi2EventDataIrVolume_t *event_data =
6507                 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
6508
6509         if (ioc->shost_recovery)
6510                 return;
6511
6512         if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6513                 return;
6514
6515         handle = le16_to_cpu(event_data->VolDevHandle);
6516         state = le32_to_cpu(event_data->NewValue);
6517         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6518                 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6519                 ioc->name, __func__,  handle,
6520             le32_to_cpu(event_data->PreviousValue), state));
6521         switch (state) {
6522         case MPI2_RAID_VOL_STATE_MISSING:
6523         case MPI2_RAID_VOL_STATE_FAILED:
6524                 _scsih_sas_volume_delete(ioc, handle);
6525                 break;
6526
6527         case MPI2_RAID_VOL_STATE_ONLINE:
6528         case MPI2_RAID_VOL_STATE_DEGRADED:
6529         case MPI2_RAID_VOL_STATE_OPTIMAL:
6530
6531                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6532                 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6533                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6534
6535                 if (raid_device)
6536                         break;
6537
6538                 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6539                 if (!wwid) {
6540                         pr_err(MPT3SAS_FMT
6541                             "failure at %s:%d/%s()!\n", ioc->name,
6542                             __FILE__, __LINE__, __func__);
6543                         break;
6544                 }
6545
6546                 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6547                 if (!raid_device) {
6548                         pr_err(MPT3SAS_FMT
6549                             "failure at %s:%d/%s()!\n", ioc->name,
6550                             __FILE__, __LINE__, __func__);
6551                         break;
6552                 }
6553
6554                 raid_device->id = ioc->sas_id++;
6555                 raid_device->channel = RAID_CHANNEL;
6556                 raid_device->handle = handle;
6557                 raid_device->wwid = wwid;
6558                 _scsih_raid_device_add(ioc, raid_device);
6559                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6560                     raid_device->id, 0);
6561                 if (rc)
6562                         _scsih_raid_device_remove(ioc, raid_device);
6563                 break;
6564
6565         case MPI2_RAID_VOL_STATE_INITIALIZING:
6566         default:
6567                 break;
6568         }
6569 }
6570
6571 /**
6572  * _scsih_sas_ir_physical_disk_event - PD event
6573  * @ioc: per adapter object
6574  * @fw_event: The fw_event_work object
6575  * Context: user.
6576  *
6577  * Return nothing.
6578  */
6579 static void
6580 _scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6581         struct fw_event_work *fw_event)
6582 {
6583         u16 handle, parent_handle;
6584         u32 state;
6585         struct _sas_device *sas_device;
6586         Mpi2ConfigReply_t mpi_reply;
6587         Mpi2SasDevicePage0_t sas_device_pg0;
6588         u32 ioc_status;
6589         Mpi2EventDataIrPhysicalDisk_t *event_data =
6590                 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
6591         u64 sas_address;
6592
6593         if (ioc->shost_recovery)
6594                 return;
6595
6596         if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6597                 return;
6598
6599         handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6600         state = le32_to_cpu(event_data->NewValue);
6601
6602         dewtprintk(ioc, pr_info(MPT3SAS_FMT
6603                 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6604                 ioc->name, __func__,  handle,
6605                     le32_to_cpu(event_data->PreviousValue), state));
6606         switch (state) {
6607         case MPI2_RAID_PD_STATE_ONLINE:
6608         case MPI2_RAID_PD_STATE_DEGRADED:
6609         case MPI2_RAID_PD_STATE_REBUILDING:
6610         case MPI2_RAID_PD_STATE_OPTIMAL:
6611         case MPI2_RAID_PD_STATE_HOT_SPARE:
6612
6613                 set_bit(handle, ioc->pd_handles);
6614
6615                 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6616                 if (sas_device) {
6617                         sas_device_put(sas_device);
6618                         return;
6619                 }
6620
6621                 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6622                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6623                     handle))) {
6624                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6625                             ioc->name, __FILE__, __LINE__, __func__);
6626                         return;
6627                 }
6628
6629                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6630                     MPI2_IOCSTATUS_MASK;
6631                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6632                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6633                             ioc->name, __FILE__, __LINE__, __func__);
6634                         return;
6635                 }
6636
6637                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6638                 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6639                         mpt3sas_transport_update_links(ioc, sas_address, handle,
6640                             sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6641
6642                 _scsih_add_device(ioc, handle, 0, 1);
6643
6644                 break;
6645
6646         case MPI2_RAID_PD_STATE_OFFLINE:
6647         case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6648         case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6649         default:
6650                 break;
6651         }
6652 }
6653
6654 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6655 /**
6656  * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6657  * @ioc: per adapter object
6658  * @event_data: event data payload
6659  * Context: user.
6660  *
6661  * Return nothing.
6662  */
6663 static void
6664 _scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6665         Mpi2EventDataIrOperationStatus_t *event_data)
6666 {
6667         char *reason_str = NULL;
6668
6669         switch (event_data->RAIDOperation) {
6670         case MPI2_EVENT_IR_RAIDOP_RESYNC:
6671                 reason_str = "resync";
6672                 break;
6673         case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6674                 reason_str = "online capacity expansion";
6675                 break;
6676         case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6677                 reason_str = "consistency check";
6678                 break;
6679         case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6680                 reason_str = "background init";
6681                 break;
6682         case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6683                 reason_str = "make data consistent";
6684                 break;
6685         }
6686
6687         if (!reason_str)
6688                 return;
6689
6690         pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6691             "\thandle(0x%04x), percent complete(%d)\n",
6692             ioc->name, reason_str,
6693             le16_to_cpu(event_data->VolDevHandle),
6694             event_data->PercentComplete);
6695 }
6696 #endif
6697
6698 /**
6699  * _scsih_sas_ir_operation_status_event - handle RAID operation events
6700  * @ioc: per adapter object
6701  * @fw_event: The fw_event_work object
6702  * Context: user.
6703  *
6704  * Return nothing.
6705  */
6706 static void
6707 _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6708         struct fw_event_work *fw_event)
6709 {
6710         Mpi2EventDataIrOperationStatus_t *event_data =
6711                 (Mpi2EventDataIrOperationStatus_t *)
6712                 fw_event->event_data;
6713         static struct _raid_device *raid_device;
6714         unsigned long flags;
6715         u16 handle;
6716
6717 #ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6718         if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6719                 _scsih_sas_ir_operation_status_event_debug(ioc,
6720                      event_data);
6721 #endif
6722
6723         /* code added for raid transport support */
6724         if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6725
6726                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6727                 handle = le16_to_cpu(event_data->VolDevHandle);
6728                 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6729                 if (raid_device)
6730                         raid_device->percent_complete =
6731                             event_data->PercentComplete;
6732                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6733         }
6734 }
6735
6736 /**
6737  * _scsih_prep_device_scan - initialize parameters prior to device scan
6738  * @ioc: per adapter object
6739  *
6740  * Set the deleted flag prior to device scan.  If the device is found during
6741  * the scan, then we clear the deleted flag.
6742  */
6743 static void
6744 _scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6745 {
6746         struct MPT3SAS_DEVICE *sas_device_priv_data;
6747         struct scsi_device *sdev;
6748
6749         shost_for_each_device(sdev, ioc->shost) {
6750                 sas_device_priv_data = sdev->hostdata;
6751                 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6752                         sas_device_priv_data->sas_target->deleted = 1;
6753         }
6754 }
6755
6756 /**
6757  * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6758  * @ioc: per adapter object
6759  * @sas_device_pg0: SAS Device page 0
6760  *
6761  * After host reset, find out whether devices are still responding.
6762  * Used in _scsih_remove_unresponsive_sas_devices.
6763  *
6764  * Return nothing.
6765  */
6766 static void
6767 _scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6768 Mpi2SasDevicePage0_t *sas_device_pg0)
6769 {
6770         struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6771         struct scsi_target *starget;
6772         struct _sas_device *sas_device;
6773         unsigned long flags;
6774
6775         spin_lock_irqsave(&ioc->sas_device_lock, flags);
6776         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6777                 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
6778                         (sas_device->slot == sas_device_pg0->Slot)) {
6779                         sas_device->responding = 1;
6780                         starget = sas_device->starget;
6781                         if (starget && starget->hostdata) {
6782                                 sas_target_priv_data = starget->hostdata;
6783                                 sas_target_priv_data->tm_busy = 0;
6784                                 sas_target_priv_data->deleted = 0;
6785                         } else
6786                                 sas_target_priv_data = NULL;
6787                         if (starget) {
6788                                 starget_printk(KERN_INFO, starget,
6789                                     "handle(0x%04x), sas_addr(0x%016llx)\n",
6790                                     sas_device_pg0->DevHandle,
6791                                     (unsigned long long)
6792                                     sas_device->sas_address);
6793
6794                                 if (sas_device->enclosure_handle != 0)
6795                                         starget_printk(KERN_INFO, starget,
6796                                          "enclosure logical id(0x%016llx),"
6797                                          " slot(%d)\n",
6798                                          (unsigned long long)
6799                                          sas_device->enclosure_logical_id,
6800                                          sas_device->slot);
6801                         }
6802                         if (sas_device_pg0->Flags &
6803                               MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
6804                                 sas_device->enclosure_level =
6805                                    le16_to_cpu(sas_device_pg0->EnclosureLevel);
6806                                 memcpy(&sas_device->connector_name[0],
6807                                         &sas_device_pg0->ConnectorName[0], 4);
6808                         } else {
6809                                 sas_device->enclosure_level = 0;
6810                                 sas_device->connector_name[0] = '\0';
6811                         }
6812
6813                         if (sas_device->handle == sas_device_pg0->DevHandle)
6814                                 goto out;
6815                         pr_info("\thandle changed from(0x%04x)!!!\n",
6816                             sas_device->handle);
6817                         sas_device->handle = sas_device_pg0->DevHandle;
6818                         if (sas_target_priv_data)
6819                                 sas_target_priv_data->handle =
6820                                         sas_device_pg0->DevHandle;
6821                         goto out;
6822                 }
6823         }
6824  out:
6825         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6826 }
6827
6828 /**
6829  * _scsih_search_responding_sas_devices -
6830  * @ioc: per adapter object
6831  *
6832  * After host reset, find out whether devices are still responding.
6833  * If not remove.
6834  *
6835  * Return nothing.
6836  */
6837 static void
6838 _scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6839 {
6840         Mpi2SasDevicePage0_t sas_device_pg0;
6841         Mpi2ConfigReply_t mpi_reply;
6842         u16 ioc_status;
6843         u16 handle;
6844         u32 device_info;
6845
6846         pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
6847
6848         if (list_empty(&ioc->sas_device_list))
6849                 goto out;
6850
6851         handle = 0xFFFF;
6852         while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6853             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6854             handle))) {
6855                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6856                     MPI2_IOCSTATUS_MASK;
6857                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6858                         break;
6859                 handle = sas_device_pg0.DevHandle =
6860                                 le16_to_cpu(sas_device_pg0.DevHandle);
6861                 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6862                 if (!(_scsih_is_end_device(device_info)))
6863                         continue;
6864                 sas_device_pg0.SASAddress =
6865                                 le64_to_cpu(sas_device_pg0.SASAddress);
6866                 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
6867                 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
6868         }
6869
6870  out:
6871         pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
6872             ioc->name);
6873 }
6874
6875 /**
6876  * _scsih_mark_responding_raid_device - mark a raid_device as responding
6877  * @ioc: per adapter object
6878  * @wwid: world wide identifier for raid volume
6879  * @handle: device handle
6880  *
6881  * After host reset, find out whether devices are still responding.
6882  * Used in _scsih_remove_unresponsive_raid_devices.
6883  *
6884  * Return nothing.
6885  */
6886 static void
6887 _scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
6888         u16 handle)
6889 {
6890         struct MPT3SAS_TARGET *sas_target_priv_data;
6891         struct scsi_target *starget;
6892         struct _raid_device *raid_device;
6893         unsigned long flags;
6894
6895         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6896         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6897                 if (raid_device->wwid == wwid && raid_device->starget) {
6898                         starget = raid_device->starget;
6899                         if (starget && starget->hostdata) {
6900                                 sas_target_priv_data = starget->hostdata;
6901                                 sas_target_priv_data->deleted = 0;
6902                         } else
6903                                 sas_target_priv_data = NULL;
6904                         raid_device->responding = 1;
6905                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6906                         starget_printk(KERN_INFO, raid_device->starget,
6907                             "handle(0x%04x), wwid(0x%016llx)\n", handle,
6908                             (unsigned long long)raid_device->wwid);
6909                         spin_lock_irqsave(&ioc->raid_device_lock, flags);
6910                         if (raid_device->handle == handle) {
6911                                 spin_unlock_irqrestore(&ioc->raid_device_lock,
6912                                     flags);
6913                                 return;
6914                         }
6915                         pr_info("\thandle changed from(0x%04x)!!!\n",
6916                             raid_device->handle);
6917                         raid_device->handle = handle;
6918                         if (sas_target_priv_data)
6919                                 sas_target_priv_data->handle = handle;
6920                         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6921                         return;
6922                 }
6923         }
6924         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6925 }
6926
6927 /**
6928  * _scsih_search_responding_raid_devices -
6929  * @ioc: per adapter object
6930  *
6931  * After host reset, find out whether devices are still responding.
6932  * If not remove.
6933  *
6934  * Return nothing.
6935  */
6936 static void
6937 _scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
6938 {
6939         Mpi2RaidVolPage1_t volume_pg1;
6940         Mpi2RaidVolPage0_t volume_pg0;
6941         Mpi2RaidPhysDiskPage0_t pd_pg0;
6942         Mpi2ConfigReply_t mpi_reply;
6943         u16 ioc_status;
6944         u16 handle;
6945         u8 phys_disk_num;
6946
6947         if (!ioc->ir_firmware)
6948                 return;
6949
6950         pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
6951             ioc->name);
6952
6953         if (list_empty(&ioc->raid_device_list))
6954                 goto out;
6955
6956         handle = 0xFFFF;
6957         while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6958             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6959                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6960                     MPI2_IOCSTATUS_MASK;
6961                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6962                         break;
6963                 handle = le16_to_cpu(volume_pg1.DevHandle);
6964
6965                 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6966                     &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6967                      sizeof(Mpi2RaidVolPage0_t)))
6968                         continue;
6969
6970                 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6971                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6972                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6973                         _scsih_mark_responding_raid_device(ioc,
6974                             le64_to_cpu(volume_pg1.WWID), handle);
6975         }
6976
6977         /* refresh the pd_handles */
6978                 phys_disk_num = 0xFF;
6979                 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6980                 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6981                     &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6982                     phys_disk_num))) {
6983                         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6984                             MPI2_IOCSTATUS_MASK;
6985                         if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
6986                                 break;
6987                         phys_disk_num = pd_pg0.PhysDiskNum;
6988                         handle = le16_to_cpu(pd_pg0.DevHandle);
6989                         set_bit(handle, ioc->pd_handles);
6990                 }
6991  out:
6992         pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
6993                 ioc->name);
6994 }
6995
6996 /**
6997  * _scsih_mark_responding_expander - mark a expander as responding
6998  * @ioc: per adapter object
6999  * @sas_address: sas address
7000  * @handle:
7001  *
7002  * After host reset, find out whether devices are still responding.
7003  * Used in _scsih_remove_unresponsive_expanders.
7004  *
7005  * Return nothing.
7006  */
7007 static void
7008 _scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7009         u16 handle)
7010 {
7011         struct _sas_node *sas_expander;
7012         unsigned long flags;
7013         int i;
7014
7015         spin_lock_irqsave(&ioc->sas_node_lock, flags);
7016         list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7017                 if (sas_expander->sas_address != sas_address)
7018                         continue;
7019                 sas_expander->responding = 1;
7020                 if (sas_expander->handle == handle)
7021                         goto out;
7022                 pr_info("\texpander(0x%016llx): handle changed" \
7023                     " from(0x%04x) to (0x%04x)!!!\n",
7024                     (unsigned long long)sas_expander->sas_address,
7025                     sas_expander->handle, handle);
7026                 sas_expander->handle = handle;
7027                 for (i = 0 ; i < sas_expander->num_phys ; i++)
7028                         sas_expander->phy[i].handle = handle;
7029                 goto out;
7030         }
7031  out:
7032         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7033 }
7034
7035 /**
7036  * _scsih_search_responding_expanders -
7037  * @ioc: per adapter object
7038  *
7039  * After host reset, find out whether devices are still responding.
7040  * If not remove.
7041  *
7042  * Return nothing.
7043  */
7044 static void
7045 _scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7046 {
7047         Mpi2ExpanderPage0_t expander_pg0;
7048         Mpi2ConfigReply_t mpi_reply;
7049         u16 ioc_status;
7050         u64 sas_address;
7051         u16 handle;
7052
7053         pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7054
7055         if (list_empty(&ioc->sas_expander_list))
7056                 goto out;
7057
7058         handle = 0xFFFF;
7059         while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7060             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7061
7062                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7063                     MPI2_IOCSTATUS_MASK;
7064                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
7065                         break;
7066
7067                 handle = le16_to_cpu(expander_pg0.DevHandle);
7068                 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7069                 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7070                         handle,
7071                     (unsigned long long)sas_address);
7072                 _scsih_mark_responding_expander(ioc, sas_address, handle);
7073         }
7074
7075  out:
7076         pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7077 }
7078
7079 /**
7080  * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7081  * @ioc: per adapter object
7082  *
7083  * Return nothing.
7084  */
7085 static void
7086 _scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7087 {
7088         struct _sas_device *sas_device, *sas_device_next;
7089         struct _sas_node *sas_expander, *sas_expander_next;
7090         struct _raid_device *raid_device, *raid_device_next;
7091         struct list_head tmp_list;
7092         unsigned long flags;
7093         LIST_HEAD(head);
7094
7095         pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7096             ioc->name);
7097
7098         /* removing unresponding end devices */
7099         pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7100             ioc->name);
7101
7102         /*
7103          * Iterate, pulling off devices marked as non-responding. We become the
7104          * owner for the reference the list had on any object we prune.
7105          */
7106         spin_lock_irqsave(&ioc->sas_device_lock, flags);
7107         list_for_each_entry_safe(sas_device, sas_device_next,
7108                                  &ioc->sas_device_list, list) {
7109                 if (!sas_device->responding)
7110                         list_move_tail(&sas_device->list, &head);
7111                 else
7112                         sas_device->responding = 0;
7113         }
7114         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7115
7116         /*
7117          * Now, uninitialize and remove the unresponding devices we pruned.
7118          */
7119         list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7120                 _scsih_remove_device(ioc, sas_device);
7121                 list_del_init(&sas_device->list);
7122                 sas_device_put(sas_device);
7123         }
7124
7125         /* removing unresponding volumes */
7126         if (ioc->ir_firmware) {
7127                 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7128                         ioc->name);
7129                 list_for_each_entry_safe(raid_device, raid_device_next,
7130                     &ioc->raid_device_list, list) {
7131                         if (!raid_device->responding)
7132                                 _scsih_sas_volume_delete(ioc,
7133                                     raid_device->handle);
7134                         else
7135                                 raid_device->responding = 0;
7136                 }
7137         }
7138
7139         /* removing unresponding expanders */
7140         pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7141             ioc->name);
7142         spin_lock_irqsave(&ioc->sas_node_lock, flags);
7143         INIT_LIST_HEAD(&tmp_list);
7144         list_for_each_entry_safe(sas_expander, sas_expander_next,
7145             &ioc->sas_expander_list, list) {
7146                 if (!sas_expander->responding)
7147                         list_move_tail(&sas_expander->list, &tmp_list);
7148                 else
7149                         sas_expander->responding = 0;
7150         }
7151         spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7152         list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7153             list) {
7154                 list_del(&sas_expander->list);
7155                 _scsih_expander_node_remove(ioc, sas_expander);
7156         }
7157
7158         pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7159             ioc->name);
7160
7161         /* unblock devices */
7162         _scsih_ublock_io_all_device(ioc);
7163 }
7164
7165 static void
7166 _scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7167         struct _sas_node *sas_expander, u16 handle)
7168 {
7169         Mpi2ExpanderPage1_t expander_pg1;
7170         Mpi2ConfigReply_t mpi_reply;
7171         int i;
7172
7173         for (i = 0 ; i < sas_expander->num_phys ; i++) {
7174                 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7175                     &expander_pg1, i, handle))) {
7176                         pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7177                             ioc->name, __FILE__, __LINE__, __func__);
7178                         return;
7179                 }
7180
7181                 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7182                     le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7183                     expander_pg1.NegotiatedLinkRate >> 4);
7184         }
7185 }
7186
7187 /**
7188  * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7189  * @ioc: per adapter object
7190  *
7191  * Return nothing.
7192  */
7193 static void
7194 _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7195 {
7196         Mpi2ExpanderPage0_t expander_pg0;
7197         Mpi2SasDevicePage0_t sas_device_pg0;
7198         Mpi2RaidVolPage1_t volume_pg1;
7199         Mpi2RaidVolPage0_t volume_pg0;
7200         Mpi2RaidPhysDiskPage0_t pd_pg0;
7201         Mpi2EventIrConfigElement_t element;
7202         Mpi2ConfigReply_t mpi_reply;
7203         u8 phys_disk_num;
7204         u16 ioc_status;
7205         u16 handle, parent_handle;
7206         u64 sas_address;
7207         struct _sas_device *sas_device;
7208         struct _sas_node *expander_device;
7209         static struct _raid_device *raid_device;
7210         u8 retry_count;
7211         unsigned long flags;
7212
7213         pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7214
7215         _scsih_sas_host_refresh(ioc);
7216
7217         pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7218
7219         /* expanders */
7220         handle = 0xFFFF;
7221         while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7222             MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7223                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7224                     MPI2_IOCSTATUS_MASK;
7225                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7226                         pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7227                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7228                             ioc->name, ioc_status,
7229                             le32_to_cpu(mpi_reply.IOCLogInfo));
7230                         break;
7231                 }
7232                 handle = le16_to_cpu(expander_pg0.DevHandle);
7233                 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7234                 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7235                     ioc, le64_to_cpu(expander_pg0.SASAddress));
7236                 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7237                 if (expander_device)
7238                         _scsih_refresh_expander_links(ioc, expander_device,
7239                             handle);
7240                 else {
7241                         pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7242                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7243                             handle, (unsigned long long)
7244                             le64_to_cpu(expander_pg0.SASAddress));
7245                         _scsih_expander_add(ioc, handle);
7246                         pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7247                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7248                             handle, (unsigned long long)
7249                             le64_to_cpu(expander_pg0.SASAddress));
7250                 }
7251         }
7252
7253         pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7254             ioc->name);
7255
7256         if (!ioc->ir_firmware)
7257                 goto skip_to_sas;
7258
7259         pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7260
7261         /* phys disk */
7262         phys_disk_num = 0xFF;
7263         while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7264             &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7265             phys_disk_num))) {
7266                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7267                     MPI2_IOCSTATUS_MASK;
7268                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7269                         pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7270                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7271                             ioc->name, ioc_status,
7272                             le32_to_cpu(mpi_reply.IOCLogInfo));
7273                         break;
7274                 }
7275                 phys_disk_num = pd_pg0.PhysDiskNum;
7276                 handle = le16_to_cpu(pd_pg0.DevHandle);
7277                 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7278                 if (sas_device) {
7279                         sas_device_put(sas_device);
7280                         continue;
7281                 }
7282                 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7283                     &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7284                     handle) != 0)
7285                         continue;
7286                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7287                     MPI2_IOCSTATUS_MASK;
7288                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7289                         pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7290                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7291                             ioc->name, ioc_status,
7292                             le32_to_cpu(mpi_reply.IOCLogInfo));
7293                         break;
7294                 }
7295                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7296                 if (!_scsih_get_sas_address(ioc, parent_handle,
7297                     &sas_address)) {
7298                         pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7299                             " handle (0x%04x), sas_addr(0x%016llx)\n",
7300                             ioc->name, handle, (unsigned long long)
7301                             le64_to_cpu(sas_device_pg0.SASAddress));
7302                         mpt3sas_transport_update_links(ioc, sas_address,
7303                             handle, sas_device_pg0.PhyNum,
7304                             MPI2_SAS_NEG_LINK_RATE_1_5);
7305                         set_bit(handle, ioc->pd_handles);
7306                         retry_count = 0;
7307                         /* This will retry adding the end device.
7308                          * _scsih_add_device() will decide on retries and
7309                          * return "1" when it should be retried
7310                          */
7311                         while (_scsih_add_device(ioc, handle, retry_count++,
7312                             1)) {
7313                                 ssleep(1);
7314                         }
7315                         pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7316                             " handle (0x%04x), sas_addr(0x%016llx)\n",
7317                             ioc->name, handle, (unsigned long long)
7318                             le64_to_cpu(sas_device_pg0.SASAddress));
7319                 }
7320         }
7321
7322         pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7323             ioc->name);
7324
7325         pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7326
7327         /* volumes */
7328         handle = 0xFFFF;
7329         while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7330             &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7331                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7332                     MPI2_IOCSTATUS_MASK;
7333                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7334                         pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7335                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7336                             ioc->name, ioc_status,
7337                             le32_to_cpu(mpi_reply.IOCLogInfo));
7338                         break;
7339                 }
7340                 handle = le16_to_cpu(volume_pg1.DevHandle);
7341                 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7342                 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7343                     le64_to_cpu(volume_pg1.WWID));
7344                 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7345                 if (raid_device)
7346                         continue;
7347                 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7348                     &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7349                      sizeof(Mpi2RaidVolPage0_t)))
7350                         continue;
7351                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7352                     MPI2_IOCSTATUS_MASK;
7353                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7354                         pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7355                             "ioc_status(0x%04x), loginfo(0x%08x)\n",
7356                             ioc->name, ioc_status,
7357                             le32_to_cpu(mpi_reply.IOCLogInfo));
7358                         break;
7359                 }
7360                 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7361                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7362                     volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7363                         memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7364                         element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7365                         element.VolDevHandle = volume_pg1.DevHandle;
7366                         pr_info(MPT3SAS_FMT
7367                                 "\tBEFORE adding volume: handle (0x%04x)\n",
7368                                 ioc->name, volume_pg1.DevHandle);
7369                         _scsih_sas_volume_add(ioc, &element);
7370                         pr_info(MPT3SAS_FMT
7371                                 "\tAFTER adding volume: handle (0x%04x)\n",
7372                                 ioc->name, volume_pg1.DevHandle);
7373                 }
7374         }
7375
7376         pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7377             ioc->name);
7378
7379  skip_to_sas:
7380
7381         pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7382             ioc->name);
7383
7384         /* sas devices */
7385         handle = 0xFFFF;
7386         while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7387             &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7388             handle))) {
7389                 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7390                     MPI2_IOCSTATUS_MASK;
7391                 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7392                         pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7393                             " ioc_status(0x%04x), loginfo(0x%08x)\n",
7394                             ioc->name, ioc_status,
7395                             le32_to_cpu(mpi_reply.IOCLogInfo));
7396                         break;
7397                 }
7398                 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7399                 if (!(_scsih_is_end_device(
7400                     le32_to_cpu(sas_device_pg0.DeviceInfo))))
7401                         continue;
7402                 sas_device = mpt3sas_get_sdev_by_addr(ioc,
7403                     le64_to_cpu(sas_device_pg0.SASAddress));
7404                 if (sas_device) {
7405                         sas_device_put(sas_device);
7406                         continue;
7407                 }
7408                 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7409                 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7410                         pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7411                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7412                             handle, (unsigned long long)
7413                             le64_to_cpu(sas_device_pg0.SASAddress));
7414                         mpt3sas_transport_update_links(ioc, sas_address, handle,
7415                             sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7416                         retry_count = 0;
7417                         /* This will retry adding the end device.
7418                          * _scsih_add_device() will decide on retries and
7419                          * return "1" when it should be retried
7420                          */
7421                         while (_scsih_add_device(ioc, handle, retry_count++,
7422                             0)) {
7423                                 ssleep(1);
7424                         }
7425                         pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7426                             "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7427                             handle, (unsigned long long)
7428                             le64_to_cpu(sas_device_pg0.SASAddress));
7429                 }
7430         }
7431         pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7432             ioc->name);
7433
7434         pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7435 }
7436 /**
7437  * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7438  * @ioc: per adapter object
7439  * @reset_phase: phase
7440  *
7441  * The handler for doing any required cleanup or initialization.
7442  *
7443  * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7444  * MPT3_IOC_DONE_RESET
7445  *
7446  * Return nothing.
7447  */
7448 void
7449 mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7450 {
7451         switch (reset_phase) {
7452         case MPT3_IOC_PRE_RESET:
7453                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7454                         "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7455                 break;
7456         case MPT3_IOC_AFTER_RESET:
7457                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7458                         "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7459                 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7460                         ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7461                         mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7462                         complete(&ioc->scsih_cmds.done);
7463                 }
7464                 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7465                         ioc->tm_cmds.status |= MPT3_CMD_RESET;
7466                         mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7467                         complete(&ioc->tm_cmds.done);
7468                 }
7469
7470                 _scsih_fw_event_cleanup_queue(ioc);
7471                 _scsih_flush_running_cmds(ioc);
7472                 break;
7473         case MPT3_IOC_DONE_RESET:
7474                 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7475                         "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7476                 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7477                     !ioc->sas_hba.num_phys)) {
7478                         _scsih_prep_device_scan(ioc);
7479                         _scsih_search_responding_sas_devices(ioc);
7480                         _scsih_search_responding_raid_devices(ioc);
7481                         _scsih_search_responding_expanders(ioc);
7482                         _scsih_error_recovery_delete_devices(ioc);
7483                 }
7484                 break;
7485         }
7486 }
7487
7488 /**
7489  * _mpt3sas_fw_work - delayed task for processing firmware events
7490  * @ioc: per adapter object
7491  * @fw_event: The fw_event_work object
7492  * Context: user.
7493  *
7494  * Return nothing.
7495  */
7496 static void
7497 _mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7498 {
7499         _scsih_fw_event_del_from_list(ioc, fw_event);
7500
7501         /* the queue is being flushed so ignore this event */
7502         if (ioc->remove_host || ioc->pci_error_recovery) {
7503                 fw_event_work_put(fw_event);
7504                 return;
7505         }
7506
7507         switch (fw_event->event) {
7508         case MPT3SAS_PROCESS_TRIGGER_DIAG:
7509                 mpt3sas_process_trigger_data(ioc,
7510                         (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7511                         fw_event->event_data);
7512                 break;
7513         case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7514                 while (scsi_host_in_recovery(ioc->shost) ||
7515                                 ioc->shost_recovery) {
7516                         /*
7517                          * If we're unloading, bail. Otherwise, this can become
7518                          * an infinite loop.
7519                          */
7520                         if (ioc->remove_host)
7521                                 goto out;
7522
7523                         ssleep(1);
7524                 }
7525                 _scsih_remove_unresponding_sas_devices(ioc);
7526                 _scsih_scan_for_devices_after_reset(ioc);
7527                 break;
7528         case MPT3SAS_PORT_ENABLE_COMPLETE:
7529                 ioc->start_scan = 0;
7530         if (missing_delay[0] != -1 && missing_delay[1] != -1)
7531                         mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7532                             missing_delay[1]);
7533                 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7534                         "port enable: complete from worker thread\n",
7535                         ioc->name));
7536                 break;
7537         case MPT3SAS_TURN_ON_PFA_LED:
7538                 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
7539                 break;
7540         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7541                 _scsih_sas_topology_change_event(ioc, fw_event);
7542                 break;
7543         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7544                 _scsih_sas_device_status_change_event(ioc, fw_event);
7545                 break;
7546         case MPI2_EVENT_SAS_DISCOVERY:
7547                 _scsih_sas_discovery_event(ioc, fw_event);
7548                 break;
7549         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7550                 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7551                 break;
7552         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7553                 _scsih_sas_enclosure_dev_status_change_event(ioc,
7554                     fw_event);
7555                 break;
7556         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7557                 _scsih_sas_ir_config_change_event(ioc, fw_event);
7558                 break;
7559         case MPI2_EVENT_IR_VOLUME:
7560                 _scsih_sas_ir_volume_event(ioc, fw_event);
7561                 break;
7562         case MPI2_EVENT_IR_PHYSICAL_DISK:
7563                 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7564                 break;
7565         case MPI2_EVENT_IR_OPERATION_STATUS:
7566                 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7567                 break;
7568         }
7569 out:
7570         fw_event_work_put(fw_event);
7571 }
7572
7573 /**
7574  * _firmware_event_work
7575  * @ioc: per adapter object
7576  * @work: The fw_event_work object
7577  * Context: user.
7578  *
7579  * wrappers for the work thread handling firmware events
7580  *
7581  * Return nothing.
7582  */
7583
7584 static void
7585 _firmware_event_work(struct work_struct *work)
7586 {
7587         struct fw_event_work *fw_event = container_of(work,
7588             struct fw_event_work, work);
7589
7590         _mpt3sas_fw_work(fw_event->ioc, fw_event);
7591 }
7592
7593 /**
7594  * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7595  * @ioc: per adapter object
7596  * @msix_index: MSIX table index supplied by the OS
7597  * @reply: reply message frame(lower 32bit addr)
7598  * Context: interrupt.
7599  *
7600  * This function merely adds a new work task into ioc->firmware_event_thread.
7601  * The tasks are worked from _firmware_event_work in user context.
7602  *
7603  * Return 1 meaning mf should be freed from _base_interrupt
7604  *        0 means the mf is freed from this function.
7605  */
7606 u8
7607 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7608         u32 reply)
7609 {
7610         struct fw_event_work *fw_event;
7611         Mpi2EventNotificationReply_t *mpi_reply;
7612         u16 event;
7613         u16 sz;
7614
7615         /* events turned off due to host reset or driver unloading */
7616         if (ioc->remove_host || ioc->pci_error_recovery)
7617                 return 1;
7618
7619         mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7620
7621         if (unlikely(!mpi_reply)) {
7622                 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7623                     ioc->name, __FILE__, __LINE__, __func__);
7624                 return 1;
7625         }
7626
7627         event = le16_to_cpu(mpi_reply->Event);
7628
7629         if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7630                 mpt3sas_trigger_event(ioc, event, 0);
7631
7632         switch (event) {
7633         /* handle these */
7634         case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7635         {
7636                 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7637                     (Mpi2EventDataSasBroadcastPrimitive_t *)
7638                     mpi_reply->EventData;
7639
7640                 if (baen_data->Primitive !=
7641                     MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7642                         return 1;
7643
7644                 if (ioc->broadcast_aen_busy) {
7645                         ioc->broadcast_aen_pending++;
7646                         return 1;
7647                 } else
7648                         ioc->broadcast_aen_busy = 1;
7649                 break;
7650         }
7651
7652         case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7653                 _scsih_check_topo_delete_events(ioc,
7654                     (Mpi2EventDataSasTopologyChangeList_t *)
7655                     mpi_reply->EventData);
7656                 break;
7657         case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7658                 _scsih_check_ir_config_unhide_events(ioc,
7659                     (Mpi2EventDataIrConfigChangeList_t *)
7660                     mpi_reply->EventData);
7661                 break;
7662         case MPI2_EVENT_IR_VOLUME:
7663                 _scsih_check_volume_delete_events(ioc,
7664                     (Mpi2EventDataIrVolume_t *)
7665                     mpi_reply->EventData);
7666                 break;
7667
7668         case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7669         case MPI2_EVENT_IR_OPERATION_STATUS:
7670         case MPI2_EVENT_SAS_DISCOVERY:
7671         case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7672         case MPI2_EVENT_IR_PHYSICAL_DISK:
7673                 break;
7674
7675         case MPI2_EVENT_TEMP_THRESHOLD:
7676                 _scsih_temp_threshold_events(ioc,
7677                         (Mpi2EventDataTemperature_t *)
7678                         mpi_reply->EventData);
7679                 break;
7680
7681         default: /* ignore the rest */
7682                 return 1;
7683         }
7684
7685         sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7686         fw_event = alloc_fw_event_work(sz);
7687         if (!fw_event) {
7688                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7689                     ioc->name, __FILE__, __LINE__, __func__);
7690                 return 1;
7691         }
7692
7693         memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7694         fw_event->ioc = ioc;
7695         fw_event->VF_ID = mpi_reply->VF_ID;
7696         fw_event->VP_ID = mpi_reply->VP_ID;
7697         fw_event->event = event;
7698         _scsih_fw_event_add(ioc, fw_event);
7699         fw_event_work_put(fw_event);
7700         return 1;
7701 }
7702
7703 /* shost template */
7704 static struct scsi_host_template scsih_driver_template = {
7705         .module                         = THIS_MODULE,
7706         .name                           = "Fusion MPT SAS Host",
7707         .proc_name                      = MPT3SAS_DRIVER_NAME,
7708         .queuecommand                   = _scsih_qcmd,
7709         .target_alloc                   = _scsih_target_alloc,
7710         .slave_alloc                    = _scsih_slave_alloc,
7711         .slave_configure                = _scsih_slave_configure,
7712         .target_destroy                 = _scsih_target_destroy,
7713         .slave_destroy                  = _scsih_slave_destroy,
7714         .scan_finished                  = _scsih_scan_finished,
7715         .scan_start                     = _scsih_scan_start,
7716         .change_queue_depth             = _scsih_change_queue_depth,
7717         .eh_abort_handler               = _scsih_abort,
7718         .eh_device_reset_handler        = _scsih_dev_reset,
7719         .eh_target_reset_handler        = _scsih_target_reset,
7720         .eh_host_reset_handler          = _scsih_host_reset,
7721         .bios_param                     = _scsih_bios_param,
7722         .can_queue                      = 1,
7723         .this_id                        = -1,
7724         .sg_tablesize                   = MPT3SAS_SG_DEPTH,
7725         .max_sectors                    = 32767,
7726         .cmd_per_lun                    = 7,
7727         .use_clustering                 = ENABLE_CLUSTERING,
7728         .shost_attrs                    = mpt3sas_host_attrs,
7729         .sdev_attrs                     = mpt3sas_dev_attrs,
7730         .track_queue_depth              = 1,
7731 };
7732
7733 /**
7734  * _scsih_expander_node_remove - removing expander device from list.
7735  * @ioc: per adapter object
7736  * @sas_expander: the sas_device object
7737  * Context: Calling function should acquire ioc->sas_node_lock.
7738  *
7739  * Removing object and freeing associated memory from the
7740  * ioc->sas_expander_list.
7741  *
7742  * Return nothing.
7743  */
7744 static void
7745 _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7746         struct _sas_node *sas_expander)
7747 {
7748         struct _sas_port *mpt3sas_port, *next;
7749
7750         /* remove sibling ports attached to this expander */
7751         list_for_each_entry_safe(mpt3sas_port, next,
7752            &sas_expander->sas_port_list, port_list) {
7753                 if (ioc->shost_recovery)
7754                         return;
7755                 if (mpt3sas_port->remote_identify.device_type ==
7756                     SAS_END_DEVICE)
7757                         mpt3sas_device_remove_by_sas_address(ioc,
7758                             mpt3sas_port->remote_identify.sas_address);
7759                 else if (mpt3sas_port->remote_identify.device_type ==
7760                     SAS_EDGE_EXPANDER_DEVICE ||
7761                     mpt3sas_port->remote_identify.device_type ==
7762                     SAS_FANOUT_EXPANDER_DEVICE)
7763                         mpt3sas_expander_remove(ioc,
7764                             mpt3sas_port->remote_identify.sas_address);
7765         }
7766
7767         mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7768             sas_expander->sas_address_parent);
7769
7770         pr_info(MPT3SAS_FMT
7771                 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
7772                 ioc->name,
7773             sas_expander->handle, (unsigned long long)
7774             sas_expander->sas_address);
7775
7776         kfree(sas_expander->phy);
7777         kfree(sas_expander);
7778 }
7779
7780 /**
7781  * _scsih_ir_shutdown - IR shutdown notification
7782  * @ioc: per adapter object
7783  *
7784  * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7785  * the host system is shutting down.
7786  *
7787  * Return nothing.
7788  */
7789 static void
7790 _scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
7791 {
7792         Mpi2RaidActionRequest_t *mpi_request;
7793         Mpi2RaidActionReply_t *mpi_reply;
7794         u16 smid;
7795
7796         /* is IR firmware build loaded ? */
7797         if (!ioc->ir_firmware)
7798                 return;
7799
7800         /* are there any volumes ? */
7801         if (list_empty(&ioc->raid_device_list))
7802                 return;
7803
7804         mutex_lock(&ioc->scsih_cmds.mutex);
7805
7806         if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7807                 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
7808                     ioc->name, __func__);
7809                 goto out;
7810         }
7811         ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7812
7813         smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7814         if (!smid) {
7815                 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
7816                     ioc->name, __func__);
7817                 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7818                 goto out;
7819         }
7820
7821         mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7822         ioc->scsih_cmds.smid = smid;
7823         memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7824
7825         mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7826         mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7827
7828         pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
7829         init_completion(&ioc->scsih_cmds.done);
7830         mpt3sas_base_put_smid_default(ioc, smid);
7831         wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7832
7833         if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7834                 pr_err(MPT3SAS_FMT "%s: timeout\n",
7835                     ioc->name, __func__);
7836                 goto out;
7837         }
7838
7839         if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7840                 mpi_reply = ioc->scsih_cmds.reply;
7841                 pr_info(MPT3SAS_FMT
7842                         "IR shutdown (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
7843                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7844                     le32_to_cpu(mpi_reply->IOCLogInfo));
7845         }
7846
7847  out:
7848         ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7849         mutex_unlock(&ioc->scsih_cmds.mutex);
7850 }
7851
7852 /**
7853  * _scsih_remove - detach and remove add host
7854  * @pdev: PCI device struct
7855  *
7856  * Routine called when unloading the driver.
7857  * Return nothing.
7858  */
7859 static void _scsih_remove(struct pci_dev *pdev)
7860 {
7861         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7862         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7863         struct _sas_port *mpt3sas_port, *next_port;
7864         struct _raid_device *raid_device, *next;
7865         struct MPT3SAS_TARGET *sas_target_priv_data;
7866         struct workqueue_struct *wq;
7867         unsigned long flags;
7868
7869         ioc->remove_host = 1;
7870         _scsih_fw_event_cleanup_queue(ioc);
7871
7872         spin_lock_irqsave(&ioc->fw_event_lock, flags);
7873         wq = ioc->firmware_event_thread;
7874         ioc->firmware_event_thread = NULL;
7875         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7876         if (wq)
7877                 destroy_workqueue(wq);
7878
7879         /* release all the volumes */
7880         _scsih_ir_shutdown(ioc);
7881         list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7882             list) {
7883                 if (raid_device->starget) {
7884                         sas_target_priv_data =
7885                             raid_device->starget->hostdata;
7886                         sas_target_priv_data->deleted = 1;
7887                         scsi_remove_target(&raid_device->starget->dev);
7888                 }
7889                 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
7890                         ioc->name,  raid_device->handle,
7891                     (unsigned long long) raid_device->wwid);
7892                 _scsih_raid_device_remove(ioc, raid_device);
7893         }
7894
7895         /* free ports attached to the sas_host */
7896         list_for_each_entry_safe(mpt3sas_port, next_port,
7897            &ioc->sas_hba.sas_port_list, port_list) {
7898                 if (mpt3sas_port->remote_identify.device_type ==
7899                     SAS_END_DEVICE)
7900                         mpt3sas_device_remove_by_sas_address(ioc,
7901                             mpt3sas_port->remote_identify.sas_address);
7902                 else if (mpt3sas_port->remote_identify.device_type ==
7903                     SAS_EDGE_EXPANDER_DEVICE ||
7904                     mpt3sas_port->remote_identify.device_type ==
7905                     SAS_FANOUT_EXPANDER_DEVICE)
7906                         mpt3sas_expander_remove(ioc,
7907                             mpt3sas_port->remote_identify.sas_address);
7908         }
7909
7910         /* free phys attached to the sas_host */
7911         if (ioc->sas_hba.num_phys) {
7912                 kfree(ioc->sas_hba.phy);
7913                 ioc->sas_hba.phy = NULL;
7914                 ioc->sas_hba.num_phys = 0;
7915         }
7916
7917         sas_remove_host(shost);
7918         scsi_remove_host(shost);
7919         mpt3sas_base_detach(ioc);
7920         list_del(&ioc->list);
7921         scsi_host_put(shost);
7922 }
7923
7924 /**
7925  * _scsih_shutdown - routine call during system shutdown
7926  * @pdev: PCI device struct
7927  *
7928  * Return nothing.
7929  */
7930 static void
7931 _scsih_shutdown(struct pci_dev *pdev)
7932 {
7933         struct Scsi_Host *shost = pci_get_drvdata(pdev);
7934         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7935         struct workqueue_struct *wq;
7936         unsigned long flags;
7937
7938         ioc->remove_host = 1;
7939         _scsih_fw_event_cleanup_queue(ioc);
7940
7941         spin_lock_irqsave(&ioc->fw_event_lock, flags);
7942         wq = ioc->firmware_event_thread;
7943         ioc->firmware_event_thread = NULL;
7944         spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7945         if (wq)
7946                 destroy_workqueue(wq);
7947
7948         _scsih_ir_shutdown(ioc);
7949         mpt3sas_base_detach(ioc);
7950 }
7951
7952
7953 /**
7954  * _scsih_probe_boot_devices - reports 1st device
7955  * @ioc: per adapter object
7956  *
7957  * If specified in bios page 2, this routine reports the 1st
7958  * device scsi-ml or sas transport for persistent boot device
7959  * purposes.  Please refer to function _scsih_determine_boot_device()
7960  */
7961 static void
7962 _scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
7963 {
7964         u8 is_raid;
7965         void *device;
7966         struct _sas_device *sas_device;
7967         struct _raid_device *raid_device;
7968         u16 handle;
7969         u64 sas_address_parent;
7970         u64 sas_address;
7971         unsigned long flags;
7972         int rc;
7973
7974          /* no Bios, return immediately */
7975         if (!ioc->bios_pg3.BiosVersion)
7976                 return;
7977
7978         device = NULL;
7979         is_raid = 0;
7980         if (ioc->req_boot_device.device) {
7981                 device =  ioc->req_boot_device.device;
7982                 is_raid = ioc->req_boot_device.is_raid;
7983         } else if (ioc->req_alt_boot_device.device) {
7984                 device =  ioc->req_alt_boot_device.device;
7985                 is_raid = ioc->req_alt_boot_device.is_raid;
7986         } else if (ioc->current_boot_device.device) {
7987                 device =  ioc->current_boot_device.device;
7988                 is_raid = ioc->current_boot_device.is_raid;
7989         }
7990
7991         if (!device)
7992                 return;
7993
7994         if (is_raid) {
7995                 raid_device = device;
7996                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7997                     raid_device->id, 0);
7998                 if (rc)
7999                         _scsih_raid_device_remove(ioc, raid_device);
8000         } else {
8001                 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8002                 sas_device = device;
8003                 handle = sas_device->handle;
8004                 sas_address_parent = sas_device->sas_address_parent;
8005                 sas_address = sas_device->sas_address;
8006                 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8007                 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8008
8009                 if (!mpt3sas_transport_port_add(ioc, handle,
8010                     sas_address_parent)) {
8011                         _scsih_sas_device_remove(ioc, sas_device);
8012                 } else if (!sas_device->starget) {
8013                         if (!ioc->is_driver_loading) {
8014                                 mpt3sas_transport_port_remove(ioc,
8015                                     sas_address,
8016                                     sas_address_parent);
8017                                 _scsih_sas_device_remove(ioc, sas_device);
8018                         }
8019                 }
8020         }
8021 }
8022
8023 /**
8024  * _scsih_probe_raid - reporting raid volumes to scsi-ml
8025  * @ioc: per adapter object
8026  *
8027  * Called during initial loading of the driver.
8028  */
8029 static void
8030 _scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8031 {
8032         struct _raid_device *raid_device, *raid_next;
8033         int rc;
8034
8035         list_for_each_entry_safe(raid_device, raid_next,
8036             &ioc->raid_device_list, list) {
8037                 if (raid_device->starget)
8038                         continue;
8039                 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8040                     raid_device->id, 0);
8041                 if (rc)
8042                         _scsih_raid_device_remove(ioc, raid_device);
8043         }
8044 }
8045
8046 static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8047 {
8048         struct _sas_device *sas_device = NULL;
8049         unsigned long flags;
8050
8051         spin_lock_irqsave(&ioc->sas_device_lock, flags);
8052         if (!list_empty(&ioc->sas_device_init_list)) {
8053                 sas_device = list_first_entry(&ioc->sas_device_init_list,
8054                                 struct _sas_device, list);
8055                 sas_device_get(sas_device);
8056         }
8057         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8058
8059         return sas_device;
8060 }
8061
8062 static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8063                 struct _sas_device *sas_device)
8064 {
8065         unsigned long flags;
8066
8067         spin_lock_irqsave(&ioc->sas_device_lock, flags);
8068
8069         /*
8070          * Since we dropped the lock during the call to port_add(), we need to
8071          * be careful here that somebody else didn't move or delete this item
8072          * while we were busy with other things.
8073          *
8074          * If it was on the list, we need a put() for the reference the list
8075          * had. Either way, we need a get() for the destination list.
8076          */
8077         if (!list_empty(&sas_device->list)) {
8078                 list_del_init(&sas_device->list);
8079                 sas_device_put(sas_device);
8080         }
8081
8082         sas_device_get(sas_device);
8083         list_add_tail(&sas_device->list, &ioc->sas_device_list);
8084
8085         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8086 }
8087
8088 /**
8089  * _scsih_probe_sas - reporting sas devices to sas transport
8090  * @ioc: per adapter object
8091  *
8092  * Called during initial loading of the driver.
8093  */
8094 static void
8095 _scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8096 {
8097         struct _sas_device *sas_device;
8098
8099         while ((sas_device = get_next_sas_device(ioc))) {
8100                 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8101                                 sas_device->sas_address_parent)) {
8102                         _scsih_sas_device_remove(ioc, sas_device);
8103                         sas_device_put(sas_device);
8104                         continue;
8105                 } else if (!sas_device->starget) {
8106                         /*
8107                          * When asyn scanning is enabled, its not possible to
8108                          * remove devices while scanning is turned on due to an
8109                          * oops in scsi_sysfs_add_sdev()->add_device()->
8110                          * sysfs_addrm_start()
8111                          */
8112                         if (!ioc->is_driver_loading) {
8113                                 mpt3sas_transport_port_remove(ioc,
8114                                                 sas_device->sas_address,
8115                                                 sas_device->sas_address_parent);
8116                                 _scsih_sas_device_remove(ioc, sas_device);
8117                                 sas_device_put(sas_device);
8118                                 continue;
8119                         }
8120                 }
8121
8122                 sas_device_make_active(ioc, sas_device);
8123                 sas_device_put(sas_device);
8124         }
8125 }
8126
8127 /**
8128  * _scsih_probe_devices - probing for devices
8129  * @ioc: per adapter object
8130  *
8131  * Called during initial loading of the driver.
8132  */
8133 static void
8134 _scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8135 {
8136         u16 volume_mapping_flags;
8137
8138         if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8139                 return;  /* return when IOC doesn't support initiator mode */
8140
8141         _scsih_probe_boot_devices(ioc);
8142
8143         if (ioc->ir_firmware) {
8144                 volume_mapping_flags =
8145                     le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8146                     MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8147                 if (volume_mapping_flags ==
8148                     MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8149                         _scsih_probe_raid(ioc);
8150                         _scsih_probe_sas(ioc);
8151                 } else {
8152                         _scsih_probe_sas(ioc);
8153                         _scsih_probe_raid(ioc);
8154                 }
8155         } else
8156                 _scsih_probe_sas(ioc);
8157 }
8158
8159 /**
8160  * _scsih_scan_start - scsi lld callback for .scan_start
8161  * @shost: SCSI host pointer
8162  *
8163  * The shost has the ability to discover targets on its own instead
8164  * of scanning the entire bus.  In our implemention, we will kick off
8165  * firmware discovery.
8166  */
8167 static void
8168 _scsih_scan_start(struct Scsi_Host *shost)
8169 {
8170         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8171         int rc;
8172         if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8173                 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8174
8175         if (disable_discovery > 0)
8176                 return;
8177
8178         ioc->start_scan = 1;
8179         rc = mpt3sas_port_enable(ioc);
8180
8181         if (rc != 0)
8182                 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8183 }
8184
8185 /**
8186  * _scsih_scan_finished - scsi lld callback for .scan_finished
8187  * @shost: SCSI host pointer
8188  * @time: elapsed time of the scan in jiffies
8189  *
8190  * This function will be called periodicallyn until it returns 1 with the
8191  * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8192  * we wait for firmware discovery to complete, then return 1.
8193  */
8194 static int
8195 _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8196 {
8197         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8198
8199         if (disable_discovery > 0) {
8200                 ioc->is_driver_loading = 0;
8201                 ioc->wait_for_discovery_to_complete = 0;
8202                 return 1;
8203         }
8204
8205         if (time >= (300 * HZ)) {
8206                 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8207                 pr_info(MPT3SAS_FMT
8208                         "port enable: FAILED with timeout (timeout=300s)\n",
8209                         ioc->name);
8210                 ioc->is_driver_loading = 0;
8211                 return 1;
8212         }
8213
8214         if (ioc->start_scan)
8215                 return 0;
8216
8217         if (ioc->start_scan_failed) {
8218                 pr_info(MPT3SAS_FMT
8219                         "port enable: FAILED with (ioc_status=0x%08x)\n",
8220                         ioc->name, ioc->start_scan_failed);
8221                 ioc->is_driver_loading = 0;
8222                 ioc->wait_for_discovery_to_complete = 0;
8223                 ioc->remove_host = 1;
8224                 return 1;
8225         }
8226
8227         pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8228         ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8229
8230         if (ioc->wait_for_discovery_to_complete) {
8231                 ioc->wait_for_discovery_to_complete = 0;
8232                 _scsih_probe_devices(ioc);
8233         }
8234         mpt3sas_base_start_watchdog(ioc);
8235         ioc->is_driver_loading = 0;
8236         return 1;
8237 }
8238
8239 /**
8240  * _scsih_probe - attach and add scsi host
8241  * @pdev: PCI device struct
8242  * @id: pci device id
8243  *
8244  * Returns 0 success, anything else error.
8245  */
8246 static int
8247 _scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
8248 {
8249         struct MPT3SAS_ADAPTER *ioc;
8250         struct Scsi_Host *shost;
8251         int rv;
8252
8253         shost = scsi_host_alloc(&scsih_driver_template,
8254             sizeof(struct MPT3SAS_ADAPTER));
8255         if (!shost)
8256                 return -ENODEV;
8257
8258         /* init local params */
8259         ioc = shost_priv(shost);
8260         memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8261         INIT_LIST_HEAD(&ioc->list);
8262         list_add_tail(&ioc->list, &mpt3sas_ioc_list);
8263         ioc->shost = shost;
8264         ioc->id = mpt_ids++;
8265         sprintf(ioc->name, "%s%d", MPT3SAS_DRIVER_NAME, ioc->id);
8266         ioc->pdev = pdev;
8267         ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8268         ioc->tm_cb_idx = tm_cb_idx;
8269         ioc->ctl_cb_idx = ctl_cb_idx;
8270         ioc->base_cb_idx = base_cb_idx;
8271         ioc->port_enable_cb_idx = port_enable_cb_idx;
8272         ioc->transport_cb_idx = transport_cb_idx;
8273         ioc->scsih_cb_idx = scsih_cb_idx;
8274         ioc->config_cb_idx = config_cb_idx;
8275         ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8276         ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8277         ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8278         ioc->logging_level = logging_level;
8279         ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8280         /* misc semaphores and spin locks */
8281         mutex_init(&ioc->reset_in_progress_mutex);
8282         spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8283         spin_lock_init(&ioc->scsi_lookup_lock);
8284         spin_lock_init(&ioc->sas_device_lock);
8285         spin_lock_init(&ioc->sas_node_lock);
8286         spin_lock_init(&ioc->fw_event_lock);
8287         spin_lock_init(&ioc->raid_device_lock);
8288         spin_lock_init(&ioc->diag_trigger_lock);
8289
8290         INIT_LIST_HEAD(&ioc->sas_device_list);
8291         INIT_LIST_HEAD(&ioc->sas_device_init_list);
8292         INIT_LIST_HEAD(&ioc->sas_expander_list);
8293         INIT_LIST_HEAD(&ioc->fw_event_list);
8294         INIT_LIST_HEAD(&ioc->raid_device_list);
8295         INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8296         INIT_LIST_HEAD(&ioc->delayed_tr_list);
8297         INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
8298         INIT_LIST_HEAD(&ioc->reply_queue_list);
8299
8300         /* init shost parameters */
8301         shost->max_cmd_len = 32;
8302         shost->max_lun = max_lun;
8303         shost->transportt = mpt3sas_transport_template;
8304         shost->unique_id = ioc->id;
8305
8306         if (max_sectors != 0xFFFF) {
8307                 if (max_sectors < 64) {
8308                         shost->max_sectors = 64;
8309                         pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8310                             "for max_sectors, range is 64 to 32767. Assigning "
8311                             "value of 64.\n", ioc->name, max_sectors);
8312                 } else if (max_sectors > 32767) {
8313                         shost->max_sectors = 32767;
8314                         pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8315                             "for max_sectors, range is 64 to 32767. Assigning "
8316                             "default value of 32767.\n", ioc->name,
8317                             max_sectors);
8318                 } else {
8319                         shost->max_sectors = max_sectors & 0xFFFE;
8320                         pr_info(MPT3SAS_FMT
8321                                 "The max_sectors value is set to %d\n",
8322                                 ioc->name, shost->max_sectors);
8323                 }
8324         }
8325
8326         /* register EEDP capabilities with SCSI layer */
8327         if (prot_mask > 0)
8328                 scsi_host_set_prot(shost, prot_mask);
8329         else
8330                 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8331                                    | SHOST_DIF_TYPE2_PROTECTION
8332                                    | SHOST_DIF_TYPE3_PROTECTION);
8333
8334         scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8335
8336         /* event thread */
8337         snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8338             "fw_event%d", ioc->id);
8339         ioc->firmware_event_thread = alloc_ordered_workqueue(
8340             ioc->firmware_event_name, WQ_MEM_RECLAIM);
8341         if (!ioc->firmware_event_thread) {
8342                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8343                     ioc->name, __FILE__, __LINE__, __func__);
8344                 rv = -ENODEV;
8345                 goto out_thread_fail;
8346         }
8347
8348         ioc->is_driver_loading = 1;
8349         if ((mpt3sas_base_attach(ioc))) {
8350                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8351                     ioc->name, __FILE__, __LINE__, __func__);
8352                 rv = -ENODEV;
8353                 goto out_attach_fail;
8354         }
8355         rv = scsi_add_host(shost, &pdev->dev);
8356         if (rv) {
8357                 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8358                     ioc->name, __FILE__, __LINE__, __func__);
8359                 goto out_add_shost_fail;
8360         }
8361
8362         scsi_scan_host(shost);
8363         return 0;
8364 out_add_shost_fail:
8365         mpt3sas_base_detach(ioc);
8366  out_attach_fail:
8367         destroy_workqueue(ioc->firmware_event_thread);
8368  out_thread_fail:
8369         list_del(&ioc->list);
8370         scsi_host_put(shost);
8371         return rv;
8372 }
8373
8374 #ifdef CONFIG_PM
8375 /**
8376  * _scsih_suspend - power management suspend main entry point
8377  * @pdev: PCI device struct
8378  * @state: PM state change to (usually PCI_D3)
8379  *
8380  * Returns 0 success, anything else error.
8381  */
8382 static int
8383 _scsih_suspend(struct pci_dev *pdev, pm_message_t state)
8384 {
8385         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8386         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8387         pci_power_t device_state;
8388
8389         mpt3sas_base_stop_watchdog(ioc);
8390         flush_scheduled_work();
8391         scsi_block_requests(shost);
8392         device_state = pci_choose_state(pdev, state);
8393         pr_info(MPT3SAS_FMT
8394                 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8395                 ioc->name, pdev, pci_name(pdev), device_state);
8396
8397         pci_save_state(pdev);
8398         mpt3sas_base_free_resources(ioc);
8399         pci_set_power_state(pdev, device_state);
8400         return 0;
8401 }
8402
8403 /**
8404  * _scsih_resume - power management resume main entry point
8405  * @pdev: PCI device struct
8406  *
8407  * Returns 0 success, anything else error.
8408  */
8409 static int
8410 _scsih_resume(struct pci_dev *pdev)
8411 {
8412         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8413         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8414         pci_power_t device_state = pdev->current_state;
8415         int r;
8416
8417         pr_info(MPT3SAS_FMT
8418                 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8419                 ioc->name, pdev, pci_name(pdev), device_state);
8420
8421         pci_set_power_state(pdev, PCI_D0);
8422         pci_enable_wake(pdev, PCI_D0, 0);
8423         pci_restore_state(pdev);
8424         ioc->pdev = pdev;
8425         r = mpt3sas_base_map_resources(ioc);
8426         if (r)
8427                 return r;
8428
8429         mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8430         scsi_unblock_requests(shost);
8431         mpt3sas_base_start_watchdog(ioc);
8432         return 0;
8433 }
8434 #endif /* CONFIG_PM */
8435
8436 /**
8437  * _scsih_pci_error_detected - Called when a PCI error is detected.
8438  * @pdev: PCI device struct
8439  * @state: PCI channel state
8440  *
8441  * Description: Called when a PCI error is detected.
8442  *
8443  * Return value:
8444  *      PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8445  */
8446 static pci_ers_result_t
8447 _scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8448 {
8449         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8450         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8451
8452         pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8453             ioc->name, state);
8454
8455         switch (state) {
8456         case pci_channel_io_normal:
8457                 return PCI_ERS_RESULT_CAN_RECOVER;
8458         case pci_channel_io_frozen:
8459                 /* Fatal error, prepare for slot reset */
8460                 ioc->pci_error_recovery = 1;
8461                 scsi_block_requests(ioc->shost);
8462                 mpt3sas_base_stop_watchdog(ioc);
8463                 mpt3sas_base_free_resources(ioc);
8464                 return PCI_ERS_RESULT_NEED_RESET;
8465         case pci_channel_io_perm_failure:
8466                 /* Permanent error, prepare for device removal */
8467                 ioc->pci_error_recovery = 1;
8468                 mpt3sas_base_stop_watchdog(ioc);
8469                 _scsih_flush_running_cmds(ioc);
8470                 return PCI_ERS_RESULT_DISCONNECT;
8471         }
8472         return PCI_ERS_RESULT_NEED_RESET;
8473 }
8474
8475 /**
8476  * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8477  * @pdev: PCI device struct
8478  *
8479  * Description: This routine is called by the pci error recovery
8480  * code after the PCI slot has been reset, just before we
8481  * should resume normal operations.
8482  */
8483 static pci_ers_result_t
8484 _scsih_pci_slot_reset(struct pci_dev *pdev)
8485 {
8486         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8487         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8488         int rc;
8489
8490         pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8491              ioc->name);
8492
8493         ioc->pci_error_recovery = 0;
8494         ioc->pdev = pdev;
8495         pci_restore_state(pdev);
8496         rc = mpt3sas_base_map_resources(ioc);
8497         if (rc)
8498                 return PCI_ERS_RESULT_DISCONNECT;
8499
8500         rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8501             FORCE_BIG_HAMMER);
8502
8503         pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8504             (rc == 0) ? "success" : "failed");
8505
8506         if (!rc)
8507                 return PCI_ERS_RESULT_RECOVERED;
8508         else
8509                 return PCI_ERS_RESULT_DISCONNECT;
8510 }
8511
8512 /**
8513  * _scsih_pci_resume() - resume normal ops after PCI reset
8514  * @pdev: pointer to PCI device
8515  *
8516  * Called when the error recovery driver tells us that its
8517  * OK to resume normal operation. Use completion to allow
8518  * halted scsi ops to resume.
8519  */
8520 static void
8521 _scsih_pci_resume(struct pci_dev *pdev)
8522 {
8523         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8524         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8525
8526         pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8527
8528         pci_cleanup_aer_uncorrect_error_status(pdev);
8529         mpt3sas_base_start_watchdog(ioc);
8530         scsi_unblock_requests(ioc->shost);
8531 }
8532
8533 /**
8534  * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8535  * @pdev: pointer to PCI device
8536  */
8537 static pci_ers_result_t
8538 _scsih_pci_mmio_enabled(struct pci_dev *pdev)
8539 {
8540         struct Scsi_Host *shost = pci_get_drvdata(pdev);
8541         struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8542
8543         pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
8544             ioc->name);
8545
8546         /* TODO - dump whatever for debugging purposes */
8547
8548         /* Request a slot reset. */
8549         return PCI_ERS_RESULT_NEED_RESET;
8550 }
8551
8552 /* raid transport support */
8553 static struct raid_function_template mpt3sas_raid_functions = {
8554         .cookie         = &scsih_driver_template,
8555         .is_raid        = _scsih_is_raid,
8556         .get_resync     = _scsih_get_resync,
8557         .get_state      = _scsih_get_state,
8558 };
8559
8560 static struct pci_error_handlers _scsih_err_handler = {
8561         .error_detected = _scsih_pci_error_detected,
8562         .mmio_enabled = _scsih_pci_mmio_enabled,
8563         .slot_reset =   _scsih_pci_slot_reset,
8564         .resume =       _scsih_pci_resume,
8565 };
8566
8567 static struct pci_driver scsih_driver = {
8568         .name           = MPT3SAS_DRIVER_NAME,
8569         .id_table       = scsih_pci_table,
8570         .probe          = _scsih_probe,
8571         .remove         = _scsih_remove,
8572         .shutdown       = _scsih_shutdown,
8573         .err_handler    = &_scsih_err_handler,
8574 #ifdef CONFIG_PM
8575         .suspend        = _scsih_suspend,
8576         .resume         = _scsih_resume,
8577 #endif
8578 };
8579
8580
8581 /**
8582  * _scsih_init - main entry point for this driver.
8583  *
8584  * Returns 0 success, anything else error.
8585  */
8586 static int __init
8587 _scsih_init(void)
8588 {
8589         int error;
8590
8591         mpt_ids = 0;
8592
8593         pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
8594             MPT3SAS_DRIVER_VERSION);
8595
8596         mpt3sas_transport_template =
8597             sas_attach_transport(&mpt3sas_transport_functions);
8598         if (!mpt3sas_transport_template)
8599                 return -ENODEV;
8600
8601 /* raid transport support */
8602         mpt3sas_raid_template = raid_class_attach(&mpt3sas_raid_functions);
8603         if (!mpt3sas_raid_template) {
8604                 sas_release_transport(mpt3sas_transport_template);
8605                 return -ENODEV;
8606         }
8607
8608         mpt3sas_base_initialize_callback_handler();
8609
8610          /* queuecommand callback hander */
8611         scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
8612
8613         /* task managment callback handler */
8614         tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
8615
8616         /* base internal commands callback handler */
8617         base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
8618         port_enable_cb_idx = mpt3sas_base_register_callback_handler(
8619             mpt3sas_port_enable_done);
8620
8621         /* transport internal commands callback handler */
8622         transport_cb_idx = mpt3sas_base_register_callback_handler(
8623             mpt3sas_transport_done);
8624
8625         /* scsih internal commands callback handler */
8626         scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
8627
8628         /* configuration page API internal commands callback handler */
8629         config_cb_idx = mpt3sas_base_register_callback_handler(
8630             mpt3sas_config_done);
8631
8632         /* ctl module callback handler */
8633         ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
8634
8635         tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
8636             _scsih_tm_tr_complete);
8637
8638         tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
8639             _scsih_tm_volume_tr_complete);
8640
8641         tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
8642             _scsih_sas_control_complete);
8643
8644         mpt3sas_ctl_init();
8645
8646         error = pci_register_driver(&scsih_driver);
8647         if (error) {
8648                 /* raid transport support */
8649                 raid_class_release(mpt3sas_raid_template);
8650                 sas_release_transport(mpt3sas_transport_template);
8651         }
8652
8653         return error;
8654 }
8655
8656 /**
8657  * _scsih_exit - exit point for this driver (when it is a module).
8658  *
8659  * Returns 0 success, anything else error.
8660  */
8661 static void __exit
8662 _scsih_exit(void)
8663 {
8664         pr_info("mpt3sas version %s unloading\n",
8665             MPT3SAS_DRIVER_VERSION);
8666
8667         mpt3sas_ctl_exit();
8668
8669         pci_unregister_driver(&scsih_driver);
8670
8671
8672         mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
8673         mpt3sas_base_release_callback_handler(tm_cb_idx);
8674         mpt3sas_base_release_callback_handler(base_cb_idx);
8675         mpt3sas_base_release_callback_handler(port_enable_cb_idx);
8676         mpt3sas_base_release_callback_handler(transport_cb_idx);
8677         mpt3sas_base_release_callback_handler(scsih_cb_idx);
8678         mpt3sas_base_release_callback_handler(config_cb_idx);
8679         mpt3sas_base_release_callback_handler(ctl_cb_idx);
8680
8681         mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
8682         mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
8683         mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
8684
8685 /* raid transport support */
8686         raid_class_release(mpt3sas_raid_template);
8687         sas_release_transport(mpt3sas_transport_template);
8688 }
8689
8690 module_init(_scsih_init);
8691 module_exit(_scsih_exit);