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