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