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