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