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