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