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