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