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