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