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