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