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