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