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