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