blob: f2d967c5415e6f8b4060bb1844f13442736072f1 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
5 * Copyright (C) 2007-2008 LSI Corporation
6 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
55
56#include "mpt2sas_base.h"
57
58MODULE_AUTHOR(MPT2SAS_AUTHOR);
59MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
60MODULE_LICENSE("GPL");
61MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
62
63#define RAID_CHANNEL 1
64
65/* forward proto's */
66static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
67 struct _sas_node *sas_expander);
68static void _firmware_event_work(struct work_struct *work);
69
70/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060071LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060072
73/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060074static u8 scsi_io_cb_idx = -1;
75static u8 tm_cb_idx = -1;
76static u8 ctl_cb_idx = -1;
77static u8 base_cb_idx = -1;
78static u8 transport_cb_idx = -1;
79static u8 config_cb_idx = -1;
80static int mpt_ids;
81
82/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060083static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060084MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
85 "(default=0)");
86
87/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
88#define MPT2SAS_MAX_LUN (16895)
89static int max_lun = MPT2SAS_MAX_LUN;
90module_param(max_lun, int, 0);
91MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
92
93/**
94 * struct sense_info - common structure for obtaining sense keys
95 * @skey: sense key
96 * @asc: additional sense code
97 * @ascq: additional sense code qualifier
98 */
99struct sense_info {
100 u8 skey;
101 u8 asc;
102 u8 ascq;
103};
104
105
106#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
107/**
108 * struct fw_event_work - firmware event struct
109 * @list: link list framework
110 * @work: work object (ioc->fault_reset_work_q)
111 * @ioc: per adapter object
112 * @VF_ID: virtual function id
113 * @host_reset_handling: handling events during host reset
114 * @ignore: flag meaning this event has been marked to ignore
115 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
116 * @event_data: reply event data payload follows
117 *
118 * This object stored on ioc->fw_event_list.
119 */
120struct fw_event_work {
121 struct list_head list;
122 struct delayed_work work;
123 struct MPT2SAS_ADAPTER *ioc;
124 u8 VF_ID;
125 u8 host_reset_handling;
126 u8 ignore;
127 u16 event;
128 void *event_data;
129};
130
131/**
132 * struct _scsi_io_transfer - scsi io transfer
133 * @handle: sas device handle (assigned by firmware)
134 * @is_raid: flag set for hidden raid components
135 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
136 * @data_length: data transfer length
137 * @data_dma: dma pointer to data
138 * @sense: sense data
139 * @lun: lun number
140 * @cdb_length: cdb length
141 * @cdb: cdb contents
142 * @valid_reply: flag set for reply message
143 * @timeout: timeout for this command
144 * @sense_length: sense length
145 * @ioc_status: ioc status
146 * @scsi_state: scsi state
147 * @scsi_status: scsi staus
148 * @log_info: log information
149 * @transfer_length: data length transfer when there is a reply message
150 *
151 * Used for sending internal scsi commands to devices within this module.
152 * Refer to _scsi_send_scsi_io().
153 */
154struct _scsi_io_transfer {
155 u16 handle;
156 u8 is_raid;
157 enum dma_data_direction dir;
158 u32 data_length;
159 dma_addr_t data_dma;
160 u8 sense[SCSI_SENSE_BUFFERSIZE];
161 u32 lun;
162 u8 cdb_length;
163 u8 cdb[32];
164 u8 timeout;
165 u8 valid_reply;
166 /* the following bits are only valid when 'valid_reply = 1' */
167 u32 sense_length;
168 u16 ioc_status;
169 u8 scsi_state;
170 u8 scsi_status;
171 u32 log_info;
172 u32 transfer_length;
173};
174
175/*
176 * The pci device ids are defined in mpi/mpi2_cnfg.h.
177 */
178static struct pci_device_id scsih_pci_table[] = {
179 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
180 PCI_ANY_ID, PCI_ANY_ID },
181 /* Falcon ~ 2008*/
182 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
183 PCI_ANY_ID, PCI_ANY_ID },
184 /* Liberator ~ 2108 */
185 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
186 PCI_ANY_ID, PCI_ANY_ID },
187 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
188 PCI_ANY_ID, PCI_ANY_ID },
189 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
190 PCI_ANY_ID, PCI_ANY_ID },
191 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
192 PCI_ANY_ID, PCI_ANY_ID },
193 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
194 PCI_ANY_ID, PCI_ANY_ID },
195 {0} /* Terminating entry */
196};
197MODULE_DEVICE_TABLE(pci, scsih_pci_table);
198
199/**
200 * scsih_set_debug_level - global setting of ioc->logging_level.
201 *
202 * Note: The logging levels are defined in mpt2sas_debug.h.
203 */
204static int
205scsih_set_debug_level(const char *val, struct kernel_param *kp)
206{
207 int ret = param_set_int(val, kp);
208 struct MPT2SAS_ADAPTER *ioc;
209
210 if (ret)
211 return ret;
212
213 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600214 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600215 ioc->logging_level = logging_level;
216 return 0;
217}
218module_param_call(logging_level, scsih_set_debug_level, param_get_int,
219 &logging_level, 0644);
220
221/**
222 * _scsih_srch_boot_sas_address - search based on sas_address
223 * @sas_address: sas address
224 * @boot_device: boot device object from bios page 2
225 *
226 * Returns 1 when there's a match, 0 means no match.
227 */
228static inline int
229_scsih_srch_boot_sas_address(u64 sas_address,
230 Mpi2BootDeviceSasWwid_t *boot_device)
231{
232 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
233}
234
235/**
236 * _scsih_srch_boot_device_name - search based on device name
237 * @device_name: device name specified in INDENTIFY fram
238 * @boot_device: boot device object from bios page 2
239 *
240 * Returns 1 when there's a match, 0 means no match.
241 */
242static inline int
243_scsih_srch_boot_device_name(u64 device_name,
244 Mpi2BootDeviceDeviceName_t *boot_device)
245{
246 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
247}
248
249/**
250 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
251 * @enclosure_logical_id: enclosure logical id
252 * @slot_number: slot number
253 * @boot_device: boot device object from bios page 2
254 *
255 * Returns 1 when there's a match, 0 means no match.
256 */
257static inline int
258_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
259 Mpi2BootDeviceEnclosureSlot_t *boot_device)
260{
261 return (enclosure_logical_id == le64_to_cpu(boot_device->
262 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
263 SlotNumber)) ? 1 : 0;
264}
265
266/**
267 * _scsih_is_boot_device - search for matching boot device.
268 * @sas_address: sas address
269 * @device_name: device name specified in INDENTIFY fram
270 * @enclosure_logical_id: enclosure logical id
271 * @slot_number: slot number
272 * @form: specifies boot device form
273 * @boot_device: boot device object from bios page 2
274 *
275 * Returns 1 when there's a match, 0 means no match.
276 */
277static int
278_scsih_is_boot_device(u64 sas_address, u64 device_name,
279 u64 enclosure_logical_id, u16 slot, u8 form,
280 Mpi2BiosPage2BootDevice_t *boot_device)
281{
282 int rc = 0;
283
284 switch (form) {
285 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
286 if (!sas_address)
287 break;
288 rc = _scsih_srch_boot_sas_address(
289 sas_address, &boot_device->SasWwid);
290 break;
291 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
292 if (!enclosure_logical_id)
293 break;
294 rc = _scsih_srch_boot_encl_slot(
295 enclosure_logical_id,
296 slot, &boot_device->EnclosureSlot);
297 break;
298 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
299 if (!device_name)
300 break;
301 rc = _scsih_srch_boot_device_name(
302 device_name, &boot_device->DeviceName);
303 break;
304 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
305 break;
306 }
307
308 return rc;
309}
310
311/**
312 * _scsih_determine_boot_device - determine boot device.
313 * @ioc: per adapter object
314 * @device: either sas_device or raid_device object
315 * @is_raid: [flag] 1 = raid object, 0 = sas object
316 *
317 * Determines whether this device should be first reported device to
318 * to scsi-ml or sas transport, this purpose is for persistant boot device.
319 * There are primary, alternate, and current entries in bios page 2. The order
320 * priority is primary, alternate, then current. This routine saves
321 * the corresponding device object and is_raid flag in the ioc object.
322 * The saved data to be used later in _scsih_probe_boot_devices().
323 */
324static void
325_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
326 void *device, u8 is_raid)
327{
328 struct _sas_device *sas_device;
329 struct _raid_device *raid_device;
330 u64 sas_address;
331 u64 device_name;
332 u64 enclosure_logical_id;
333 u16 slot;
334
335 /* only process this function when driver loads */
336 if (!ioc->wait_for_port_enable_to_complete)
337 return;
338
339 if (!is_raid) {
340 sas_device = device;
341 sas_address = sas_device->sas_address;
342 device_name = sas_device->device_name;
343 enclosure_logical_id = sas_device->enclosure_logical_id;
344 slot = sas_device->slot;
345 } else {
346 raid_device = device;
347 sas_address = raid_device->wwid;
348 device_name = 0;
349 enclosure_logical_id = 0;
350 slot = 0;
351 }
352
353 if (!ioc->req_boot_device.device) {
354 if (_scsih_is_boot_device(sas_address, device_name,
355 enclosure_logical_id, slot,
356 (ioc->bios_pg2.ReqBootDeviceForm &
357 MPI2_BIOSPAGE2_FORM_MASK),
358 &ioc->bios_pg2.RequestedBootDevice)) {
359 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
360 "%s: req_boot_device(0x%016llx)\n",
361 ioc->name, __func__,
362 (unsigned long long)sas_address));
363 ioc->req_boot_device.device = device;
364 ioc->req_boot_device.is_raid = is_raid;
365 }
366 }
367
368 if (!ioc->req_alt_boot_device.device) {
369 if (_scsih_is_boot_device(sas_address, device_name,
370 enclosure_logical_id, slot,
371 (ioc->bios_pg2.ReqAltBootDeviceForm &
372 MPI2_BIOSPAGE2_FORM_MASK),
373 &ioc->bios_pg2.RequestedAltBootDevice)) {
374 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
375 "%s: req_alt_boot_device(0x%016llx)\n",
376 ioc->name, __func__,
377 (unsigned long long)sas_address));
378 ioc->req_alt_boot_device.device = device;
379 ioc->req_alt_boot_device.is_raid = is_raid;
380 }
381 }
382
383 if (!ioc->current_boot_device.device) {
384 if (_scsih_is_boot_device(sas_address, device_name,
385 enclosure_logical_id, slot,
386 (ioc->bios_pg2.CurrentBootDeviceForm &
387 MPI2_BIOSPAGE2_FORM_MASK),
388 &ioc->bios_pg2.CurrentBootDevice)) {
389 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT
390 "%s: current_boot_device(0x%016llx)\n",
391 ioc->name, __func__,
392 (unsigned long long)sas_address));
393 ioc->current_boot_device.device = device;
394 ioc->current_boot_device.is_raid = is_raid;
395 }
396 }
397}
398
399/**
400 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
401 * @ioc: per adapter object
402 * @sas_address: sas address
403 * Context: Calling function should acquire ioc->sas_device_lock
404 *
405 * This searches for sas_device based on sas_address, then return sas_device
406 * object.
407 */
408struct _sas_device *
409mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
410 u64 sas_address)
411{
412 struct _sas_device *sas_device, *r;
413
414 r = NULL;
415 /* check the sas_device_init_list */
416 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
417 list) {
418 if (sas_device->sas_address != sas_address)
419 continue;
420 r = sas_device;
421 goto out;
422 }
423
424 /* then check the sas_device_list */
425 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
426 if (sas_device->sas_address != sas_address)
427 continue;
428 r = sas_device;
429 goto out;
430 }
431 out:
432 return r;
433}
434
435/**
436 * _scsih_sas_device_find_by_handle - sas device search
437 * @ioc: per adapter object
438 * @handle: sas device handle (assigned by firmware)
439 * Context: Calling function should acquire ioc->sas_device_lock
440 *
441 * This searches for sas_device based on sas_address, then return sas_device
442 * object.
443 */
444static struct _sas_device *
445_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
446{
447 struct _sas_device *sas_device, *r;
448
449 r = NULL;
450 if (ioc->wait_for_port_enable_to_complete) {
451 list_for_each_entry(sas_device, &ioc->sas_device_init_list,
452 list) {
453 if (sas_device->handle != handle)
454 continue;
455 r = sas_device;
456 goto out;
457 }
458 } else {
459 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
460 if (sas_device->handle != handle)
461 continue;
462 r = sas_device;
463 goto out;
464 }
465 }
466
467 out:
468 return r;
469}
470
471/**
472 * _scsih_sas_device_remove - remove sas_device from list.
473 * @ioc: per adapter object
474 * @sas_device: the sas_device object
475 * Context: This function will acquire ioc->sas_device_lock.
476 *
477 * Removing object and freeing associated memory from the ioc->sas_device_list.
478 */
479static void
480_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
481 struct _sas_device *sas_device)
482{
483 unsigned long flags;
484
485 spin_lock_irqsave(&ioc->sas_device_lock, flags);
486 list_del(&sas_device->list);
487 memset(sas_device, 0, sizeof(struct _sas_device));
488 kfree(sas_device);
489 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
490}
491
492/**
493 * _scsih_sas_device_add - insert sas_device to the list.
494 * @ioc: per adapter object
495 * @sas_device: the sas_device object
496 * Context: This function will acquire ioc->sas_device_lock.
497 *
498 * Adding new object to the ioc->sas_device_list.
499 */
500static void
501_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
502 struct _sas_device *sas_device)
503{
504 unsigned long flags;
505 u16 handle, parent_handle;
506 u64 sas_address;
507
508 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
509 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
510 sas_device->handle, (unsigned long long)sas_device->sas_address));
511
512 spin_lock_irqsave(&ioc->sas_device_lock, flags);
513 list_add_tail(&sas_device->list, &ioc->sas_device_list);
514 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
515
516 handle = sas_device->handle;
517 parent_handle = sas_device->parent_handle;
518 sas_address = sas_device->sas_address;
519 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
520 _scsih_sas_device_remove(ioc, sas_device);
521 } else if (!sas_device->starget) {
522 mpt2sas_transport_port_remove(ioc, sas_address, parent_handle);
523 _scsih_sas_device_remove(ioc, sas_device);
524 }
525}
526
527/**
528 * _scsih_sas_device_init_add - insert sas_device to the list.
529 * @ioc: per adapter object
530 * @sas_device: the sas_device object
531 * Context: This function will acquire ioc->sas_device_lock.
532 *
533 * Adding new object at driver load time to the ioc->sas_device_init_list.
534 */
535static void
536_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
537 struct _sas_device *sas_device)
538{
539 unsigned long flags;
540
541 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
542 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
543 sas_device->handle, (unsigned long long)sas_device->sas_address));
544
545 spin_lock_irqsave(&ioc->sas_device_lock, flags);
546 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
547 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
548 _scsih_determine_boot_device(ioc, sas_device, 0);
549}
550
551/**
552 * mpt2sas_scsih_expander_find_by_handle - expander device search
553 * @ioc: per adapter object
554 * @handle: expander handle (assigned by firmware)
555 * Context: Calling function should acquire ioc->sas_device_lock
556 *
557 * This searches for expander device based on handle, then returns the
558 * sas_node object.
559 */
560struct _sas_node *
561mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
562{
563 struct _sas_node *sas_expander, *r;
564
565 r = NULL;
566 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
567 if (sas_expander->handle != handle)
568 continue;
569 r = sas_expander;
570 goto out;
571 }
572 out:
573 return r;
574}
575
576/**
577 * _scsih_raid_device_find_by_id - raid device search
578 * @ioc: per adapter object
579 * @id: sas device target id
580 * @channel: sas device channel
581 * Context: Calling function should acquire ioc->raid_device_lock
582 *
583 * This searches for raid_device based on target id, then return raid_device
584 * object.
585 */
586static struct _raid_device *
587_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
588{
589 struct _raid_device *raid_device, *r;
590
591 r = NULL;
592 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
593 if (raid_device->id == id && raid_device->channel == channel) {
594 r = raid_device;
595 goto out;
596 }
597 }
598
599 out:
600 return r;
601}
602
603/**
604 * _scsih_raid_device_find_by_handle - raid device search
605 * @ioc: per adapter object
606 * @handle: sas device handle (assigned by firmware)
607 * Context: Calling function should acquire ioc->raid_device_lock
608 *
609 * This searches for raid_device based on handle, then return raid_device
610 * object.
611 */
612static struct _raid_device *
613_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
614{
615 struct _raid_device *raid_device, *r;
616
617 r = NULL;
618 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
619 if (raid_device->handle != handle)
620 continue;
621 r = raid_device;
622 goto out;
623 }
624
625 out:
626 return r;
627}
628
629/**
630 * _scsih_raid_device_find_by_wwid - raid device search
631 * @ioc: per adapter object
632 * @handle: sas device handle (assigned by firmware)
633 * Context: Calling function should acquire ioc->raid_device_lock
634 *
635 * This searches for raid_device based on wwid, then return raid_device
636 * object.
637 */
638static struct _raid_device *
639_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
640{
641 struct _raid_device *raid_device, *r;
642
643 r = NULL;
644 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
645 if (raid_device->wwid != wwid)
646 continue;
647 r = raid_device;
648 goto out;
649 }
650
651 out:
652 return r;
653}
654
655/**
656 * _scsih_raid_device_add - add raid_device object
657 * @ioc: per adapter object
658 * @raid_device: raid_device object
659 *
660 * This is added to the raid_device_list link list.
661 */
662static void
663_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
664 struct _raid_device *raid_device)
665{
666 unsigned long flags;
667
668 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle"
669 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
670 raid_device->handle, (unsigned long long)raid_device->wwid));
671
672 spin_lock_irqsave(&ioc->raid_device_lock, flags);
673 list_add_tail(&raid_device->list, &ioc->raid_device_list);
674 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
675}
676
677/**
678 * _scsih_raid_device_remove - delete raid_device object
679 * @ioc: per adapter object
680 * @raid_device: raid_device object
681 *
682 * This is removed from the raid_device_list link list.
683 */
684static void
685_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
686 struct _raid_device *raid_device)
687{
688 unsigned long flags;
689
690 spin_lock_irqsave(&ioc->raid_device_lock, flags);
691 list_del(&raid_device->list);
692 memset(raid_device, 0, sizeof(struct _raid_device));
693 kfree(raid_device);
694 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
695}
696
697/**
698 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
699 * @ioc: per adapter object
700 * @sas_address: sas address
701 * Context: Calling function should acquire ioc->sas_node_lock.
702 *
703 * This searches for expander device based on sas_address, then returns the
704 * sas_node object.
705 */
706struct _sas_node *
707mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
708 u64 sas_address)
709{
710 struct _sas_node *sas_expander, *r;
711
712 r = NULL;
713 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
714 if (sas_expander->sas_address != sas_address)
715 continue;
716 r = sas_expander;
717 goto out;
718 }
719 out:
720 return r;
721}
722
723/**
724 * _scsih_expander_node_add - insert expander device to the list.
725 * @ioc: per adapter object
726 * @sas_expander: the sas_device object
727 * Context: This function will acquire ioc->sas_node_lock.
728 *
729 * Adding new object to the ioc->sas_expander_list.
730 *
731 * Return nothing.
732 */
733static void
734_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
735 struct _sas_node *sas_expander)
736{
737 unsigned long flags;
738
739 spin_lock_irqsave(&ioc->sas_node_lock, flags);
740 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
741 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
742}
743
744/**
745 * _scsih_is_end_device - determines if device is an end device
746 * @device_info: bitfield providing information about the device.
747 * Context: none
748 *
749 * Returns 1 if end device.
750 */
751static int
752_scsih_is_end_device(u32 device_info)
753{
754 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
755 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
756 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
757 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
758 return 1;
759 else
760 return 0;
761}
762
763/**
764 * _scsih_scsi_lookup_get - returns scmd entry
765 * @ioc: per adapter object
766 * @smid: system request message index
767 * Context: This function will acquire ioc->scsi_lookup_lock.
768 *
769 * Returns the smid stored scmd pointer.
770 */
771static struct scsi_cmnd *
772_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
773{
774 unsigned long flags;
775 struct scsi_cmnd *scmd;
776
777 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
778 scmd = ioc->scsi_lookup[smid - 1].scmd;
779 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
780 return scmd;
781}
782
783/**
784 * mptscsih_getclear_scsi_lookup - returns scmd entry
785 * @ioc: per adapter object
786 * @smid: system request message index
787 * Context: This function will acquire ioc->scsi_lookup_lock.
788 *
789 * Returns the smid stored scmd pointer, as well as clearing the scmd pointer.
790 */
791static struct scsi_cmnd *
792_scsih_scsi_lookup_getclear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
793{
794 unsigned long flags;
795 struct scsi_cmnd *scmd;
796
797 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
798 scmd = ioc->scsi_lookup[smid - 1].scmd;
799 ioc->scsi_lookup[smid - 1].scmd = NULL;
800 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
801 return scmd;
802}
803
804/**
805 * _scsih_scsi_lookup_set - updates scmd entry in lookup
806 * @ioc: per adapter object
807 * @smid: system request message index
808 * @scmd: pointer to scsi command object
809 * Context: This function will acquire ioc->scsi_lookup_lock.
810 *
811 * This will save scmd pointer in the scsi_lookup array.
812 *
813 * Return nothing.
814 */
815static void
816_scsih_scsi_lookup_set(struct MPT2SAS_ADAPTER *ioc, u16 smid,
817 struct scsi_cmnd *scmd)
818{
819 unsigned long flags;
820
821 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
822 ioc->scsi_lookup[smid - 1].scmd = scmd;
823 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
824}
825
826/**
827 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
828 * @ioc: per adapter object
829 * @smid: system request message index
830 * @scmd: pointer to scsi command object
831 * Context: This function will acquire ioc->scsi_lookup_lock.
832 *
833 * This will search for a scmd pointer in the scsi_lookup array,
834 * returning the revelent smid. A returned value of zero means invalid.
835 */
836static u16
837_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
838 *scmd)
839{
840 u16 smid;
841 unsigned long flags;
842 int i;
843
844 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
845 smid = 0;
846 for (i = 0; i < ioc->request_depth; i++) {
847 if (ioc->scsi_lookup[i].scmd == scmd) {
848 smid = i + 1;
849 goto out;
850 }
851 }
852 out:
853 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
854 return smid;
855}
856
857/**
858 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
859 * @ioc: per adapter object
860 * @id: target id
861 * @channel: channel
862 * Context: This function will acquire ioc->scsi_lookup_lock.
863 *
864 * This will search for a matching channel:id in the scsi_lookup array,
865 * returning 1 if found.
866 */
867static u8
868_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
869 int channel)
870{
871 u8 found;
872 unsigned long flags;
873 int i;
874
875 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
876 found = 0;
877 for (i = 0 ; i < ioc->request_depth; i++) {
878 if (ioc->scsi_lookup[i].scmd &&
879 (ioc->scsi_lookup[i].scmd->device->id == id &&
880 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
881 found = 1;
882 goto out;
883 }
884 }
885 out:
886 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
887 return found;
888}
889
890/**
891 * _scsih_get_chain_buffer_dma - obtain block of chains (dma address)
892 * @ioc: per adapter object
893 * @smid: system request message index
894 *
895 * Returns phys pointer to chain buffer.
896 */
897static dma_addr_t
898_scsih_get_chain_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
899{
900 return ioc->chain_dma + ((smid - 1) * (ioc->request_sz *
901 ioc->chains_needed_per_io));
902}
903
904/**
905 * _scsih_get_chain_buffer - obtain block of chains assigned to a mf request
906 * @ioc: per adapter object
907 * @smid: system request message index
908 *
909 * Returns virt pointer to chain buffer.
910 */
911static void *
912_scsih_get_chain_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
913{
914 return (void *)(ioc->chain + ((smid - 1) * (ioc->request_sz *
915 ioc->chains_needed_per_io)));
916}
917
918/**
919 * _scsih_build_scatter_gather - main sg creation routine
920 * @ioc: per adapter object
921 * @scmd: scsi command
922 * @smid: system request message index
923 * Context: none.
924 *
925 * The main routine that builds scatter gather table from a given
926 * scsi request sent via the .queuecommand main handler.
927 *
928 * Returns 0 success, anything else error
929 */
930static int
931_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
932 struct scsi_cmnd *scmd, u16 smid)
933{
934 Mpi2SCSIIORequest_t *mpi_request;
935 dma_addr_t chain_dma;
936 struct scatterlist *sg_scmd;
937 void *sg_local, *chain;
938 u32 chain_offset;
939 u32 chain_length;
940 u32 chain_flags;
941 u32 sges_left;
942 u32 sges_in_segment;
943 u32 sgl_flags;
944 u32 sgl_flags_last_element;
945 u32 sgl_flags_end_buffer;
946
947 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
948
949 /* init scatter gather flags */
950 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
951 if (scmd->sc_data_direction == DMA_TO_DEVICE)
952 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
953 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
954 << MPI2_SGE_FLAGS_SHIFT;
955 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
956 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
957 << MPI2_SGE_FLAGS_SHIFT;
958 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
959
960 sg_scmd = scsi_sglist(scmd);
961 sges_left = scsi_dma_map(scmd);
962 if (!sges_left) {
963 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
964 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
965 return -ENOMEM;
966 }
967
968 sg_local = &mpi_request->SGL;
969 sges_in_segment = ioc->max_sges_in_main_message;
970 if (sges_left <= sges_in_segment)
971 goto fill_in_last_segment;
972
973 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
974 (sges_in_segment * ioc->sge_size))/4;
975
976 /* fill in main message segment when there is a chain following */
977 while (sges_in_segment) {
978 if (sges_in_segment == 1)
979 ioc->base_add_sg_single(sg_local,
980 sgl_flags_last_element | sg_dma_len(sg_scmd),
981 sg_dma_address(sg_scmd));
982 else
983 ioc->base_add_sg_single(sg_local, sgl_flags |
984 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
985 sg_scmd = sg_next(sg_scmd);
986 sg_local += ioc->sge_size;
987 sges_left--;
988 sges_in_segment--;
989 }
990
991 /* initializing the chain flags and pointers */
992 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
993 chain = _scsih_get_chain_buffer(ioc, smid);
994 chain_dma = _scsih_get_chain_buffer_dma(ioc, smid);
995 do {
996 sges_in_segment = (sges_left <=
997 ioc->max_sges_in_chain_message) ? sges_left :
998 ioc->max_sges_in_chain_message;
999 chain_offset = (sges_left == sges_in_segment) ?
1000 0 : (sges_in_segment * ioc->sge_size)/4;
1001 chain_length = sges_in_segment * ioc->sge_size;
1002 if (chain_offset) {
1003 chain_offset = chain_offset <<
1004 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1005 chain_length += ioc->sge_size;
1006 }
1007 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1008 chain_length, chain_dma);
1009 sg_local = chain;
1010 if (!chain_offset)
1011 goto fill_in_last_segment;
1012
1013 /* fill in chain segments */
1014 while (sges_in_segment) {
1015 if (sges_in_segment == 1)
1016 ioc->base_add_sg_single(sg_local,
1017 sgl_flags_last_element |
1018 sg_dma_len(sg_scmd),
1019 sg_dma_address(sg_scmd));
1020 else
1021 ioc->base_add_sg_single(sg_local, sgl_flags |
1022 sg_dma_len(sg_scmd),
1023 sg_dma_address(sg_scmd));
1024 sg_scmd = sg_next(sg_scmd);
1025 sg_local += ioc->sge_size;
1026 sges_left--;
1027 sges_in_segment--;
1028 }
1029
1030 chain_dma += ioc->request_sz;
1031 chain += ioc->request_sz;
1032 } while (1);
1033
1034
1035 fill_in_last_segment:
1036
1037 /* fill the last segment */
1038 while (sges_left) {
1039 if (sges_left == 1)
1040 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1041 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1042 else
1043 ioc->base_add_sg_single(sg_local, sgl_flags |
1044 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1045 sg_scmd = sg_next(sg_scmd);
1046 sg_local += ioc->sge_size;
1047 sges_left--;
1048 }
1049
1050 return 0;
1051}
1052
1053/**
1054 * scsih_change_queue_depth - setting device queue depth
1055 * @sdev: scsi device struct
1056 * @qdepth: requested queue depth
1057 *
1058 * Returns queue depth.
1059 */
1060static int
1061scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
1062{
1063 struct Scsi_Host *shost = sdev->host;
1064 int max_depth;
1065 int tag_type;
1066
1067 max_depth = shost->can_queue;
1068 if (!sdev->tagged_supported)
1069 max_depth = 1;
1070 if (qdepth > max_depth)
1071 qdepth = max_depth;
1072 tag_type = (qdepth == 1) ? 0 : MSG_SIMPLE_TAG;
1073 scsi_adjust_queue_depth(sdev, tag_type, qdepth);
1074
1075 if (sdev->inquiry_len > 7)
1076 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1077 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1078 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1079 sdev->ordered_tags, sdev->scsi_level,
1080 (sdev->inquiry[7] & 2) >> 1);
1081
1082 return sdev->queue_depth;
1083}
1084
1085/**
1086 * scsih_change_queue_depth - changing device queue tag type
1087 * @sdev: scsi device struct
1088 * @tag_type: requested tag type
1089 *
1090 * Returns queue tag type.
1091 */
1092static int
1093scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
1094{
1095 if (sdev->tagged_supported) {
1096 scsi_set_tag_type(sdev, tag_type);
1097 if (tag_type)
1098 scsi_activate_tcq(sdev, sdev->queue_depth);
1099 else
1100 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1101 } else
1102 tag_type = 0;
1103
1104 return tag_type;
1105}
1106
1107/**
1108 * scsih_target_alloc - target add routine
1109 * @starget: scsi target struct
1110 *
1111 * Returns 0 if ok. Any other return is assumed to be an error and
1112 * the device is ignored.
1113 */
1114static int
1115scsih_target_alloc(struct scsi_target *starget)
1116{
1117 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1118 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1119 struct MPT2SAS_TARGET *sas_target_priv_data;
1120 struct _sas_device *sas_device;
1121 struct _raid_device *raid_device;
1122 unsigned long flags;
1123 struct sas_rphy *rphy;
1124
1125 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1126 if (!sas_target_priv_data)
1127 return -ENOMEM;
1128
1129 starget->hostdata = sas_target_priv_data;
1130 sas_target_priv_data->starget = starget;
1131 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1132
1133 /* RAID volumes */
1134 if (starget->channel == RAID_CHANNEL) {
1135 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1136 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1137 starget->channel);
1138 if (raid_device) {
1139 sas_target_priv_data->handle = raid_device->handle;
1140 sas_target_priv_data->sas_address = raid_device->wwid;
1141 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1142 raid_device->starget = starget;
1143 }
1144 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1145 return 0;
1146 }
1147
1148 /* sas/sata devices */
1149 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1150 rphy = dev_to_rphy(starget->dev.parent);
1151 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1152 rphy->identify.sas_address);
1153
1154 if (sas_device) {
1155 sas_target_priv_data->handle = sas_device->handle;
1156 sas_target_priv_data->sas_address = sas_device->sas_address;
1157 sas_device->starget = starget;
1158 sas_device->id = starget->id;
1159 sas_device->channel = starget->channel;
1160 if (sas_device->hidden_raid_component)
1161 sas_target_priv_data->flags |=
1162 MPT_TARGET_FLAGS_RAID_COMPONENT;
1163 }
1164 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1165
1166 return 0;
1167}
1168
1169/**
1170 * scsih_target_destroy - target destroy routine
1171 * @starget: scsi target struct
1172 *
1173 * Returns nothing.
1174 */
1175static void
1176scsih_target_destroy(struct scsi_target *starget)
1177{
1178 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1179 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1180 struct MPT2SAS_TARGET *sas_target_priv_data;
1181 struct _sas_device *sas_device;
1182 struct _raid_device *raid_device;
1183 unsigned long flags;
1184 struct sas_rphy *rphy;
1185
1186 sas_target_priv_data = starget->hostdata;
1187 if (!sas_target_priv_data)
1188 return;
1189
1190 if (starget->channel == RAID_CHANNEL) {
1191 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1192 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1193 starget->channel);
1194 if (raid_device) {
1195 raid_device->starget = NULL;
1196 raid_device->sdev = NULL;
1197 }
1198 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1199 goto out;
1200 }
1201
1202 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1203 rphy = dev_to_rphy(starget->dev.parent);
1204 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1205 rphy->identify.sas_address);
1206 if (sas_device)
1207 sas_device->starget = NULL;
1208
1209 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1210
1211 out:
1212 kfree(sas_target_priv_data);
1213 starget->hostdata = NULL;
1214}
1215
1216/**
1217 * scsih_slave_alloc - device add routine
1218 * @sdev: scsi device struct
1219 *
1220 * Returns 0 if ok. Any other return is assumed to be an error and
1221 * the device is ignored.
1222 */
1223static int
1224scsih_slave_alloc(struct scsi_device *sdev)
1225{
1226 struct Scsi_Host *shost;
1227 struct MPT2SAS_ADAPTER *ioc;
1228 struct MPT2SAS_TARGET *sas_target_priv_data;
1229 struct MPT2SAS_DEVICE *sas_device_priv_data;
1230 struct scsi_target *starget;
1231 struct _raid_device *raid_device;
1232 struct _sas_device *sas_device;
1233 unsigned long flags;
1234
1235 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1236 if (!sas_device_priv_data)
1237 return -ENOMEM;
1238
1239 sas_device_priv_data->lun = sdev->lun;
1240 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1241
1242 starget = scsi_target(sdev);
1243 sas_target_priv_data = starget->hostdata;
1244 sas_target_priv_data->num_luns++;
1245 sas_device_priv_data->sas_target = sas_target_priv_data;
1246 sdev->hostdata = sas_device_priv_data;
1247 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1248 sdev->no_uld_attach = 1;
1249
1250 shost = dev_to_shost(&starget->dev);
1251 ioc = shost_priv(shost);
1252 if (starget->channel == RAID_CHANNEL) {
1253 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1254 raid_device = _scsih_raid_device_find_by_id(ioc,
1255 starget->id, starget->channel);
1256 if (raid_device)
1257 raid_device->sdev = sdev; /* raid is single lun */
1258 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1259 } else {
1260 /* set TLR bit for SSP devices */
1261 if (!(ioc->facts.IOCCapabilities &
1262 MPI2_IOCFACTS_CAPABILITY_TLR))
1263 goto out;
1264 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1265 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1266 sas_device_priv_data->sas_target->sas_address);
1267 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1268 if (sas_device && sas_device->device_info &
1269 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
1270 sas_device_priv_data->flags |= MPT_DEVICE_TLR_ON;
1271 }
1272
1273 out:
1274 return 0;
1275}
1276
1277/**
1278 * scsih_slave_destroy - device destroy routine
1279 * @sdev: scsi device struct
1280 *
1281 * Returns nothing.
1282 */
1283static void
1284scsih_slave_destroy(struct scsi_device *sdev)
1285{
1286 struct MPT2SAS_TARGET *sas_target_priv_data;
1287 struct scsi_target *starget;
1288
1289 if (!sdev->hostdata)
1290 return;
1291
1292 starget = scsi_target(sdev);
1293 sas_target_priv_data = starget->hostdata;
1294 sas_target_priv_data->num_luns--;
1295 kfree(sdev->hostdata);
1296 sdev->hostdata = NULL;
1297}
1298
1299/**
1300 * scsih_display_sata_capabilities - sata capabilities
1301 * @ioc: per adapter object
1302 * @sas_device: the sas_device object
1303 * @sdev: scsi device struct
1304 */
1305static void
1306scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
1307 struct _sas_device *sas_device, struct scsi_device *sdev)
1308{
1309 Mpi2ConfigReply_t mpi_reply;
1310 Mpi2SasDevicePage0_t sas_device_pg0;
1311 u32 ioc_status;
1312 u16 flags;
1313 u32 device_info;
1314
1315 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1316 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1317 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1318 ioc->name, __FILE__, __LINE__, __func__);
1319 return;
1320 }
1321
1322 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1323 MPI2_IOCSTATUS_MASK;
1324 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1325 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1326 ioc->name, __FILE__, __LINE__, __func__);
1327 return;
1328 }
1329
1330 flags = le16_to_cpu(sas_device_pg0.Flags);
1331 device_info = le16_to_cpu(sas_device_pg0.DeviceInfo);
1332
1333 sdev_printk(KERN_INFO, sdev,
1334 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1335 "sw_preserve(%s)\n",
1336 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1337 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1338 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1339 "n",
1340 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1341 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1342 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1343}
1344
1345/**
1346 * _scsih_get_volume_capabilities - volume capabilities
1347 * @ioc: per adapter object
1348 * @sas_device: the raid_device object
1349 */
1350static void
1351_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1352 struct _raid_device *raid_device)
1353{
1354 Mpi2RaidVolPage0_t *vol_pg0;
1355 Mpi2RaidPhysDiskPage0_t pd_pg0;
1356 Mpi2SasDevicePage0_t sas_device_pg0;
1357 Mpi2ConfigReply_t mpi_reply;
1358 u16 sz;
1359 u8 num_pds;
1360
1361 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1362 &num_pds)) || !num_pds) {
1363 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1364 ioc->name, __FILE__, __LINE__, __func__);
1365 return;
1366 }
1367
1368 raid_device->num_pds = num_pds;
1369 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1370 sizeof(Mpi2RaidVol0PhysDisk_t));
1371 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1372 if (!vol_pg0) {
1373 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1374 ioc->name, __FILE__, __LINE__, __func__);
1375 return;
1376 }
1377
1378 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1379 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1380 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1381 ioc->name, __FILE__, __LINE__, __func__);
1382 kfree(vol_pg0);
1383 return;
1384 }
1385
1386 raid_device->volume_type = vol_pg0->VolumeType;
1387
1388 /* figure out what the underlying devices are by
1389 * obtaining the device_info bits for the 1st device
1390 */
1391 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1392 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1393 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1394 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1395 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1396 le16_to_cpu(pd_pg0.DevHandle)))) {
1397 raid_device->device_info =
1398 le32_to_cpu(sas_device_pg0.DeviceInfo);
1399 }
1400 }
1401
1402 kfree(vol_pg0);
1403}
1404
1405/**
1406 * scsih_slave_configure - device configure routine.
1407 * @sdev: scsi device struct
1408 *
1409 * Returns 0 if ok. Any other return is assumed to be an error and
1410 * the device is ignored.
1411 */
1412static int
1413scsih_slave_configure(struct scsi_device *sdev)
1414{
1415 struct Scsi_Host *shost = sdev->host;
1416 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1417 struct MPT2SAS_DEVICE *sas_device_priv_data;
1418 struct MPT2SAS_TARGET *sas_target_priv_data;
1419 struct _sas_device *sas_device;
1420 struct _raid_device *raid_device;
1421 unsigned long flags;
1422 int qdepth;
1423 u8 ssp_target = 0;
1424 char *ds = "";
1425 char *r_level = "";
1426
1427 qdepth = 1;
1428 sas_device_priv_data = sdev->hostdata;
1429 sas_device_priv_data->configured_lun = 1;
1430 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1431 sas_target_priv_data = sas_device_priv_data->sas_target;
1432
1433 /* raid volume handling */
1434 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1435
1436 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1437 raid_device = _scsih_raid_device_find_by_handle(ioc,
1438 sas_target_priv_data->handle);
1439 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1440 if (!raid_device) {
1441 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1442 ioc->name, __FILE__, __LINE__, __func__);
1443 return 0;
1444 }
1445
1446 _scsih_get_volume_capabilities(ioc, raid_device);
1447
1448 /* RAID Queue Depth Support
1449 * IS volume = underlying qdepth of drive type, either
1450 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1451 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1452 */
1453 if (raid_device->device_info &
1454 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1455 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1456 ds = "SSP";
1457 } else {
1458 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1459 if (raid_device->device_info &
1460 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1461 ds = "SATA";
1462 else
1463 ds = "STP";
1464 }
1465
1466 switch (raid_device->volume_type) {
1467 case MPI2_RAID_VOL_TYPE_RAID0:
1468 r_level = "RAID0";
1469 break;
1470 case MPI2_RAID_VOL_TYPE_RAID1E:
1471 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1472 r_level = "RAID1E";
1473 break;
1474 case MPI2_RAID_VOL_TYPE_RAID1:
1475 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1476 r_level = "RAID1";
1477 break;
1478 case MPI2_RAID_VOL_TYPE_RAID10:
1479 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1480 r_level = "RAID10";
1481 break;
1482 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1483 default:
1484 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1485 r_level = "RAIDX";
1486 break;
1487 }
1488
1489 sdev_printk(KERN_INFO, sdev, "%s: "
1490 "handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1491 r_level, raid_device->handle,
1492 (unsigned long long)raid_device->wwid,
1493 raid_device->num_pds, ds);
1494 scsih_change_queue_depth(sdev, qdepth);
1495 return 0;
1496 }
1497
1498 /* non-raid handling */
1499 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1500 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1501 sas_device_priv_data->sas_target->sas_address);
1502 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1503 if (sas_device) {
1504 if (sas_target_priv_data->flags &
1505 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1506 mpt2sas_config_get_volume_handle(ioc,
1507 sas_device->handle, &sas_device->volume_handle);
1508 mpt2sas_config_get_volume_wwid(ioc,
1509 sas_device->volume_handle,
1510 &sas_device->volume_wwid);
1511 }
1512 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1513 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1514 ssp_target = 1;
1515 ds = "SSP";
1516 } else {
1517 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1518 if (sas_device->device_info &
1519 MPI2_SAS_DEVICE_INFO_STP_TARGET)
1520 ds = "STP";
1521 else if (sas_device->device_info &
1522 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1523 ds = "SATA";
1524 }
1525
1526 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1527 "sas_addr(0x%016llx), device_name(0x%016llx)\n",
1528 ds, sas_device->handle,
1529 (unsigned long long)sas_device->sas_address,
1530 (unsigned long long)sas_device->device_name);
1531 sdev_printk(KERN_INFO, sdev, "%s: "
1532 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
1533 (unsigned long long) sas_device->enclosure_logical_id,
1534 sas_device->slot);
1535
1536 if (!ssp_target)
1537 scsih_display_sata_capabilities(ioc, sas_device, sdev);
1538 }
1539
1540 scsih_change_queue_depth(sdev, qdepth);
1541
1542 if (ssp_target)
1543 sas_read_port_mode_page(sdev);
1544 return 0;
1545}
1546
1547/**
1548 * scsih_bios_param - fetch head, sector, cylinder info for a disk
1549 * @sdev: scsi device struct
1550 * @bdev: pointer to block device context
1551 * @capacity: device size (in 512 byte sectors)
1552 * @params: three element array to place output:
1553 * params[0] number of heads (max 255)
1554 * params[1] number of sectors (max 63)
1555 * params[2] number of cylinders
1556 *
1557 * Return nothing.
1558 */
1559static int
1560scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1561 sector_t capacity, int params[])
1562{
1563 int heads;
1564 int sectors;
1565 sector_t cylinders;
1566 ulong dummy;
1567
1568 heads = 64;
1569 sectors = 32;
1570
1571 dummy = heads * sectors;
1572 cylinders = capacity;
1573 sector_div(cylinders, dummy);
1574
1575 /*
1576 * Handle extended translation size for logical drives
1577 * > 1Gb
1578 */
1579 if ((ulong)capacity >= 0x200000) {
1580 heads = 255;
1581 sectors = 63;
1582 dummy = heads * sectors;
1583 cylinders = capacity;
1584 sector_div(cylinders, dummy);
1585 }
1586
1587 /* return result */
1588 params[0] = heads;
1589 params[1] = sectors;
1590 params[2] = cylinders;
1591
1592 return 0;
1593}
1594
1595/**
1596 * _scsih_response_code - translation of device response code
1597 * @ioc: per adapter object
1598 * @response_code: response code returned by the device
1599 *
1600 * Return nothing.
1601 */
1602static void
1603_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
1604{
1605 char *desc;
1606
1607 switch (response_code) {
1608 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1609 desc = "task management request completed";
1610 break;
1611 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1612 desc = "invalid frame";
1613 break;
1614 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1615 desc = "task management request not supported";
1616 break;
1617 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1618 desc = "task management request failed";
1619 break;
1620 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1621 desc = "task management request succeeded";
1622 break;
1623 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1624 desc = "invalid lun";
1625 break;
1626 case 0xA:
1627 desc = "overlapped tag attempted";
1628 break;
1629 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1630 desc = "task queued, however not sent to target";
1631 break;
1632 default:
1633 desc = "unknown";
1634 break;
1635 }
1636 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
1637 ioc->name, response_code, desc);
1638}
1639
1640/**
1641 * scsih_tm_done - tm completion routine
1642 * @ioc: per adapter object
1643 * @smid: system request message index
1644 * @VF_ID: virtual function id
1645 * @reply: reply message frame(lower 32bit addr)
1646 * Context: none.
1647 *
1648 * The callback handler when using scsih_issue_tm.
1649 *
1650 * Return nothing.
1651 */
1652static void
1653scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
1654{
1655 MPI2DefaultReply_t *mpi_reply;
1656
1657 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
1658 return;
1659 if (ioc->tm_cmds.smid != smid)
1660 return;
1661 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
1662 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
1663 if (mpi_reply) {
1664 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1665 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
1666 }
1667 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
1668 complete(&ioc->tm_cmds.done);
1669}
1670
1671/**
1672 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
1673 * @ioc: per adapter object
1674 * @handle: device handle
1675 *
1676 * During taskmangement request, we need to freeze the device queue.
1677 */
1678void
1679mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1680{
1681 struct MPT2SAS_DEVICE *sas_device_priv_data;
1682 struct scsi_device *sdev;
1683 u8 skip = 0;
1684
1685 shost_for_each_device(sdev, ioc->shost) {
1686 if (skip)
1687 continue;
1688 sas_device_priv_data = sdev->hostdata;
1689 if (!sas_device_priv_data)
1690 continue;
1691 if (sas_device_priv_data->sas_target->handle == handle) {
1692 sas_device_priv_data->sas_target->tm_busy = 1;
1693 skip = 1;
1694 ioc->ignore_loginfos = 1;
1695 }
1696 }
1697}
1698
1699/**
1700 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
1701 * @ioc: per adapter object
1702 * @handle: device handle
1703 *
1704 * During taskmangement request, we need to freeze the device queue.
1705 */
1706void
1707mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
1708{
1709 struct MPT2SAS_DEVICE *sas_device_priv_data;
1710 struct scsi_device *sdev;
1711 u8 skip = 0;
1712
1713 shost_for_each_device(sdev, ioc->shost) {
1714 if (skip)
1715 continue;
1716 sas_device_priv_data = sdev->hostdata;
1717 if (!sas_device_priv_data)
1718 continue;
1719 if (sas_device_priv_data->sas_target->handle == handle) {
1720 sas_device_priv_data->sas_target->tm_busy = 0;
1721 skip = 1;
1722 ioc->ignore_loginfos = 0;
1723 }
1724 }
1725}
1726
1727/**
1728 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
1729 * @ioc: per adapter struct
1730 * @device_handle: device handle
1731 * @lun: lun number
1732 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
1733 * @smid_task: smid assigned to the task
1734 * @timeout: timeout in seconds
1735 * Context: The calling function needs to acquire the tm_cmds.mutex
1736 *
1737 * A generic API for sending task management requests to firmware.
1738 *
1739 * The ioc->tm_cmds.status flag should be MPT2_CMD_NOT_USED before calling
1740 * this API.
1741 *
1742 * The callback index is set inside `ioc->tm_cb_idx`.
1743 *
1744 * Return nothing.
1745 */
1746void
1747mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint lun,
1748 u8 type, u16 smid_task, ulong timeout)
1749{
1750 Mpi2SCSITaskManagementRequest_t *mpi_request;
1751 Mpi2SCSITaskManagementReply_t *mpi_reply;
1752 u16 smid = 0;
1753 u32 ioc_state;
1754 unsigned long timeleft;
1755 u8 VF_ID = 0;
1756 unsigned long flags;
1757
1758 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
1759 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED ||
1760 ioc->shost_recovery) {
1761 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1762 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1763 __func__, ioc->name);
1764 return;
1765 }
1766 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1767
1768 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
1769 if (ioc_state & MPI2_DOORBELL_USED) {
1770 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
1771 "active!\n", ioc->name));
1772 goto issue_host_reset;
1773 }
1774
1775 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
1776 mpt2sas_base_fault_info(ioc, ioc_state &
1777 MPI2_DOORBELL_DATA_MASK);
1778 goto issue_host_reset;
1779 }
1780
1781 smid = mpt2sas_base_get_smid(ioc, ioc->tm_cb_idx);
1782 if (!smid) {
1783 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1784 ioc->name, __func__);
1785 return;
1786 }
1787
1788 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
1789 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type, smid));
1790 ioc->tm_cmds.status = MPT2_CMD_PENDING;
1791 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1792 ioc->tm_cmds.smid = smid;
1793 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
1794 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1795 mpi_request->DevHandle = cpu_to_le16(handle);
1796 mpi_request->TaskType = type;
1797 mpi_request->TaskMID = cpu_to_le16(smid_task);
1798 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
1799 mpt2sas_scsih_set_tm_flag(ioc, handle);
1800 mpt2sas_base_put_smid_hi_priority(ioc, smid, VF_ID);
1801 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
1802 mpt2sas_scsih_clear_tm_flag(ioc, handle);
1803 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
1804 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
1805 ioc->name, __func__);
1806 _debug_dump_mf(mpi_request,
1807 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
1808 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET))
1809 goto issue_host_reset;
1810 }
1811
1812 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
1813 mpi_reply = ioc->tm_cmds.reply;
1814 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
1815 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
1816 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
1817 le32_to_cpu(mpi_reply->IOCLogInfo),
1818 le32_to_cpu(mpi_reply->TerminationCount)));
1819 if (ioc->logging_level & MPT_DEBUG_TM)
1820 _scsih_response_code(ioc, mpi_reply->ResponseCode);
1821 }
1822 return;
1823 issue_host_reset:
1824 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, FORCE_BIG_HAMMER);
1825}
1826
1827/**
1828 * scsih_abort - eh threads main abort routine
1829 * @sdev: scsi device struct
1830 *
1831 * Returns SUCCESS if command aborted else FAILED
1832 */
1833static int
1834scsih_abort(struct scsi_cmnd *scmd)
1835{
1836 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1837 struct MPT2SAS_DEVICE *sas_device_priv_data;
1838 u16 smid;
1839 u16 handle;
1840 int r;
1841 struct scsi_cmnd *scmd_lookup;
1842
1843 printk(MPT2SAS_INFO_FMT "attempting task abort! scmd(%p)\n",
1844 ioc->name, scmd);
1845 scsi_print_command(scmd);
1846
1847 sas_device_priv_data = scmd->device->hostdata;
1848 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1849 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1850 ioc->name, scmd);
1851 scmd->result = DID_NO_CONNECT << 16;
1852 scmd->scsi_done(scmd);
1853 r = SUCCESS;
1854 goto out;
1855 }
1856
1857 /* search for the command */
1858 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
1859 if (!smid) {
1860 scmd->result = DID_RESET << 16;
1861 r = SUCCESS;
1862 goto out;
1863 }
1864
1865 /* for hidden raid components and volumes this is not supported */
1866 if (sas_device_priv_data->sas_target->flags &
1867 MPT_TARGET_FLAGS_RAID_COMPONENT ||
1868 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
1869 scmd->result = DID_RESET << 16;
1870 r = FAILED;
1871 goto out;
1872 }
1873
1874 mutex_lock(&ioc->tm_cmds.mutex);
1875 handle = sas_device_priv_data->sas_target->handle;
1876 mpt2sas_scsih_issue_tm(ioc, handle, sas_device_priv_data->lun,
1877 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
1878
1879 /* sanity check - see whether command actually completed */
1880 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid);
1881 if (scmd_lookup && (scmd_lookup->serial_number == scmd->serial_number))
1882 r = FAILED;
1883 else
1884 r = SUCCESS;
1885 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1886 mutex_unlock(&ioc->tm_cmds.mutex);
1887
1888 out:
1889 printk(MPT2SAS_INFO_FMT "task abort: %s scmd(%p)\n",
1890 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1891 return r;
1892}
1893
1894
1895/**
1896 * scsih_dev_reset - eh threads main device reset routine
1897 * @sdev: scsi device struct
1898 *
1899 * Returns SUCCESS if command aborted else FAILED
1900 */
1901static int
1902scsih_dev_reset(struct scsi_cmnd *scmd)
1903{
1904 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1905 struct MPT2SAS_DEVICE *sas_device_priv_data;
1906 struct _sas_device *sas_device;
1907 unsigned long flags;
1908 u16 handle;
1909 int r;
1910
1911 printk(MPT2SAS_INFO_FMT "attempting target reset! scmd(%p)\n",
1912 ioc->name, scmd);
1913 scsi_print_command(scmd);
1914
1915 sas_device_priv_data = scmd->device->hostdata;
1916 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
1917 printk(MPT2SAS_INFO_FMT "device been deleted! scmd(%p)\n",
1918 ioc->name, scmd);
1919 scmd->result = DID_NO_CONNECT << 16;
1920 scmd->scsi_done(scmd);
1921 r = SUCCESS;
1922 goto out;
1923 }
1924
1925 /* for hidden raid components obtain the volume_handle */
1926 handle = 0;
1927 if (sas_device_priv_data->sas_target->flags &
1928 MPT_TARGET_FLAGS_RAID_COMPONENT) {
1929 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1930 sas_device = _scsih_sas_device_find_by_handle(ioc,
1931 sas_device_priv_data->sas_target->handle);
1932 if (sas_device)
1933 handle = sas_device->volume_handle;
1934 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1935 } else
1936 handle = sas_device_priv_data->sas_target->handle;
1937
1938 if (!handle) {
1939 scmd->result = DID_RESET << 16;
1940 r = FAILED;
1941 goto out;
1942 }
1943
1944 mutex_lock(&ioc->tm_cmds.mutex);
1945 mpt2sas_scsih_issue_tm(ioc, handle, 0,
1946 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
1947
1948 /*
1949 * sanity check see whether all commands to this target been
1950 * completed
1951 */
1952 if (_scsih_scsi_lookup_find_by_target(ioc, scmd->device->id,
1953 scmd->device->channel))
1954 r = FAILED;
1955 else
1956 r = SUCCESS;
1957 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1958 mutex_unlock(&ioc->tm_cmds.mutex);
1959
1960 out:
1961 printk(MPT2SAS_INFO_FMT "target reset: %s scmd(%p)\n",
1962 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1963 return r;
1964}
1965
1966/**
1967 * scsih_abort - eh threads main host reset routine
1968 * @sdev: scsi device struct
1969 *
1970 * Returns SUCCESS if command aborted else FAILED
1971 */
1972static int
1973scsih_host_reset(struct scsi_cmnd *scmd)
1974{
1975 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
1976 int r, retval;
1977
1978 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
1979 ioc->name, scmd);
1980 scsi_print_command(scmd);
1981
1982 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1983 FORCE_BIG_HAMMER);
1984 r = (retval < 0) ? FAILED : SUCCESS;
1985 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
1986 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
1987
1988 return r;
1989}
1990
1991/**
1992 * _scsih_fw_event_add - insert and queue up fw_event
1993 * @ioc: per adapter object
1994 * @fw_event: object describing the event
1995 * Context: This function will acquire ioc->fw_event_lock.
1996 *
1997 * This adds the firmware event object into link list, then queues it up to
1998 * be processed from user context.
1999 *
2000 * Return nothing.
2001 */
2002static void
2003_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2004{
2005 unsigned long flags;
2006
2007 if (ioc->firmware_event_thread == NULL)
2008 return;
2009
2010 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2011 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2012 INIT_DELAYED_WORK(&fw_event->work, _firmware_event_work);
2013 queue_delayed_work(ioc->firmware_event_thread, &fw_event->work, 1);
2014 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2015}
2016
2017/**
2018 * _scsih_fw_event_free - delete fw_event
2019 * @ioc: per adapter object
2020 * @fw_event: object describing the event
2021 * Context: This function will acquire ioc->fw_event_lock.
2022 *
2023 * This removes firmware event object from link list, frees associated memory.
2024 *
2025 * Return nothing.
2026 */
2027static void
2028_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2029 *fw_event)
2030{
2031 unsigned long flags;
2032
2033 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2034 list_del(&fw_event->list);
2035 kfree(fw_event->event_data);
2036 kfree(fw_event);
2037 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2038}
2039
2040/**
2041 * _scsih_fw_event_add - requeue an event
2042 * @ioc: per adapter object
2043 * @fw_event: object describing the event
2044 * Context: This function will acquire ioc->fw_event_lock.
2045 *
2046 * Return nothing.
2047 */
2048static void
2049_scsih_fw_event_requeue(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2050 *fw_event, unsigned long delay)
2051{
2052 unsigned long flags;
2053 if (ioc->firmware_event_thread == NULL)
2054 return;
2055
2056 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2057 queue_delayed_work(ioc->firmware_event_thread, &fw_event->work, delay);
2058 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2059}
2060
2061/**
2062 * _scsih_fw_event_off - turn flag off preventing event handling
2063 * @ioc: per adapter object
2064 *
2065 * Used to prevent handling of firmware events during adapter reset
2066 * driver unload.
2067 *
2068 * Return nothing.
2069 */
2070static void
2071_scsih_fw_event_off(struct MPT2SAS_ADAPTER *ioc)
2072{
2073 unsigned long flags;
2074
2075 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2076 ioc->fw_events_off = 1;
2077 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2078
2079}
2080
2081/**
2082 * _scsih_fw_event_on - turn flag on allowing firmware event handling
2083 * @ioc: per adapter object
2084 *
2085 * Returns nothing.
2086 */
2087static void
2088_scsih_fw_event_on(struct MPT2SAS_ADAPTER *ioc)
2089{
2090 unsigned long flags;
2091
2092 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2093 ioc->fw_events_off = 0;
2094 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2095}
2096
2097/**
2098 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2099 * @ioc: per adapter object
2100 * @handle: device handle
2101 *
2102 * During device pull we need to appropiately set the sdev state.
2103 */
2104static void
2105_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2106{
2107 struct MPT2SAS_DEVICE *sas_device_priv_data;
2108 struct scsi_device *sdev;
2109
2110 shost_for_each_device(sdev, ioc->shost) {
2111 sas_device_priv_data = sdev->hostdata;
2112 if (!sas_device_priv_data)
2113 continue;
2114 if (!sas_device_priv_data->block)
2115 continue;
2116 if (sas_device_priv_data->sas_target->handle == handle) {
2117 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2118 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2119 "handle(0x%04x)\n", ioc->name, handle));
2120 sas_device_priv_data->block = 0;
2121 scsi_device_set_state(sdev, SDEV_RUNNING);
2122 }
2123 }
2124}
2125
2126/**
2127 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2128 * @ioc: per adapter object
2129 * @handle: device handle
2130 *
2131 * During device pull we need to appropiately set the sdev state.
2132 */
2133static void
2134_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2135{
2136 struct MPT2SAS_DEVICE *sas_device_priv_data;
2137 struct scsi_device *sdev;
2138
2139 shost_for_each_device(sdev, ioc->shost) {
2140 sas_device_priv_data = sdev->hostdata;
2141 if (!sas_device_priv_data)
2142 continue;
2143 if (sas_device_priv_data->block)
2144 continue;
2145 if (sas_device_priv_data->sas_target->handle == handle) {
2146 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2147 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2148 "handle(0x%04x)\n", ioc->name, handle));
2149 sas_device_priv_data->block = 1;
2150 scsi_device_set_state(sdev, SDEV_BLOCK);
2151 }
2152 }
2153}
2154
2155/**
2156 * _scsih_block_io_to_children_attached_to_ex
2157 * @ioc: per adapter object
2158 * @sas_expander: the sas_device object
2159 *
2160 * This routine set sdev state to SDEV_BLOCK for all devices
2161 * attached to this expander. This function called when expander is
2162 * pulled.
2163 */
2164static void
2165_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2166 struct _sas_node *sas_expander)
2167{
2168 struct _sas_port *mpt2sas_port;
2169 struct _sas_device *sas_device;
2170 struct _sas_node *expander_sibling;
2171 unsigned long flags;
2172
2173 if (!sas_expander)
2174 return;
2175
2176 list_for_each_entry(mpt2sas_port,
2177 &sas_expander->sas_port_list, port_list) {
2178 if (mpt2sas_port->remote_identify.device_type ==
2179 SAS_END_DEVICE) {
2180 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2181 sas_device =
2182 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2183 mpt2sas_port->remote_identify.sas_address);
2184 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2185 if (!sas_device)
2186 continue;
2187 _scsih_block_io_device(ioc, sas_device->handle);
2188 }
2189 }
2190
2191 list_for_each_entry(mpt2sas_port,
2192 &sas_expander->sas_port_list, port_list) {
2193
2194 if (mpt2sas_port->remote_identify.device_type ==
2195 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
2196 mpt2sas_port->remote_identify.device_type ==
2197 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
2198
2199 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2200 expander_sibling =
2201 mpt2sas_scsih_expander_find_by_sas_address(
2202 ioc, mpt2sas_port->remote_identify.sas_address);
2203 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2204 _scsih_block_io_to_children_attached_to_ex(ioc,
2205 expander_sibling);
2206 }
2207 }
2208}
2209
2210/**
2211 * _scsih_block_io_to_children_attached_directly
2212 * @ioc: per adapter object
2213 * @event_data: topology change event data
2214 *
2215 * This routine set sdev state to SDEV_BLOCK for all devices
2216 * direct attached during device pull.
2217 */
2218static void
2219_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2220 Mpi2EventDataSasTopologyChangeList_t *event_data)
2221{
2222 int i;
2223 u16 handle;
2224 u16 reason_code;
2225 u8 phy_number;
2226
2227 for (i = 0; i < event_data->NumEntries; i++) {
2228 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2229 if (!handle)
2230 continue;
2231 phy_number = event_data->StartPhyNum + i;
2232 reason_code = event_data->PHY[i].PhyStatus &
2233 MPI2_EVENT_SAS_TOPO_RC_MASK;
2234 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2235 _scsih_block_io_device(ioc, handle);
2236 }
2237}
2238
2239/**
2240 * _scsih_check_topo_delete_events - sanity check on topo events
2241 * @ioc: per adapter object
2242 * @event_data: the event data payload
2243 *
2244 * This routine added to better handle cable breaker.
2245 *
2246 * This handles the case where driver recieves multiple expander
2247 * add and delete events in a single shot. When there is a delete event
2248 * the routine will void any pending add events waiting in the event queue.
2249 *
2250 * Return nothing.
2251 */
2252static void
2253_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
2254 Mpi2EventDataSasTopologyChangeList_t *event_data)
2255{
2256 struct fw_event_work *fw_event;
2257 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
2258 u16 expander_handle;
2259 struct _sas_node *sas_expander;
2260 unsigned long flags;
2261
2262 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
2263 if (expander_handle < ioc->sas_hba.num_phys) {
2264 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2265 return;
2266 }
2267
2268 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
2269 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
2270 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2271 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
2272 expander_handle);
2273 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2274 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
2275 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
2276 _scsih_block_io_to_children_attached_directly(ioc, event_data);
2277
2278 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
2279 return;
2280
2281 /* mark ignore flag for pending events */
2282 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2283 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
2284 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
2285 fw_event->ignore)
2286 continue;
2287 local_event_data = fw_event->event_data;
2288 if (local_event_data->ExpStatus ==
2289 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
2290 local_event_data->ExpStatus ==
2291 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
2292 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
2293 expander_handle) {
2294 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2295 "setting ignoring flag\n", ioc->name));
2296 fw_event->ignore = 1;
2297 }
2298 }
2299 }
2300 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2301}
2302
2303/**
2304 * _scsih_queue_rescan - queue a topology rescan from user context
2305 * @ioc: per adapter object
2306 *
2307 * Return nothing.
2308 */
2309static void
2310_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
2311{
2312 struct fw_event_work *fw_event;
2313
2314 if (ioc->wait_for_port_enable_to_complete)
2315 return;
2316 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2317 if (!fw_event)
2318 return;
2319 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2320 fw_event->ioc = ioc;
2321 _scsih_fw_event_add(ioc, fw_event);
2322}
2323
2324/**
2325 * _scsih_flush_running_cmds - completing outstanding commands.
2326 * @ioc: per adapter object
2327 *
2328 * The flushing out of all pending scmd commands following host reset,
2329 * where all IO is dropped to the floor.
2330 *
2331 * Return nothing.
2332 */
2333static void
2334_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
2335{
2336 struct scsi_cmnd *scmd;
2337 u16 smid;
2338 u16 count = 0;
2339
2340 for (smid = 1; smid <= ioc->request_depth; smid++) {
2341 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2342 if (!scmd)
2343 continue;
2344 count++;
2345 mpt2sas_base_free_smid(ioc, smid);
2346 scsi_dma_unmap(scmd);
2347 scmd->result = DID_RESET << 16;
2348 scmd->scsi_done(scmd);
2349 }
2350 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
2351 ioc->name, count));
2352}
2353
2354/**
2355 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
2356 * @ioc: per adapter object
2357 * @reset_phase: phase
2358 *
2359 * The handler for doing any required cleanup or initialization.
2360 *
2361 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
2362 * MPT2_IOC_DONE_RESET
2363 *
2364 * Return nothing.
2365 */
2366void
2367mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
2368{
2369 switch (reset_phase) {
2370 case MPT2_IOC_PRE_RESET:
2371 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2372 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
2373 _scsih_fw_event_off(ioc);
2374 break;
2375 case MPT2_IOC_AFTER_RESET:
2376 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2377 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
2378 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
2379 ioc->tm_cmds.status |= MPT2_CMD_RESET;
2380 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
2381 complete(&ioc->tm_cmds.done);
2382 }
2383 _scsih_fw_event_on(ioc);
2384 _scsih_flush_running_cmds(ioc);
2385 break;
2386 case MPT2_IOC_DONE_RESET:
2387 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2388 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
2389 _scsih_queue_rescan(ioc);
2390 break;
2391 }
2392}
2393
2394/**
2395 * scsih_qcmd - main scsi request entry point
2396 * @scmd: pointer to scsi command object
2397 * @done: function pointer to be invoked on completion
2398 *
2399 * The callback index is set inside `ioc->scsi_io_cb_idx`.
2400 *
2401 * Returns 0 on success. If there's a failure, return either:
2402 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
2403 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
2404 */
2405static int
2406scsih_qcmd(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
2407{
2408 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2409 struct MPT2SAS_DEVICE *sas_device_priv_data;
2410 struct MPT2SAS_TARGET *sas_target_priv_data;
2411 Mpi2SCSIIORequest_t *mpi_request;
2412 u32 mpi_control;
2413 u16 smid;
2414 unsigned long flags;
2415
2416 scmd->scsi_done = done;
2417 sas_device_priv_data = scmd->device->hostdata;
2418 if (!sas_device_priv_data) {
2419 scmd->result = DID_NO_CONNECT << 16;
2420 scmd->scsi_done(scmd);
2421 return 0;
2422 }
2423
2424 sas_target_priv_data = sas_device_priv_data->sas_target;
2425 if (!sas_target_priv_data || sas_target_priv_data->handle ==
2426 MPT2SAS_INVALID_DEVICE_HANDLE || sas_target_priv_data->deleted) {
2427 scmd->result = DID_NO_CONNECT << 16;
2428 scmd->scsi_done(scmd);
2429 return 0;
2430 }
2431
2432 /* see if we are busy with task managment stuff */
2433 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
2434 if (sas_target_priv_data->tm_busy ||
2435 ioc->shost_recovery || ioc->ioc_link_reset_in_progress) {
2436 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
2437 return SCSI_MLQUEUE_HOST_BUSY;
2438 }
2439 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
2440
2441 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2442 mpi_control = MPI2_SCSIIO_CONTROL_READ;
2443 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
2444 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
2445 else
2446 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
2447
2448 /* set tags */
2449 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
2450 if (scmd->device->tagged_supported) {
2451 if (scmd->device->ordered_tags)
2452 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
2453 else
2454 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2455 } else
2456/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
2457/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
2458 */
2459 mpi_control |= (0x500);
2460
2461 } else
2462 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
2463
2464 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON))
2465 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
2466
2467 smid = mpt2sas_base_get_smid(ioc, ioc->scsi_io_cb_idx);
2468 if (!smid) {
2469 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2470 ioc->name, __func__);
2471 goto out;
2472 }
2473 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2474 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
2475 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2476 if (sas_device_priv_data->sas_target->flags &
2477 MPT_TARGET_FLAGS_RAID_COMPONENT)
2478 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2479 else
2480 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2481 mpi_request->DevHandle =
2482 cpu_to_le16(sas_device_priv_data->sas_target->handle);
2483 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2484 mpi_request->Control = cpu_to_le32(mpi_control);
2485 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
2486 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
2487 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2488 mpi_request->SenseBufferLowAddress =
2489 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
2490 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
2491 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
2492 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
2493
2494 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
2495 mpi_request->LUN);
2496 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
2497
2498 if (!mpi_request->DataLength) {
2499 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
2500 } else {
2501 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
2502 mpt2sas_base_free_smid(ioc, smid);
2503 goto out;
2504 }
2505 }
2506
2507 _scsih_scsi_lookup_set(ioc, smid, scmd);
2508 mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
2509 sas_device_priv_data->sas_target->handle);
2510 return 0;
2511
2512 out:
2513 return SCSI_MLQUEUE_HOST_BUSY;
2514}
2515
2516/**
2517 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
2518 * @sense_buffer: sense data returned by target
2519 * @data: normalized skey/asc/ascq
2520 *
2521 * Return nothing.
2522 */
2523static void
2524_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
2525{
2526 if ((sense_buffer[0] & 0x7F) >= 0x72) {
2527 /* descriptor format */
2528 data->skey = sense_buffer[1] & 0x0F;
2529 data->asc = sense_buffer[2];
2530 data->ascq = sense_buffer[3];
2531 } else {
2532 /* fixed format */
2533 data->skey = sense_buffer[2] & 0x0F;
2534 data->asc = sense_buffer[12];
2535 data->ascq = sense_buffer[13];
2536 }
2537}
2538
2539#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2540/**
2541 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
2542 * @ioc: per adapter object
2543 * @scmd: pointer to scsi command object
2544 * @mpi_reply: reply mf payload returned from firmware
2545 *
2546 * scsi_status - SCSI Status code returned from target device
2547 * scsi_state - state info associated with SCSI_IO determined by ioc
2548 * ioc_status - ioc supplied status info
2549 *
2550 * Return nothing.
2551 */
2552static void
2553_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
2554 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
2555{
2556 u32 response_info;
2557 u8 *response_bytes;
2558 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
2559 MPI2_IOCSTATUS_MASK;
2560 u8 scsi_state = mpi_reply->SCSIState;
2561 u8 scsi_status = mpi_reply->SCSIStatus;
2562 char *desc_ioc_state = NULL;
2563 char *desc_scsi_status = NULL;
2564 char *desc_scsi_state = ioc->tmp_string;
2565
2566 switch (ioc_status) {
2567 case MPI2_IOCSTATUS_SUCCESS:
2568 desc_ioc_state = "success";
2569 break;
2570 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2571 desc_ioc_state = "invalid function";
2572 break;
2573 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2574 desc_ioc_state = "scsi recovered error";
2575 break;
2576 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2577 desc_ioc_state = "scsi invalid dev handle";
2578 break;
2579 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2580 desc_ioc_state = "scsi device not there";
2581 break;
2582 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2583 desc_ioc_state = "scsi data overrun";
2584 break;
2585 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2586 desc_ioc_state = "scsi data underrun";
2587 break;
2588 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2589 desc_ioc_state = "scsi io data error";
2590 break;
2591 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2592 desc_ioc_state = "scsi protocol error";
2593 break;
2594 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2595 desc_ioc_state = "scsi task terminated";
2596 break;
2597 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2598 desc_ioc_state = "scsi residual mismatch";
2599 break;
2600 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2601 desc_ioc_state = "scsi task mgmt failed";
2602 break;
2603 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2604 desc_ioc_state = "scsi ioc terminated";
2605 break;
2606 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2607 desc_ioc_state = "scsi ext terminated";
2608 break;
2609 default:
2610 desc_ioc_state = "unknown";
2611 break;
2612 }
2613
2614 switch (scsi_status) {
2615 case MPI2_SCSI_STATUS_GOOD:
2616 desc_scsi_status = "good";
2617 break;
2618 case MPI2_SCSI_STATUS_CHECK_CONDITION:
2619 desc_scsi_status = "check condition";
2620 break;
2621 case MPI2_SCSI_STATUS_CONDITION_MET:
2622 desc_scsi_status = "condition met";
2623 break;
2624 case MPI2_SCSI_STATUS_BUSY:
2625 desc_scsi_status = "busy";
2626 break;
2627 case MPI2_SCSI_STATUS_INTERMEDIATE:
2628 desc_scsi_status = "intermediate";
2629 break;
2630 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2631 desc_scsi_status = "intermediate condmet";
2632 break;
2633 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2634 desc_scsi_status = "reservation conflict";
2635 break;
2636 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2637 desc_scsi_status = "command terminated";
2638 break;
2639 case MPI2_SCSI_STATUS_TASK_SET_FULL:
2640 desc_scsi_status = "task set full";
2641 break;
2642 case MPI2_SCSI_STATUS_ACA_ACTIVE:
2643 desc_scsi_status = "aca active";
2644 break;
2645 case MPI2_SCSI_STATUS_TASK_ABORTED:
2646 desc_scsi_status = "task aborted";
2647 break;
2648 default:
2649 desc_scsi_status = "unknown";
2650 break;
2651 }
2652
2653 desc_scsi_state[0] = '\0';
2654 if (!scsi_state)
2655 desc_scsi_state = " ";
2656 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2657 strcat(desc_scsi_state, "response info ");
2658 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2659 strcat(desc_scsi_state, "state terminated ");
2660 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2661 strcat(desc_scsi_state, "no status ");
2662 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2663 strcat(desc_scsi_state, "autosense failed ");
2664 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2665 strcat(desc_scsi_state, "autosense valid ");
2666
2667 scsi_print_command(scmd);
2668 printk(MPT2SAS_WARN_FMT "\tdev handle(0x%04x), "
2669 "ioc_status(%s)(0x%04x), smid(%d)\n", ioc->name,
2670 le16_to_cpu(mpi_reply->DevHandle), desc_ioc_state,
2671 ioc_status, smid);
2672 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
2673 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
2674 scsi_get_resid(scmd));
2675 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
2676 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
2677 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
2678 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
2679 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
2680 scsi_status, desc_scsi_state, scsi_state);
2681
2682 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2683 struct sense_info data;
2684 _scsih_normalize_sense(scmd->sense_buffer, &data);
2685 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
2686 "[0x%02x,0x%02x,0x%02x]\n", ioc->name, data.skey,
2687 data.asc, data.ascq);
2688 }
2689
2690 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2691 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
2692 response_bytes = (u8 *)&response_info;
2693 _scsih_response_code(ioc, response_bytes[3]);
2694 }
2695}
2696#endif
2697
2698/**
2699 * _scsih_smart_predicted_fault - illuminate Fault LED
2700 * @ioc: per adapter object
2701 * @handle: device handle
2702 *
2703 * Return nothing.
2704 */
2705static void
2706_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2707{
2708 Mpi2SepReply_t mpi_reply;
2709 Mpi2SepRequest_t mpi_request;
2710 struct scsi_target *starget;
2711 struct MPT2SAS_TARGET *sas_target_priv_data;
2712 Mpi2EventNotificationReply_t *event_reply;
2713 Mpi2EventDataSasDeviceStatusChange_t *event_data;
2714 struct _sas_device *sas_device;
2715 ssize_t sz;
2716 unsigned long flags;
2717
2718 /* only handle non-raid devices */
2719 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2720 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2721 if (!sas_device) {
2722 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2723 return;
2724 }
2725 starget = sas_device->starget;
2726 sas_target_priv_data = starget->hostdata;
2727
2728 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
2729 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
2730 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2731 return;
2732 }
2733 starget_printk(KERN_WARNING, starget, "predicted fault\n");
2734 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2735
2736 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) {
2737 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
2738 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
2739 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
2740 mpi_request.SlotStatus =
2741 MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT;
2742 mpi_request.DevHandle = cpu_to_le16(handle);
2743 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
2744 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
2745 &mpi_request)) != 0) {
2746 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2747 ioc->name, __FILE__, __LINE__, __func__);
2748 return;
2749 }
2750
2751 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
2752 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2753 "enclosure_processor: ioc_status (0x%04x), "
2754 "loginfo(0x%08x)\n", ioc->name,
2755 le16_to_cpu(mpi_reply.IOCStatus),
2756 le32_to_cpu(mpi_reply.IOCLogInfo)));
2757 return;
2758 }
2759 }
2760
2761 /* insert into event log */
2762 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
2763 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
2764 event_reply = kzalloc(sz, GFP_KERNEL);
2765 if (!event_reply) {
2766 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2767 ioc->name, __FILE__, __LINE__, __func__);
2768 return;
2769 }
2770
2771 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2772 event_reply->Event =
2773 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
2774 event_reply->MsgLength = sz/4;
2775 event_reply->EventDataLength =
2776 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
2777 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
2778 event_reply->EventData;
2779 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
2780 event_data->ASC = 0x5D;
2781 event_data->DevHandle = cpu_to_le16(handle);
2782 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
2783 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
2784 kfree(event_reply);
2785}
2786
2787/**
2788 * scsih_io_done - scsi request callback
2789 * @ioc: per adapter object
2790 * @smid: system request message index
2791 * @VF_ID: virtual function id
2792 * @reply: reply message frame(lower 32bit addr)
2793 *
2794 * Callback handler when using scsih_qcmd.
2795 *
2796 * Return nothing.
2797 */
2798static void
2799scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
2800{
2801 Mpi2SCSIIORequest_t *mpi_request;
2802 Mpi2SCSIIOReply_t *mpi_reply;
2803 struct scsi_cmnd *scmd;
2804 u16 ioc_status;
2805 u32 xfer_cnt;
2806 u8 scsi_state;
2807 u8 scsi_status;
2808 u32 log_info;
2809 struct MPT2SAS_DEVICE *sas_device_priv_data;
2810 u32 response_code;
2811
2812 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2813 scmd = _scsih_scsi_lookup_getclear(ioc, smid);
2814 if (scmd == NULL)
2815 return;
2816
2817 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2818
2819 if (mpi_reply == NULL) {
2820 scmd->result = DID_OK << 16;
2821 goto out;
2822 }
2823
2824 sas_device_priv_data = scmd->device->hostdata;
2825 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
2826 sas_device_priv_data->sas_target->deleted) {
2827 scmd->result = DID_NO_CONNECT << 16;
2828 goto out;
2829 }
2830
2831 /* turning off TLR */
2832 if (!sas_device_priv_data->tlr_snoop_check) {
2833 sas_device_priv_data->tlr_snoop_check++;
2834 if (sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) {
2835 response_code = (le32_to_cpu(mpi_reply->ResponseInfo)
2836 >> 24);
2837 if (response_code ==
2838 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
2839 sas_device_priv_data->flags &=
2840 ~MPT_DEVICE_TLR_ON;
2841 }
2842 }
2843
2844 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
2845 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
2846 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
2847 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
2848 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
2849 else
2850 log_info = 0;
2851 ioc_status &= MPI2_IOCSTATUS_MASK;
2852 scsi_state = mpi_reply->SCSIState;
2853 scsi_status = mpi_reply->SCSIStatus;
2854
2855 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
2856 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
2857 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
2858 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
2859 ioc_status = MPI2_IOCSTATUS_SUCCESS;
2860 }
2861
2862 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2863 struct sense_info data;
2864 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
2865 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06002866 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06002867 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06002868 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06002869 _scsih_normalize_sense(scmd->sense_buffer, &data);
2870 /* failure prediction threshold exceeded */
2871 if (data.asc == 0x5D)
2872 _scsih_smart_predicted_fault(ioc,
2873 le16_to_cpu(mpi_reply->DevHandle));
2874 }
2875
2876 switch (ioc_status) {
2877 case MPI2_IOCSTATUS_BUSY:
2878 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
2879 scmd->result = SAM_STAT_BUSY;
2880 break;
2881
2882 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2883 scmd->result = DID_NO_CONNECT << 16;
2884 break;
2885
2886 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2887 if (sas_device_priv_data->block) {
2888 scmd->result = (DID_BUS_BUSY << 16);
2889 break;
2890 }
2891
2892 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2893 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2894 scmd->result = DID_RESET << 16;
2895 break;
2896
2897 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2898 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
2899 scmd->result = DID_SOFT_ERROR << 16;
2900 else
2901 scmd->result = (DID_OK << 16) | scsi_status;
2902 break;
2903
2904 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2905 scmd->result = (DID_OK << 16) | scsi_status;
2906
2907 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
2908 break;
2909
2910 if (xfer_cnt < scmd->underflow) {
2911 if (scsi_status == SAM_STAT_BUSY)
2912 scmd->result = SAM_STAT_BUSY;
2913 else
2914 scmd->result = DID_SOFT_ERROR << 16;
2915 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
2916 MPI2_SCSI_STATE_NO_SCSI_STATUS))
2917 scmd->result = DID_SOFT_ERROR << 16;
2918 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2919 scmd->result = DID_RESET << 16;
2920 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
2921 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
2922 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
2923 scmd->result = (DRIVER_SENSE << 24) |
2924 SAM_STAT_CHECK_CONDITION;
2925 scmd->sense_buffer[0] = 0x70;
2926 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
2927 scmd->sense_buffer[12] = 0x20;
2928 scmd->sense_buffer[13] = 0;
2929 }
2930 break;
2931
2932 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2933 scsi_set_resid(scmd, 0);
2934 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2935 case MPI2_IOCSTATUS_SUCCESS:
2936 scmd->result = (DID_OK << 16) | scsi_status;
2937 if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
2938 MPI2_SCSI_STATE_NO_SCSI_STATUS))
2939 scmd->result = DID_SOFT_ERROR << 16;
2940 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2941 scmd->result = DID_RESET << 16;
2942 break;
2943
2944 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2945 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2946 case MPI2_IOCSTATUS_INVALID_SGL:
2947 case MPI2_IOCSTATUS_INTERNAL_ERROR:
2948 case MPI2_IOCSTATUS_INVALID_FIELD:
2949 case MPI2_IOCSTATUS_INVALID_STATE:
2950 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2951 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2952 default:
2953 scmd->result = DID_SOFT_ERROR << 16;
2954 break;
2955
2956 }
2957
2958#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
2959 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
2960 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
2961#endif
2962
2963 out:
2964 scsi_dma_unmap(scmd);
2965 scmd->scsi_done(scmd);
2966}
2967
2968/**
2969 * _scsih_link_change - process phy link changes
2970 * @ioc: per adapter object
2971 * @handle: phy handle
2972 * @attached_handle: valid for devices attached to link
2973 * @phy_number: phy number
2974 * @link_rate: new link rate
2975 * Context: user.
2976 *
2977 * Return nothing.
2978 */
2979static void
2980_scsih_link_change(struct MPT2SAS_ADAPTER *ioc, u16 handle, u16 attached_handle,
2981 u8 phy_number, u8 link_rate)
2982{
2983 mpt2sas_transport_update_phy_link_change(ioc, handle, attached_handle,
2984 phy_number, link_rate);
2985}
2986
2987/**
2988 * _scsih_sas_host_refresh - refreshing sas host object contents
2989 * @ioc: per adapter object
2990 * @update: update link information
2991 * Context: user
2992 *
2993 * During port enable, fw will send topology events for every device. Its
2994 * possible that the handles may change from the previous setting, so this
2995 * code keeping handles updating if changed.
2996 *
2997 * Return nothing.
2998 */
2999static void
3000_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc, u8 update)
3001{
3002 u16 sz;
3003 u16 ioc_status;
3004 int i;
3005 Mpi2ConfigReply_t mpi_reply;
3006 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3007
3008 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
3009 "updating handles for sas_host(0x%016llx)\n",
3010 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
3011
3012 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
3013 * sizeof(Mpi2SasIOUnit0PhyData_t));
3014 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3015 if (!sas_iounit_pg0) {
3016 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3017 ioc->name, __FILE__, __LINE__, __func__);
3018 return;
3019 }
3020 if (!(mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3021 sas_iounit_pg0, sz))) {
3022 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3023 MPI2_IOCSTATUS_MASK;
3024 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3025 goto out;
3026 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3027 ioc->sas_hba.phy[i].handle =
3028 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3029 ControllerDevHandle);
3030 if (update)
3031 _scsih_link_change(ioc,
3032 ioc->sas_hba.phy[i].handle,
3033 le16_to_cpu(sas_iounit_pg0->PhyData[i].
3034 AttachedDevHandle), i,
3035 sas_iounit_pg0->PhyData[i].
3036 NegotiatedLinkRate >> 4);
3037 }
3038 }
3039
3040 out:
3041 kfree(sas_iounit_pg0);
3042}
3043
3044/**
3045 * _scsih_sas_host_add - create sas host object
3046 * @ioc: per adapter object
3047 *
3048 * Creating host side data object, stored in ioc->sas_hba
3049 *
3050 * Return nothing.
3051 */
3052static void
3053_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
3054{
3055 int i;
3056 Mpi2ConfigReply_t mpi_reply;
3057 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
3058 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3059 Mpi2SasPhyPage0_t phy_pg0;
3060 Mpi2SasDevicePage0_t sas_device_pg0;
3061 Mpi2SasEnclosurePage0_t enclosure_pg0;
3062 u16 ioc_status;
3063 u16 sz;
3064 u16 device_missing_delay;
3065
3066 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
3067 if (!ioc->sas_hba.num_phys) {
3068 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3069 ioc->name, __FILE__, __LINE__, __func__);
3070 return;
3071 }
3072
3073 /* sas_iounit page 0 */
3074 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
3075 sizeof(Mpi2SasIOUnit0PhyData_t));
3076 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
3077 if (!sas_iounit_pg0) {
3078 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3079 ioc->name, __FILE__, __LINE__, __func__);
3080 return;
3081 }
3082 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
3083 sas_iounit_pg0, sz))) {
3084 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3085 ioc->name, __FILE__, __LINE__, __func__);
3086 goto out;
3087 }
3088 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3089 MPI2_IOCSTATUS_MASK;
3090 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3091 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3092 ioc->name, __FILE__, __LINE__, __func__);
3093 goto out;
3094 }
3095
3096 /* sas_iounit page 1 */
3097 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
3098 sizeof(Mpi2SasIOUnit1PhyData_t));
3099 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3100 if (!sas_iounit_pg1) {
3101 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3102 ioc->name, __FILE__, __LINE__, __func__);
3103 goto out;
3104 }
3105 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3106 sas_iounit_pg1, sz))) {
3107 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3108 ioc->name, __FILE__, __LINE__, __func__);
3109 goto out;
3110 }
3111 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3112 MPI2_IOCSTATUS_MASK;
3113 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3114 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3115 ioc->name, __FILE__, __LINE__, __func__);
3116 goto out;
3117 }
3118
3119 ioc->io_missing_delay =
3120 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
3121 device_missing_delay =
3122 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
3123 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3124 ioc->device_missing_delay = (device_missing_delay &
3125 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3126 else
3127 ioc->device_missing_delay = device_missing_delay &
3128 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3129
3130 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
3131 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
3132 sizeof(struct _sas_phy), GFP_KERNEL);
3133 if (!ioc->sas_hba.phy) {
3134 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3135 ioc->name, __FILE__, __LINE__, __func__);
3136 goto out;
3137 }
3138 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
3139 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
3140 i))) {
3141 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3142 ioc->name, __FILE__, __LINE__, __func__);
3143 goto out;
3144 }
3145 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3146 MPI2_IOCSTATUS_MASK;
3147 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3148 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3149 ioc->name, __FILE__, __LINE__, __func__);
3150 goto out;
3151 }
3152 ioc->sas_hba.phy[i].handle =
3153 le16_to_cpu(sas_iounit_pg0->PhyData[i].ControllerDevHandle);
3154 ioc->sas_hba.phy[i].phy_id = i;
3155 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
3156 phy_pg0, ioc->sas_hba.parent_dev);
3157 }
3158 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3159 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.phy[0].handle))) {
3160 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3161 ioc->name, __FILE__, __LINE__, __func__);
3162 goto out;
3163 }
3164 ioc->sas_hba.handle = le16_to_cpu(sas_device_pg0.DevHandle);
3165 ioc->sas_hba.enclosure_handle =
3166 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3167 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3168 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
3169 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
3170 (unsigned long long) ioc->sas_hba.sas_address,
3171 ioc->sas_hba.num_phys) ;
3172
3173 if (ioc->sas_hba.enclosure_handle) {
3174 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3175 &enclosure_pg0,
3176 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3177 ioc->sas_hba.enclosure_handle))) {
3178 ioc->sas_hba.enclosure_logical_id =
3179 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3180 }
3181 }
3182
3183 out:
3184 kfree(sas_iounit_pg1);
3185 kfree(sas_iounit_pg0);
3186}
3187
3188/**
3189 * _scsih_expander_add - creating expander object
3190 * @ioc: per adapter object
3191 * @handle: expander handle
3192 *
3193 * Creating expander object, stored in ioc->sas_expander_list.
3194 *
3195 * Return 0 for success, else error.
3196 */
3197static int
3198_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3199{
3200 struct _sas_node *sas_expander;
3201 Mpi2ConfigReply_t mpi_reply;
3202 Mpi2ExpanderPage0_t expander_pg0;
3203 Mpi2ExpanderPage1_t expander_pg1;
3204 Mpi2SasEnclosurePage0_t enclosure_pg0;
3205 u32 ioc_status;
3206 u16 parent_handle;
3207 __le64 sas_address;
3208 int i;
3209 unsigned long flags;
3210 struct _sas_port *mpt2sas_port;
3211 int rc = 0;
3212
3213 if (!handle)
3214 return -1;
3215
3216 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
3217 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
3218 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3219 ioc->name, __FILE__, __LINE__, __func__);
3220 return -1;
3221 }
3222
3223 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3224 MPI2_IOCSTATUS_MASK;
3225 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3226 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3227 ioc->name, __FILE__, __LINE__, __func__);
3228 return -1;
3229 }
3230
3231 /* handle out of order topology events */
3232 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
3233 if (parent_handle >= ioc->sas_hba.num_phys) {
3234 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3235 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3236 parent_handle);
3237 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3238 if (!sas_expander) {
3239 rc = _scsih_expander_add(ioc, parent_handle);
3240 if (rc != 0)
3241 return rc;
3242 }
3243 }
3244
3245 sas_address = le64_to_cpu(expander_pg0.SASAddress);
3246
3247 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3248 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
3249 sas_address);
3250 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3251
3252 if (sas_expander)
3253 return 0;
3254
3255 sas_expander = kzalloc(sizeof(struct _sas_node),
3256 GFP_KERNEL);
3257 if (!sas_expander) {
3258 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3259 ioc->name, __FILE__, __LINE__, __func__);
3260 return -1;
3261 }
3262
3263 sas_expander->handle = handle;
3264 sas_expander->num_phys = expander_pg0.NumPhys;
3265 sas_expander->parent_handle = parent_handle;
3266 sas_expander->enclosure_handle =
3267 le16_to_cpu(expander_pg0.EnclosureHandle);
3268 sas_expander->sas_address = sas_address;
3269
3270 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
3271 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
3272 handle, sas_expander->parent_handle, (unsigned long long)
3273 sas_expander->sas_address, sas_expander->num_phys);
3274
3275 if (!sas_expander->num_phys)
3276 goto out_fail;
3277 sas_expander->phy = kcalloc(sas_expander->num_phys,
3278 sizeof(struct _sas_phy), GFP_KERNEL);
3279 if (!sas_expander->phy) {
3280 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3281 ioc->name, __FILE__, __LINE__, __func__);
3282 rc = -1;
3283 goto out_fail;
3284 }
3285
3286 INIT_LIST_HEAD(&sas_expander->sas_port_list);
3287 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
3288 sas_expander->parent_handle);
3289 if (!mpt2sas_port) {
3290 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3291 ioc->name, __FILE__, __LINE__, __func__);
3292 rc = -1;
3293 goto out_fail;
3294 }
3295 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
3296
3297 for (i = 0 ; i < sas_expander->num_phys ; i++) {
3298 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
3299 &expander_pg1, i, handle))) {
3300 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3301 ioc->name, __FILE__, __LINE__, __func__);
3302 continue;
3303 }
3304 sas_expander->phy[i].handle = handle;
3305 sas_expander->phy[i].phy_id = i;
3306 mpt2sas_transport_add_expander_phy(ioc, &sas_expander->phy[i],
3307 expander_pg1, sas_expander->parent_dev);
3308 }
3309
3310 if (sas_expander->enclosure_handle) {
3311 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
3312 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3313 sas_expander->enclosure_handle))) {
3314 sas_expander->enclosure_logical_id =
3315 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3316 }
3317 }
3318
3319 _scsih_expander_node_add(ioc, sas_expander);
3320 return 0;
3321
3322 out_fail:
3323
3324 if (sas_expander)
3325 kfree(sas_expander->phy);
3326 kfree(sas_expander);
3327 return rc;
3328}
3329
3330/**
3331 * _scsih_expander_remove - removing expander object
3332 * @ioc: per adapter object
3333 * @handle: expander handle
3334 *
3335 * Return nothing.
3336 */
3337static void
3338_scsih_expander_remove(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3339{
3340 struct _sas_node *sas_expander;
3341 unsigned long flags;
3342
3343 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3344 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc, handle);
3345 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3346 _scsih_expander_node_remove(ioc, sas_expander);
3347}
3348
3349/**
3350 * _scsih_add_device - creating sas device object
3351 * @ioc: per adapter object
3352 * @handle: sas device handle
3353 * @phy_num: phy number end device attached to
3354 * @is_pd: is this hidden raid component
3355 *
3356 * Creating end device object, stored in ioc->sas_device_list.
3357 *
3358 * Returns 0 for success, non-zero for failure.
3359 */
3360static int
3361_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
3362{
3363 Mpi2ConfigReply_t mpi_reply;
3364 Mpi2SasDevicePage0_t sas_device_pg0;
3365 Mpi2SasEnclosurePage0_t enclosure_pg0;
3366 struct _sas_device *sas_device;
3367 u32 ioc_status;
3368 __le64 sas_address;
3369 u32 device_info;
3370 unsigned long flags;
3371
3372 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
3373 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
3374 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3375 ioc->name, __FILE__, __LINE__, __func__);
3376 return -1;
3377 }
3378
3379 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3380 MPI2_IOCSTATUS_MASK;
3381 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3382 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3383 ioc->name, __FILE__, __LINE__, __func__);
3384 return -1;
3385 }
3386
3387 /* check if device is present */
3388 if (!(le16_to_cpu(sas_device_pg0.Flags) &
3389 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
3390 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3391 ioc->name, __FILE__, __LINE__, __func__);
3392 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
3393 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
3394 return -1;
3395 }
3396
3397 /* check if there were any issus with discovery */
3398 if (sas_device_pg0.AccessStatus ==
3399 MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED) {
3400 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3401 ioc->name, __FILE__, __LINE__, __func__);
3402 printk(MPT2SAS_ERR_FMT "AccessStatus = 0x%02x\n",
3403 ioc->name, sas_device_pg0.AccessStatus);
3404 return -1;
3405 }
3406
3407 /* check if this is end device */
3408 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
3409 if (!(_scsih_is_end_device(device_info))) {
3410 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3411 ioc->name, __FILE__, __LINE__, __func__);
3412 return -1;
3413 }
3414
3415 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
3416
3417 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3418 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3419 sas_address);
3420 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3421
3422 if (sas_device) {
3423 _scsih_ublock_io_device(ioc, handle);
3424 return 0;
3425 }
3426
3427 sas_device = kzalloc(sizeof(struct _sas_device),
3428 GFP_KERNEL);
3429 if (!sas_device) {
3430 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3431 ioc->name, __FILE__, __LINE__, __func__);
3432 return -1;
3433 }
3434
3435 sas_device->handle = handle;
3436 sas_device->parent_handle =
3437 le16_to_cpu(sas_device_pg0.ParentDevHandle);
3438 sas_device->enclosure_handle =
3439 le16_to_cpu(sas_device_pg0.EnclosureHandle);
3440 sas_device->slot =
3441 le16_to_cpu(sas_device_pg0.Slot);
3442 sas_device->device_info = device_info;
3443 sas_device->sas_address = sas_address;
3444 sas_device->hidden_raid_component = is_pd;
3445
3446 /* get enclosure_logical_id */
3447 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply, &enclosure_pg0,
3448 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
3449 sas_device->enclosure_handle))) {
3450 sas_device->enclosure_logical_id =
3451 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
3452 }
3453
3454 /* get device name */
3455 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
3456
3457 if (ioc->wait_for_port_enable_to_complete)
3458 _scsih_sas_device_init_add(ioc, sas_device);
3459 else
3460 _scsih_sas_device_add(ioc, sas_device);
3461
3462 return 0;
3463}
3464
3465/**
3466 * _scsih_remove_device - removing sas device object
3467 * @ioc: per adapter object
3468 * @handle: sas device handle
3469 *
3470 * Return nothing.
3471 */
3472static void
3473_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3474{
3475 struct MPT2SAS_TARGET *sas_target_priv_data;
3476 struct _sas_device *sas_device;
3477 unsigned long flags;
3478 Mpi2SasIoUnitControlReply_t mpi_reply;
3479 Mpi2SasIoUnitControlRequest_t mpi_request;
3480 u16 device_handle;
3481
3482 /* lookup sas_device */
3483 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3484 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3485 if (!sas_device) {
3486 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3487 return;
3488 }
3489
3490 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: handle"
3491 "(0x%04x)\n", ioc->name, __func__, handle));
3492
3493 if (sas_device->starget && sas_device->starget->hostdata) {
3494 sas_target_priv_data = sas_device->starget->hostdata;
3495 sas_target_priv_data->deleted = 1;
3496 }
3497 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3498
3499 if (ioc->remove_host)
3500 goto out;
3501
3502 /* Target Reset to flush out all the outstanding IO */
3503 device_handle = (sas_device->hidden_raid_component) ?
3504 sas_device->volume_handle : handle;
3505 if (device_handle) {
3506 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset: "
3507 "handle(0x%04x)\n", ioc->name, device_handle));
3508 mutex_lock(&ioc->tm_cmds.mutex);
3509 mpt2sas_scsih_issue_tm(ioc, device_handle, 0,
3510 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
3511 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3512 mutex_unlock(&ioc->tm_cmds.mutex);
3513 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "issue target reset "
3514 "done: handle(0x%04x)\n", ioc->name, device_handle));
3515 }
3516
3517 /* SAS_IO_UNIT_CNTR - send REMOVE_DEVICE */
3518 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: handle"
3519 "(0x%04x)\n", ioc->name, handle));
3520 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3521 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3522 mpi_request.Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3523 mpi_request.DevHandle = handle;
3524 mpi_request.VF_ID = 0;
3525 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply,
3526 &mpi_request)) != 0) {
3527 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
3528 ioc->name, __FILE__, __LINE__, __func__);
3529 }
3530
3531 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sas_iounit: ioc_status"
3532 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
3533 le16_to_cpu(mpi_reply.IOCStatus),
3534 le32_to_cpu(mpi_reply.IOCLogInfo)));
3535
3536 out:
3537 mpt2sas_transport_port_remove(ioc, sas_device->sas_address,
3538 sas_device->parent_handle);
3539
3540 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
3541 "(0x%016llx)\n", ioc->name, sas_device->handle,
3542 (unsigned long long) sas_device->sas_address);
3543 _scsih_sas_device_remove(ioc, sas_device);
3544
3545 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: handle"
3546 "(0x%04x)\n", ioc->name, __func__, handle));
3547}
3548
3549#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3550/**
3551 * _scsih_sas_topology_change_event_debug - debug for topology event
3552 * @ioc: per adapter object
3553 * @event_data: event data payload
3554 * Context: user.
3555 */
3556static void
3557_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3558 Mpi2EventDataSasTopologyChangeList_t *event_data)
3559{
3560 int i;
3561 u16 handle;
3562 u16 reason_code;
3563 u8 phy_number;
3564 char *status_str = NULL;
3565 char link_rate[25];
3566
3567 switch (event_data->ExpStatus) {
3568 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
3569 status_str = "add";
3570 break;
3571 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
3572 status_str = "remove";
3573 break;
3574 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
3575 status_str = "responding";
3576 break;
3577 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
3578 status_str = "remove delay";
3579 break;
3580 default:
3581 status_str = "unknown status";
3582 break;
3583 }
3584 printk(MPT2SAS_DEBUG_FMT "sas topology change: (%s)\n",
3585 ioc->name, status_str);
3586 printk(KERN_DEBUG "\thandle(0x%04x), enclosure_handle(0x%04x) "
3587 "start_phy(%02d), count(%d)\n",
3588 le16_to_cpu(event_data->ExpanderDevHandle),
3589 le16_to_cpu(event_data->EnclosureHandle),
3590 event_data->StartPhyNum, event_data->NumEntries);
3591 for (i = 0; i < event_data->NumEntries; i++) {
3592 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3593 if (!handle)
3594 continue;
3595 phy_number = event_data->StartPhyNum + i;
3596 reason_code = event_data->PHY[i].PhyStatus &
3597 MPI2_EVENT_SAS_TOPO_RC_MASK;
3598 switch (reason_code) {
3599 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3600 snprintf(link_rate, 25, ": add, link(0x%02x)",
3601 (event_data->PHY[i].LinkRate >> 4));
3602 status_str = link_rate;
3603 break;
3604 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3605 status_str = ": remove";
3606 break;
3607 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
3608 status_str = ": remove_delay";
3609 break;
3610 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3611 snprintf(link_rate, 25, ": link(0x%02x)",
3612 (event_data->PHY[i].LinkRate >> 4));
3613 status_str = link_rate;
3614 break;
3615 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
3616 status_str = ": responding";
3617 break;
3618 default:
3619 status_str = ": unknown";
3620 break;
3621 }
3622 printk(KERN_DEBUG "\tphy(%02d), attached_handle(0x%04x)%s\n",
3623 phy_number, handle, status_str);
3624 }
3625}
3626#endif
3627
3628/**
3629 * _scsih_sas_topology_change_event - handle topology changes
3630 * @ioc: per adapter object
3631 * @VF_ID:
3632 * @event_data: event data payload
3633 * fw_event:
3634 * Context: user.
3635 *
3636 */
3637static void
3638_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3639 Mpi2EventDataSasTopologyChangeList_t *event_data,
3640 struct fw_event_work *fw_event)
3641{
3642 int i;
3643 u16 parent_handle, handle;
3644 u16 reason_code;
3645 u8 phy_number;
3646 struct _sas_node *sas_expander;
3647 unsigned long flags;
3648 u8 link_rate_;
3649
3650#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3651 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3652 _scsih_sas_topology_change_event_debug(ioc, event_data);
3653#endif
3654
3655 if (!ioc->sas_hba.num_phys)
3656 _scsih_sas_host_add(ioc);
3657 else
3658 _scsih_sas_host_refresh(ioc, 0);
3659
3660 if (fw_event->ignore) {
3661 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring expander "
3662 "event\n", ioc->name));
3663 return;
3664 }
3665
3666 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3667
3668 /* handle expander add */
3669 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
3670 if (_scsih_expander_add(ioc, parent_handle) != 0)
3671 return;
3672
3673 /* handle siblings events */
3674 for (i = 0; i < event_data->NumEntries; i++) {
3675 if (fw_event->ignore) {
3676 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ignoring "
3677 "expander event\n", ioc->name));
3678 return;
3679 }
3680 if (event_data->PHY[i].PhyStatus &
3681 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT)
3682 continue;
3683 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3684 if (!handle)
3685 continue;
3686 phy_number = event_data->StartPhyNum + i;
3687 reason_code = event_data->PHY[i].PhyStatus &
3688 MPI2_EVENT_SAS_TOPO_RC_MASK;
3689 link_rate_ = event_data->PHY[i].LinkRate >> 4;
3690 switch (reason_code) {
3691 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
3692 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
3693 if (!parent_handle) {
3694 if (phy_number < ioc->sas_hba.num_phys)
3695 _scsih_link_change(ioc,
3696 ioc->sas_hba.phy[phy_number].handle,
3697 handle, phy_number, link_rate_);
3698 } else {
3699 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3700 sas_expander =
3701 mpt2sas_scsih_expander_find_by_handle(ioc,
3702 parent_handle);
3703 spin_unlock_irqrestore(&ioc->sas_node_lock,
3704 flags);
3705 if (sas_expander) {
3706 if (phy_number < sas_expander->num_phys)
3707 _scsih_link_change(ioc,
3708 sas_expander->
3709 phy[phy_number].handle,
3710 handle, phy_number,
3711 link_rate_);
3712 }
3713 }
3714 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED) {
3715 if (link_rate_ >= MPI2_SAS_NEG_LINK_RATE_1_5)
3716 _scsih_ublock_io_device(ioc, handle);
3717 }
3718 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED) {
3719 if (link_rate_ < MPI2_SAS_NEG_LINK_RATE_1_5)
3720 break;
3721 _scsih_add_device(ioc, handle, phy_number, 0);
3722 }
3723 break;
3724 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
3725 _scsih_remove_device(ioc, handle);
3726 break;
3727 }
3728 }
3729
3730 /* handle expander removal */
3731 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3732 _scsih_expander_remove(ioc, parent_handle);
3733
3734}
3735
3736#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3737/**
3738 * _scsih_sas_device_status_change_event_debug - debug for device event
3739 * @event_data: event data payload
3740 * Context: user.
3741 *
3742 * Return nothing.
3743 */
3744static void
3745_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3746 Mpi2EventDataSasDeviceStatusChange_t *event_data)
3747{
3748 char *reason_str = NULL;
3749
3750 switch (event_data->ReasonCode) {
3751 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
3752 reason_str = "smart data";
3753 break;
3754 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
3755 reason_str = "unsupported device discovered";
3756 break;
3757 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
3758 reason_str = "internal device reset";
3759 break;
3760 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
3761 reason_str = "internal task abort";
3762 break;
3763 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
3764 reason_str = "internal task abort set";
3765 break;
3766 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
3767 reason_str = "internal clear task set";
3768 break;
3769 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
3770 reason_str = "internal query task";
3771 break;
3772 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
3773 reason_str = "sata init failure";
3774 break;
3775 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
3776 reason_str = "internal device reset complete";
3777 break;
3778 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
3779 reason_str = "internal task abort complete";
3780 break;
3781 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
3782 reason_str = "internal async notification";
3783 break;
3784 default:
3785 reason_str = "unknown reason";
3786 break;
3787 }
3788 printk(MPT2SAS_DEBUG_FMT "device status change: (%s)\n"
3789 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
3790 reason_str, le16_to_cpu(event_data->DevHandle),
3791 (unsigned long long)le64_to_cpu(event_data->SASAddress));
3792 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
3793 printk(MPT2SAS_DEBUG_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
3794 event_data->ASC, event_data->ASCQ);
3795 printk(KERN_INFO "\n");
3796}
3797#endif
3798
3799/**
3800 * _scsih_sas_device_status_change_event - handle device status change
3801 * @ioc: per adapter object
3802 * @VF_ID:
3803 * @event_data: event data payload
3804 * Context: user.
3805 *
3806 * Return nothing.
3807 */
3808static void
3809_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3810 Mpi2EventDataSasDeviceStatusChange_t *event_data)
3811{
3812#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3813 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3814 _scsih_sas_device_status_change_event_debug(ioc, event_data);
3815#endif
3816}
3817
3818#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3819/**
3820 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
3821 * @ioc: per adapter object
3822 * @event_data: event data payload
3823 * Context: user.
3824 *
3825 * Return nothing.
3826 */
3827static void
3828_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
3829 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
3830{
3831 char *reason_str = NULL;
3832
3833 switch (event_data->ReasonCode) {
3834 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
3835 reason_str = "enclosure add";
3836 break;
3837 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
3838 reason_str = "enclosure remove";
3839 break;
3840 default:
3841 reason_str = "unknown reason";
3842 break;
3843 }
3844
3845 printk(MPT2SAS_DEBUG_FMT "enclosure status change: (%s)\n"
3846 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
3847 " number slots(%d)\n", ioc->name, reason_str,
3848 le16_to_cpu(event_data->EnclosureHandle),
3849 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
3850 le16_to_cpu(event_data->StartSlot));
3851}
3852#endif
3853
3854/**
3855 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
3856 * @ioc: per adapter object
3857 * @VF_ID:
3858 * @event_data: event data payload
3859 * Context: user.
3860 *
3861 * Return nothing.
3862 */
3863static void
3864_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
3865 u8 VF_ID, Mpi2EventDataSasEnclDevStatusChange_t *event_data)
3866{
3867#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3868 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
3869 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
3870 event_data);
3871#endif
3872}
3873
3874/**
3875 * _scsih_sas_broadcast_primative_event - handle broadcast events
3876 * @ioc: per adapter object
3877 * @event_data: event data payload
3878 * Context: user.
3879 *
3880 * Return nothing.
3881 */
3882static void
3883_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3884 Mpi2EventDataSasBroadcastPrimitive_t *event_data)
3885{
3886 struct scsi_cmnd *scmd;
3887 u16 smid, handle;
3888 u32 lun;
3889 struct MPT2SAS_DEVICE *sas_device_priv_data;
3890 u32 termination_count;
3891 u32 query_count;
3892 Mpi2SCSITaskManagementReply_t *mpi_reply;
3893
3894 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "broadcast primative: "
3895 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
3896 event_data->PortWidth));
3897
3898 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
3899 __func__));
3900
3901 mutex_lock(&ioc->tm_cmds.mutex);
3902 termination_count = 0;
3903 query_count = 0;
3904 mpi_reply = ioc->tm_cmds.reply;
3905 for (smid = 1; smid <= ioc->request_depth; smid++) {
3906 scmd = _scsih_scsi_lookup_get(ioc, smid);
3907 if (!scmd)
3908 continue;
3909 sas_device_priv_data = scmd->device->hostdata;
3910 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
3911 continue;
3912 /* skip hidden raid components */
3913 if (sas_device_priv_data->sas_target->flags &
3914 MPT_TARGET_FLAGS_RAID_COMPONENT)
3915 continue;
3916 /* skip volumes */
3917 if (sas_device_priv_data->sas_target->flags &
3918 MPT_TARGET_FLAGS_VOLUME)
3919 continue;
3920
3921 handle = sas_device_priv_data->sas_target->handle;
3922 lun = sas_device_priv_data->lun;
3923 query_count++;
3924
3925 mpt2sas_scsih_issue_tm(ioc, handle, lun,
3926 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
3927 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
3928
3929 if ((mpi_reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) &&
3930 (mpi_reply->ResponseCode ==
3931 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
3932 mpi_reply->ResponseCode ==
3933 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC))
3934 continue;
3935
3936 mpt2sas_scsih_issue_tm(ioc, handle, lun,
3937 MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET, smid, 30);
3938 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
3939 }
3940 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3941 ioc->broadcast_aen_busy = 0;
3942 mutex_unlock(&ioc->tm_cmds.mutex);
3943
3944 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT
3945 "%s - exit, query_count = %d termination_count = %d\n",
3946 ioc->name, __func__, query_count, termination_count));
3947}
3948
3949/**
3950 * _scsih_sas_discovery_event - handle discovery events
3951 * @ioc: per adapter object
3952 * @event_data: event data payload
3953 * Context: user.
3954 *
3955 * Return nothing.
3956 */
3957static void
3958_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3959 Mpi2EventDataSasDiscovery_t *event_data)
3960{
3961#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3962 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
3963 printk(MPT2SAS_DEBUG_FMT "discovery event: (%s)", ioc->name,
3964 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
3965 "start" : "stop");
3966 if (event_data->DiscoveryStatus)
3967 printk(MPT2SAS_DEBUG_FMT ", discovery_status(0x%08x)",
3968 ioc->name, le32_to_cpu(event_data->DiscoveryStatus));
3969 printk("\n");
3970 }
3971#endif
3972
3973 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
3974 !ioc->sas_hba.num_phys)
3975 _scsih_sas_host_add(ioc);
3976}
3977
3978/**
3979 * _scsih_reprobe_lun - reprobing lun
3980 * @sdev: scsi device struct
3981 * @no_uld_attach: sdev->no_uld_attach flag setting
3982 *
3983 **/
3984static void
3985_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
3986{
3987 int rc;
3988
3989 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
3990 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
3991 sdev->no_uld_attach ? "hidding" : "exposing");
3992 rc = scsi_device_reprobe(sdev);
3993}
3994
3995/**
3996 * _scsih_reprobe_target - reprobing target
3997 * @starget: scsi target struct
3998 * @no_uld_attach: sdev->no_uld_attach flag setting
3999 *
4000 * Note: no_uld_attach flag determines whether the disk device is attached
4001 * to block layer. A value of `1` means to not attach.
4002 **/
4003static void
4004_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
4005{
4006 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
4007
4008 if (no_uld_attach)
4009 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
4010 else
4011 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
4012
4013 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
4014 _scsih_reprobe_lun);
4015}
4016/**
4017 * _scsih_sas_volume_add - add new volume
4018 * @ioc: per adapter object
4019 * @element: IR config element data
4020 * Context: user.
4021 *
4022 * Return nothing.
4023 */
4024static void
4025_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
4026 Mpi2EventIrConfigElement_t *element)
4027{
4028 struct _raid_device *raid_device;
4029 unsigned long flags;
4030 u64 wwid;
4031 u16 handle = le16_to_cpu(element->VolDevHandle);
4032 int rc;
4033
4034#if 0 /* RAID_HACKS */
4035 if (le32_to_cpu(event_data->Flags) &
4036 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
4037 return;
4038#endif
4039
4040 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4041 if (!wwid) {
4042 printk(MPT2SAS_ERR_FMT
4043 "failure at %s:%d/%s()!\n", ioc->name,
4044 __FILE__, __LINE__, __func__);
4045 return;
4046 }
4047
4048 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4049 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
4050 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4051
4052 if (raid_device)
4053 return;
4054
4055 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4056 if (!raid_device) {
4057 printk(MPT2SAS_ERR_FMT
4058 "failure at %s:%d/%s()!\n", ioc->name,
4059 __FILE__, __LINE__, __func__);
4060 return;
4061 }
4062
4063 raid_device->id = ioc->sas_id++;
4064 raid_device->channel = RAID_CHANNEL;
4065 raid_device->handle = handle;
4066 raid_device->wwid = wwid;
4067 _scsih_raid_device_add(ioc, raid_device);
4068 if (!ioc->wait_for_port_enable_to_complete) {
4069 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4070 raid_device->id, 0);
4071 if (rc)
4072 _scsih_raid_device_remove(ioc, raid_device);
4073 } else
4074 _scsih_determine_boot_device(ioc, raid_device, 1);
4075}
4076
4077/**
4078 * _scsih_sas_volume_delete - delete volume
4079 * @ioc: per adapter object
4080 * @element: IR config element data
4081 * Context: user.
4082 *
4083 * Return nothing.
4084 */
4085static void
4086_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc,
4087 Mpi2EventIrConfigElement_t *element)
4088{
4089 struct _raid_device *raid_device;
4090 u16 handle = le16_to_cpu(element->VolDevHandle);
4091 unsigned long flags;
4092 struct MPT2SAS_TARGET *sas_target_priv_data;
4093
4094#if 0 /* RAID_HACKS */
4095 if (le32_to_cpu(event_data->Flags) &
4096 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
4097 return;
4098#endif
4099
4100 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4101 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4102 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4103 if (!raid_device)
4104 return;
4105 if (raid_device->starget) {
4106 sas_target_priv_data = raid_device->starget->hostdata;
4107 sas_target_priv_data->deleted = 1;
4108 scsi_remove_target(&raid_device->starget->dev);
4109 }
4110 _scsih_raid_device_remove(ioc, raid_device);
4111}
4112
4113/**
4114 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
4115 * @ioc: per adapter object
4116 * @element: IR config element data
4117 * Context: user.
4118 *
4119 * Return nothing.
4120 */
4121static void
4122_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
4123 Mpi2EventIrConfigElement_t *element)
4124{
4125 struct _sas_device *sas_device;
4126 unsigned long flags;
4127 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4128
4129 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4130 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4131 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4132 if (!sas_device)
4133 return;
4134
4135 /* exposing raid component */
4136 sas_device->volume_handle = 0;
4137 sas_device->volume_wwid = 0;
4138 sas_device->hidden_raid_component = 0;
4139 _scsih_reprobe_target(sas_device->starget, 0);
4140}
4141
4142/**
4143 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
4144 * @ioc: per adapter object
4145 * @element: IR config element data
4146 * Context: user.
4147 *
4148 * Return nothing.
4149 */
4150static void
4151_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
4152 Mpi2EventIrConfigElement_t *element)
4153{
4154 struct _sas_device *sas_device;
4155 unsigned long flags;
4156 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4157
4158 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4159 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4160 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4161 if (!sas_device)
4162 return;
4163
4164 /* hiding raid component */
4165 mpt2sas_config_get_volume_handle(ioc, handle,
4166 &sas_device->volume_handle);
4167 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
4168 &sas_device->volume_wwid);
4169 sas_device->hidden_raid_component = 1;
4170 _scsih_reprobe_target(sas_device->starget, 1);
4171}
4172
4173/**
4174 * _scsih_sas_pd_delete - delete pd component
4175 * @ioc: per adapter object
4176 * @element: IR config element data
4177 * Context: user.
4178 *
4179 * Return nothing.
4180 */
4181static void
4182_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
4183 Mpi2EventIrConfigElement_t *element)
4184{
4185 struct _sas_device *sas_device;
4186 unsigned long flags;
4187 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4188
4189 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4190 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4191 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4192 if (!sas_device)
4193 return;
4194 _scsih_remove_device(ioc, handle);
4195}
4196
4197/**
4198 * _scsih_sas_pd_add - remove pd component
4199 * @ioc: per adapter object
4200 * @element: IR config element data
4201 * Context: user.
4202 *
4203 * Return nothing.
4204 */
4205static void
4206_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
4207 Mpi2EventIrConfigElement_t *element)
4208{
4209 struct _sas_device *sas_device;
4210 unsigned long flags;
4211 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
4212
4213 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4214 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4215 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4216 if (sas_device)
4217 sas_device->hidden_raid_component = 1;
4218 else
4219 _scsih_add_device(ioc, handle, 0, 1);
4220}
4221
4222#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4223/**
4224 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
4225 * @ioc: per adapter object
4226 * @event_data: event data payload
4227 * Context: user.
4228 *
4229 * Return nothing.
4230 */
4231static void
4232_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
4233 Mpi2EventDataIrConfigChangeList_t *event_data)
4234{
4235 Mpi2EventIrConfigElement_t *element;
4236 u8 element_type;
4237 int i;
4238 char *reason_str = NULL, *element_str = NULL;
4239
4240 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4241
4242 printk(MPT2SAS_DEBUG_FMT "raid config change: (%s), elements(%d)\n",
4243 ioc->name, (le32_to_cpu(event_data->Flags) &
4244 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
4245 "foreign" : "native", event_data->NumElements);
4246 for (i = 0; i < event_data->NumElements; i++, element++) {
4247 switch (element->ReasonCode) {
4248 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4249 reason_str = "add";
4250 break;
4251 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4252 reason_str = "remove";
4253 break;
4254 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
4255 reason_str = "no change";
4256 break;
4257 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4258 reason_str = "hide";
4259 break;
4260 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4261 reason_str = "unhide";
4262 break;
4263 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4264 reason_str = "volume_created";
4265 break;
4266 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4267 reason_str = "volume_deleted";
4268 break;
4269 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4270 reason_str = "pd_created";
4271 break;
4272 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4273 reason_str = "pd_deleted";
4274 break;
4275 default:
4276 reason_str = "unknown reason";
4277 break;
4278 }
4279 element_type = le16_to_cpu(element->ElementFlags) &
4280 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
4281 switch (element_type) {
4282 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
4283 element_str = "volume";
4284 break;
4285 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
4286 element_str = "phys disk";
4287 break;
4288 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
4289 element_str = "hot spare";
4290 break;
4291 default:
4292 element_str = "unknown element";
4293 break;
4294 }
4295 printk(KERN_DEBUG "\t(%s:%s), vol handle(0x%04x), "
4296 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
4297 reason_str, le16_to_cpu(element->VolDevHandle),
4298 le16_to_cpu(element->PhysDiskDevHandle),
4299 element->PhysDiskNum);
4300 }
4301}
4302#endif
4303
4304/**
4305 * _scsih_sas_ir_config_change_event - handle ir configuration change events
4306 * @ioc: per adapter object
4307 * @VF_ID:
4308 * @event_data: event data payload
4309 * Context: user.
4310 *
4311 * Return nothing.
4312 */
4313static void
4314_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4315 Mpi2EventDataIrConfigChangeList_t *event_data)
4316{
4317 Mpi2EventIrConfigElement_t *element;
4318 int i;
4319
4320#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4321 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4322 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
4323
4324#endif
4325
4326 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
4327 for (i = 0; i < event_data->NumElements; i++, element++) {
4328
4329 switch (element->ReasonCode) {
4330 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
4331 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
4332 _scsih_sas_volume_add(ioc, element);
4333 break;
4334 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
4335 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
4336 _scsih_sas_volume_delete(ioc, element);
4337 break;
4338 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
4339 _scsih_sas_pd_hide(ioc, element);
4340 break;
4341 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
4342 _scsih_sas_pd_expose(ioc, element);
4343 break;
4344 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
4345 _scsih_sas_pd_add(ioc, element);
4346 break;
4347 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
4348 _scsih_sas_pd_delete(ioc, element);
4349 break;
4350 }
4351 }
4352}
4353
4354/**
4355 * _scsih_sas_ir_volume_event - IR volume event
4356 * @ioc: per adapter object
4357 * @event_data: event data payload
4358 * Context: user.
4359 *
4360 * Return nothing.
4361 */
4362static void
4363_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4364 Mpi2EventDataIrVolume_t *event_data)
4365{
4366 u64 wwid;
4367 unsigned long flags;
4368 struct _raid_device *raid_device;
4369 u16 handle;
4370 u32 state;
4371 int rc;
4372 struct MPT2SAS_TARGET *sas_target_priv_data;
4373
4374 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
4375 return;
4376
4377 handle = le16_to_cpu(event_data->VolDevHandle);
4378 state = le32_to_cpu(event_data->NewValue);
4379 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4380 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4381 le32_to_cpu(event_data->PreviousValue), state));
4382
4383 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4384 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
4385 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4386
4387 switch (state) {
4388 case MPI2_RAID_VOL_STATE_MISSING:
4389 case MPI2_RAID_VOL_STATE_FAILED:
4390 if (!raid_device)
4391 break;
4392 if (raid_device->starget) {
4393 sas_target_priv_data = raid_device->starget->hostdata;
4394 sas_target_priv_data->deleted = 1;
4395 scsi_remove_target(&raid_device->starget->dev);
4396 }
4397 _scsih_raid_device_remove(ioc, raid_device);
4398 break;
4399
4400 case MPI2_RAID_VOL_STATE_ONLINE:
4401 case MPI2_RAID_VOL_STATE_DEGRADED:
4402 case MPI2_RAID_VOL_STATE_OPTIMAL:
4403 if (raid_device)
4404 break;
4405
4406 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
4407 if (!wwid) {
4408 printk(MPT2SAS_ERR_FMT
4409 "failure at %s:%d/%s()!\n", ioc->name,
4410 __FILE__, __LINE__, __func__);
4411 break;
4412 }
4413
4414 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
4415 if (!raid_device) {
4416 printk(MPT2SAS_ERR_FMT
4417 "failure at %s:%d/%s()!\n", ioc->name,
4418 __FILE__, __LINE__, __func__);
4419 break;
4420 }
4421
4422 raid_device->id = ioc->sas_id++;
4423 raid_device->channel = RAID_CHANNEL;
4424 raid_device->handle = handle;
4425 raid_device->wwid = wwid;
4426 _scsih_raid_device_add(ioc, raid_device);
4427 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
4428 raid_device->id, 0);
4429 if (rc)
4430 _scsih_raid_device_remove(ioc, raid_device);
4431 break;
4432
4433 case MPI2_RAID_VOL_STATE_INITIALIZING:
4434 default:
4435 break;
4436 }
4437}
4438
4439/**
4440 * _scsih_sas_ir_physical_disk_event - PD event
4441 * @ioc: per adapter object
4442 * @event_data: event data payload
4443 * Context: user.
4444 *
4445 * Return nothing.
4446 */
4447static void
4448_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4449 Mpi2EventDataIrPhysicalDisk_t *event_data)
4450{
4451 u16 handle;
4452 u32 state;
4453 struct _sas_device *sas_device;
4454 unsigned long flags;
4455
4456 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
4457 return;
4458
4459 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
4460 state = le32_to_cpu(event_data->NewValue);
4461
4462 dewtprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: handle(0x%04x), "
4463 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
4464 le32_to_cpu(event_data->PreviousValue), state));
4465
4466 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4467 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4468 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4469
4470 switch (state) {
4471#if 0
4472 case MPI2_RAID_PD_STATE_OFFLINE:
4473 if (sas_device)
4474 _scsih_remove_device(ioc, handle);
4475 break;
4476#endif
4477 case MPI2_RAID_PD_STATE_ONLINE:
4478 case MPI2_RAID_PD_STATE_DEGRADED:
4479 case MPI2_RAID_PD_STATE_REBUILDING:
4480 case MPI2_RAID_PD_STATE_OPTIMAL:
4481 if (sas_device)
4482 sas_device->hidden_raid_component = 1;
4483 else
4484 _scsih_add_device(ioc, handle, 0, 1);
4485 break;
4486
4487 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
4488 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
4489 case MPI2_RAID_PD_STATE_HOT_SPARE:
4490 default:
4491 break;
4492 }
4493}
4494
4495#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4496/**
4497 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
4498 * @ioc: per adapter object
4499 * @event_data: event data payload
4500 * Context: user.
4501 *
4502 * Return nothing.
4503 */
4504static void
4505_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
4506 Mpi2EventDataIrOperationStatus_t *event_data)
4507{
4508 char *reason_str = NULL;
4509
4510 switch (event_data->RAIDOperation) {
4511 case MPI2_EVENT_IR_RAIDOP_RESYNC:
4512 reason_str = "resync";
4513 break;
4514 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
4515 reason_str = "online capacity expansion";
4516 break;
4517 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
4518 reason_str = "consistency check";
4519 break;
4520 default:
4521 reason_str = "unknown reason";
4522 break;
4523 }
4524
4525 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
4526 "\thandle(0x%04x), percent complete(%d)\n",
4527 ioc->name, reason_str,
4528 le16_to_cpu(event_data->VolDevHandle),
4529 event_data->PercentComplete);
4530}
4531#endif
4532
4533/**
4534 * _scsih_sas_ir_operation_status_event - handle RAID operation events
4535 * @ioc: per adapter object
4536 * @VF_ID:
4537 * @event_data: event data payload
4538 * Context: user.
4539 *
4540 * Return nothing.
4541 */
4542static void
4543_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4544 Mpi2EventDataIrOperationStatus_t *event_data)
4545{
4546#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4547 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
4548 _scsih_sas_ir_operation_status_event_debug(ioc, event_data);
4549#endif
4550}
4551
4552/**
4553 * _scsih_task_set_full - handle task set full
4554 * @ioc: per adapter object
4555 * @event_data: event data payload
4556 * Context: user.
4557 *
4558 * Throttle back qdepth.
4559 */
4560static void
4561_scsih_task_set_full(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
4562 Mpi2EventDataTaskSetFull_t *event_data)
4563{
4564 unsigned long flags;
4565 struct _sas_device *sas_device;
4566 static struct _raid_device *raid_device;
4567 struct scsi_device *sdev;
4568 int depth;
4569 u16 current_depth;
4570 u16 handle;
4571 int id, channel;
4572 u64 sas_address;
4573
4574 current_depth = le16_to_cpu(event_data->CurrentDepth);
4575 handle = le16_to_cpu(event_data->DevHandle);
4576 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4577 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4578 if (!sas_device) {
4579 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4580 return;
4581 }
4582 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4583 id = sas_device->id;
4584 channel = sas_device->channel;
4585 sas_address = sas_device->sas_address;
4586
4587 /* if hidden raid component, then change to volume characteristics */
4588 if (sas_device->hidden_raid_component && sas_device->volume_handle) {
4589 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4590 raid_device = _scsih_raid_device_find_by_handle(
4591 ioc, sas_device->volume_handle);
4592 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4593 if (raid_device) {
4594 id = raid_device->id;
4595 channel = raid_device->channel;
4596 handle = raid_device->handle;
4597 sas_address = raid_device->wwid;
4598 }
4599 }
4600
4601 if (ioc->logging_level & MPT_DEBUG_TASK_SET_FULL)
4602 starget_printk(KERN_DEBUG, sas_device->starget, "task set "
4603 "full: handle(0x%04x), sas_addr(0x%016llx), depth(%d)\n",
4604 handle, (unsigned long long)sas_address, current_depth);
4605
4606 shost_for_each_device(sdev, ioc->shost) {
4607 if (sdev->id == id && sdev->channel == channel) {
4608 if (current_depth > sdev->queue_depth) {
4609 if (ioc->logging_level &
4610 MPT_DEBUG_TASK_SET_FULL)
4611 sdev_printk(KERN_INFO, sdev, "strange "
4612 "observation, the queue depth is"
4613 " (%d) meanwhile fw queue depth "
4614 "is (%d)\n", sdev->queue_depth,
4615 current_depth);
4616 continue;
4617 }
4618 depth = scsi_track_queue_full(sdev,
4619 current_depth - 1);
4620 if (depth > 0)
4621 sdev_printk(KERN_INFO, sdev, "Queue depth "
4622 "reduced to (%d)\n", depth);
4623 else if (depth < 0)
4624 sdev_printk(KERN_INFO, sdev, "Tagged Command "
4625 "Queueing is being disabled\n");
4626 else if (depth == 0)
4627 if (ioc->logging_level &
4628 MPT_DEBUG_TASK_SET_FULL)
4629 sdev_printk(KERN_INFO, sdev,
4630 "Queue depth not changed yet\n");
4631 }
4632 }
4633}
4634
4635/**
4636 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
4637 * @ioc: per adapter object
4638 * @sas_address: sas address
4639 * @slot: enclosure slot id
4640 * @handle: device handle
4641 *
4642 * After host reset, find out whether devices are still responding.
4643 * Used in _scsi_remove_unresponsive_sas_devices.
4644 *
4645 * Return nothing.
4646 */
4647static void
4648_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4649 u16 slot, u16 handle)
4650{
4651 struct MPT2SAS_TARGET *sas_target_priv_data;
4652 struct scsi_target *starget;
4653 struct _sas_device *sas_device;
4654 unsigned long flags;
4655
4656 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4657 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
4658 if (sas_device->sas_address == sas_address &&
4659 sas_device->slot == slot && sas_device->starget) {
4660 sas_device->responding = 1;
4661 starget_printk(KERN_INFO, sas_device->starget,
4662 "handle(0x%04x), sas_addr(0x%016llx), enclosure "
4663 "logical id(0x%016llx), slot(%d)\n", handle,
4664 (unsigned long long)sas_device->sas_address,
4665 (unsigned long long)
4666 sas_device->enclosure_logical_id,
4667 sas_device->slot);
4668 if (sas_device->handle == handle)
4669 goto out;
4670 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4671 sas_device->handle);
4672 sas_device->handle = handle;
4673 starget = sas_device->starget;
4674 sas_target_priv_data = starget->hostdata;
4675 sas_target_priv_data->handle = handle;
4676 goto out;
4677 }
4678 }
4679 out:
4680 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4681}
4682
4683/**
4684 * _scsih_search_responding_sas_devices -
4685 * @ioc: per adapter object
4686 *
4687 * After host reset, find out whether devices are still responding.
4688 * If not remove.
4689 *
4690 * Return nothing.
4691 */
4692static void
4693_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
4694{
4695 Mpi2SasDevicePage0_t sas_device_pg0;
4696 Mpi2ConfigReply_t mpi_reply;
4697 u16 ioc_status;
4698 __le64 sas_address;
4699 u16 handle;
4700 u32 device_info;
4701 u16 slot;
4702
4703 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4704
4705 if (list_empty(&ioc->sas_device_list))
4706 return;
4707
4708 handle = 0xFFFF;
4709 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
4710 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
4711 handle))) {
4712 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4713 MPI2_IOCSTATUS_MASK;
4714 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4715 break;
4716 handle = le16_to_cpu(sas_device_pg0.DevHandle);
4717 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4718 if (!(_scsih_is_end_device(device_info)))
4719 continue;
4720 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4721 slot = le16_to_cpu(sas_device_pg0.Slot);
4722 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
4723 handle);
4724 }
4725}
4726
4727/**
4728 * _scsih_mark_responding_raid_device - mark a raid_device as responding
4729 * @ioc: per adapter object
4730 * @wwid: world wide identifier for raid volume
4731 * @handle: device handle
4732 *
4733 * After host reset, find out whether devices are still responding.
4734 * Used in _scsi_remove_unresponsive_raid_devices.
4735 *
4736 * Return nothing.
4737 */
4738static void
4739_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
4740 u16 handle)
4741{
4742 struct MPT2SAS_TARGET *sas_target_priv_data;
4743 struct scsi_target *starget;
4744 struct _raid_device *raid_device;
4745 unsigned long flags;
4746
4747 spin_lock_irqsave(&ioc->raid_device_lock, flags);
4748 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
4749 if (raid_device->wwid == wwid && raid_device->starget) {
4750 raid_device->responding = 1;
4751 starget_printk(KERN_INFO, raid_device->starget,
4752 "handle(0x%04x), wwid(0x%016llx)\n", handle,
4753 (unsigned long long)raid_device->wwid);
4754 if (raid_device->handle == handle)
4755 goto out;
4756 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
4757 raid_device->handle);
4758 raid_device->handle = handle;
4759 starget = raid_device->starget;
4760 sas_target_priv_data = starget->hostdata;
4761 sas_target_priv_data->handle = handle;
4762 goto out;
4763 }
4764 }
4765 out:
4766 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
4767}
4768
4769/**
4770 * _scsih_search_responding_raid_devices -
4771 * @ioc: per adapter object
4772 *
4773 * After host reset, find out whether devices are still responding.
4774 * If not remove.
4775 *
4776 * Return nothing.
4777 */
4778static void
4779_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
4780{
4781 Mpi2RaidVolPage1_t volume_pg1;
4782 Mpi2ConfigReply_t mpi_reply;
4783 u16 ioc_status;
4784 u16 handle;
4785
4786 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4787
4788 if (list_empty(&ioc->raid_device_list))
4789 return;
4790
4791 handle = 0xFFFF;
4792 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
4793 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
4794 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4795 MPI2_IOCSTATUS_MASK;
4796 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4797 break;
4798 handle = le16_to_cpu(volume_pg1.DevHandle);
4799 _scsih_mark_responding_raid_device(ioc,
4800 le64_to_cpu(volume_pg1.WWID), handle);
4801 }
4802}
4803
4804/**
4805 * _scsih_mark_responding_expander - mark a expander as responding
4806 * @ioc: per adapter object
4807 * @sas_address: sas address
4808 * @handle:
4809 *
4810 * After host reset, find out whether devices are still responding.
4811 * Used in _scsi_remove_unresponsive_expanders.
4812 *
4813 * Return nothing.
4814 */
4815static void
4816_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4817 u16 handle)
4818{
4819 struct _sas_node *sas_expander;
4820 unsigned long flags;
4821
4822 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4823 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
4824 if (sas_expander->sas_address == sas_address) {
4825 sas_expander->responding = 1;
4826 if (sas_expander->handle != handle) {
4827 printk(KERN_INFO "old handle(0x%04x)\n",
4828 sas_expander->handle);
4829 sas_expander->handle = handle;
4830 }
4831 goto out;
4832 }
4833 }
4834 out:
4835 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4836}
4837
4838/**
4839 * _scsih_search_responding_expanders -
4840 * @ioc: per adapter object
4841 *
4842 * After host reset, find out whether devices are still responding.
4843 * If not remove.
4844 *
4845 * Return nothing.
4846 */
4847static void
4848_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
4849{
4850 Mpi2ExpanderPage0_t expander_pg0;
4851 Mpi2ConfigReply_t mpi_reply;
4852 u16 ioc_status;
4853 __le64 sas_address;
4854 u16 handle;
4855
4856 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
4857
4858 if (list_empty(&ioc->sas_expander_list))
4859 return;
4860
4861 handle = 0xFFFF;
4862 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4863 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
4864
4865 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4866 MPI2_IOCSTATUS_MASK;
4867 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
4868 break;
4869
4870 handle = le16_to_cpu(expander_pg0.DevHandle);
4871 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4872 printk(KERN_INFO "\texpander present: handle(0x%04x), "
4873 "sas_addr(0x%016llx)\n", handle,
4874 (unsigned long long)sas_address);
4875 _scsih_mark_responding_expander(ioc, sas_address, handle);
4876 }
4877
4878}
4879
4880/**
4881 * _scsih_remove_unresponding_devices - removing unresponding devices
4882 * @ioc: per adapter object
4883 *
4884 * Return nothing.
4885 */
4886static void
4887_scsih_remove_unresponding_devices(struct MPT2SAS_ADAPTER *ioc)
4888{
4889 struct _sas_device *sas_device, *sas_device_next;
4890 struct _sas_node *sas_expander, *sas_expander_next;
4891 struct _raid_device *raid_device, *raid_device_next;
4892 unsigned long flags;
4893
4894 _scsih_search_responding_sas_devices(ioc);
4895 _scsih_search_responding_raid_devices(ioc);
4896 _scsih_search_responding_expanders(ioc);
4897
4898 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4899 ioc->shost_recovery = 0;
4900 if (ioc->shost->shost_state == SHOST_RECOVERY) {
4901 printk(MPT2SAS_INFO_FMT "putting controller into "
4902 "SHOST_RUNNING\n", ioc->name);
4903 scsi_host_set_state(ioc->shost, SHOST_RUNNING);
4904 }
4905 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4906
4907 list_for_each_entry_safe(sas_device, sas_device_next,
4908 &ioc->sas_device_list, list) {
4909 if (sas_device->responding) {
4910 sas_device->responding = 0;
4911 continue;
4912 }
4913 if (sas_device->starget)
4914 starget_printk(KERN_INFO, sas_device->starget,
4915 "removing: handle(0x%04x), sas_addr(0x%016llx), "
4916 "enclosure logical id(0x%016llx), slot(%d)\n",
4917 sas_device->handle,
4918 (unsigned long long)sas_device->sas_address,
4919 (unsigned long long)
4920 sas_device->enclosure_logical_id,
4921 sas_device->slot);
4922 _scsih_remove_device(ioc, sas_device->handle);
4923 }
4924
4925 list_for_each_entry_safe(raid_device, raid_device_next,
4926 &ioc->raid_device_list, list) {
4927 if (raid_device->responding) {
4928 raid_device->responding = 0;
4929 continue;
4930 }
4931 if (raid_device->starget) {
4932 starget_printk(KERN_INFO, raid_device->starget,
4933 "removing: handle(0x%04x), wwid(0x%016llx)\n",
4934 raid_device->handle,
4935 (unsigned long long)raid_device->wwid);
4936 scsi_remove_target(&raid_device->starget->dev);
4937 }
4938 _scsih_raid_device_remove(ioc, raid_device);
4939 }
4940
4941 list_for_each_entry_safe(sas_expander, sas_expander_next,
4942 &ioc->sas_expander_list, list) {
4943 if (sas_expander->responding) {
4944 sas_expander->responding = 0;
4945 continue;
4946 }
4947 printk("\tremoving expander: handle(0x%04x), "
4948 " sas_addr(0x%016llx)\n", sas_expander->handle,
4949 (unsigned long long)sas_expander->sas_address);
4950 _scsih_expander_remove(ioc, sas_expander->handle);
4951 }
4952}
4953
4954/**
4955 * _firmware_event_work - delayed task for processing firmware events
4956 * @ioc: per adapter object
4957 * @work: equal to the fw_event_work object
4958 * Context: user.
4959 *
4960 * Return nothing.
4961 */
4962static void
4963_firmware_event_work(struct work_struct *work)
4964{
4965 struct fw_event_work *fw_event = container_of(work,
4966 struct fw_event_work, work.work);
4967 unsigned long flags;
4968 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
4969
4970 /* This is invoked by calling _scsih_queue_rescan(). */
4971 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
4972 _scsih_fw_event_free(ioc, fw_event);
4973 _scsih_sas_host_refresh(ioc, 1);
4974 _scsih_remove_unresponding_devices(ioc);
4975 return;
4976 }
4977
4978 /* the queue is being flushed so ignore this event */
4979 spin_lock_irqsave(&ioc->fw_event_lock, flags);
4980 if (ioc->fw_events_off || ioc->remove_host) {
4981 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
4982 _scsih_fw_event_free(ioc, fw_event);
4983 return;
4984 }
4985 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
4986
4987 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4988 if (ioc->shost_recovery) {
4989 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4990 _scsih_fw_event_requeue(ioc, fw_event, 1000);
4991 return;
4992 }
4993 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4994
4995 switch (fw_event->event) {
4996 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
4997 _scsih_sas_topology_change_event(ioc, fw_event->VF_ID,
4998 fw_event->event_data, fw_event);
4999 break;
5000 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5001 _scsih_sas_device_status_change_event(ioc, fw_event->VF_ID,
5002 fw_event->event_data);
5003 break;
5004 case MPI2_EVENT_SAS_DISCOVERY:
5005 _scsih_sas_discovery_event(ioc, fw_event->VF_ID,
5006 fw_event->event_data);
5007 break;
5008 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5009 _scsih_sas_broadcast_primative_event(ioc, fw_event->VF_ID,
5010 fw_event->event_data);
5011 break;
5012 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5013 _scsih_sas_enclosure_dev_status_change_event(ioc,
5014 fw_event->VF_ID, fw_event->event_data);
5015 break;
5016 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5017 _scsih_sas_ir_config_change_event(ioc, fw_event->VF_ID,
5018 fw_event->event_data);
5019 break;
5020 case MPI2_EVENT_IR_VOLUME:
5021 _scsih_sas_ir_volume_event(ioc, fw_event->VF_ID,
5022 fw_event->event_data);
5023 break;
5024 case MPI2_EVENT_IR_PHYSICAL_DISK:
5025 _scsih_sas_ir_physical_disk_event(ioc, fw_event->VF_ID,
5026 fw_event->event_data);
5027 break;
5028 case MPI2_EVENT_IR_OPERATION_STATUS:
5029 _scsih_sas_ir_operation_status_event(ioc, fw_event->VF_ID,
5030 fw_event->event_data);
5031 break;
5032 case MPI2_EVENT_TASK_SET_FULL:
5033 _scsih_task_set_full(ioc, fw_event->VF_ID,
5034 fw_event->event_data);
5035 break;
5036 }
5037 _scsih_fw_event_free(ioc, fw_event);
5038}
5039
5040/**
5041 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
5042 * @ioc: per adapter object
5043 * @VF_ID: virtual function id
5044 * @reply: reply message frame(lower 32bit addr)
5045 * Context: interrupt.
5046 *
5047 * This function merely adds a new work task into ioc->firmware_event_thread.
5048 * The tasks are worked from _firmware_event_work in user context.
5049 *
5050 * Return nothing.
5051 */
5052void
5053mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
5054{
5055 struct fw_event_work *fw_event;
5056 Mpi2EventNotificationReply_t *mpi_reply;
5057 unsigned long flags;
5058 u16 event;
5059
5060 /* events turned off due to host reset or driver unloading */
5061 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5062 if (ioc->fw_events_off || ioc->remove_host) {
5063 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5064 return;
5065 }
5066 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5067
5068 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5069 event = le16_to_cpu(mpi_reply->Event);
5070
5071 switch (event) {
5072 /* handle these */
5073 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
5074 {
5075 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
5076 (Mpi2EventDataSasBroadcastPrimitive_t *)
5077 mpi_reply->EventData;
5078
5079 if (baen_data->Primitive !=
5080 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
5081 ioc->broadcast_aen_busy)
5082 return;
5083 ioc->broadcast_aen_busy = 1;
5084 break;
5085 }
5086
5087 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
5088 _scsih_check_topo_delete_events(ioc,
5089 (Mpi2EventDataSasTopologyChangeList_t *)
5090 mpi_reply->EventData);
5091 break;
5092
5093 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
5094 case MPI2_EVENT_IR_OPERATION_STATUS:
5095 case MPI2_EVENT_SAS_DISCOVERY:
5096 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
5097 case MPI2_EVENT_IR_VOLUME:
5098 case MPI2_EVENT_IR_PHYSICAL_DISK:
5099 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
5100 case MPI2_EVENT_TASK_SET_FULL:
5101 break;
5102
5103 default: /* ignore the rest */
5104 return;
5105 }
5106
5107 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
5108 if (!fw_event) {
5109 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5110 ioc->name, __FILE__, __LINE__, __func__);
5111 return;
5112 }
5113 fw_event->event_data =
5114 kzalloc(mpi_reply->EventDataLength*4, GFP_ATOMIC);
5115 if (!fw_event->event_data) {
5116 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5117 ioc->name, __FILE__, __LINE__, __func__);
5118 kfree(fw_event);
5119 return;
5120 }
5121
5122 memcpy(fw_event->event_data, mpi_reply->EventData,
5123 mpi_reply->EventDataLength*4);
5124 fw_event->ioc = ioc;
5125 fw_event->VF_ID = VF_ID;
5126 fw_event->event = event;
5127 _scsih_fw_event_add(ioc, fw_event);
5128}
5129
5130/* shost template */
5131static struct scsi_host_template scsih_driver_template = {
5132 .module = THIS_MODULE,
5133 .name = "Fusion MPT SAS Host",
5134 .proc_name = MPT2SAS_DRIVER_NAME,
5135 .queuecommand = scsih_qcmd,
5136 .target_alloc = scsih_target_alloc,
5137 .slave_alloc = scsih_slave_alloc,
5138 .slave_configure = scsih_slave_configure,
5139 .target_destroy = scsih_target_destroy,
5140 .slave_destroy = scsih_slave_destroy,
5141 .change_queue_depth = scsih_change_queue_depth,
5142 .change_queue_type = scsih_change_queue_type,
5143 .eh_abort_handler = scsih_abort,
5144 .eh_device_reset_handler = scsih_dev_reset,
5145 .eh_host_reset_handler = scsih_host_reset,
5146 .bios_param = scsih_bios_param,
5147 .can_queue = 1,
5148 .this_id = -1,
5149 .sg_tablesize = MPT2SAS_SG_DEPTH,
5150 .max_sectors = 8192,
5151 .cmd_per_lun = 7,
5152 .use_clustering = ENABLE_CLUSTERING,
5153 .shost_attrs = mpt2sas_host_attrs,
5154 .sdev_attrs = mpt2sas_dev_attrs,
5155};
5156
5157/**
5158 * _scsih_expander_node_remove - removing expander device from list.
5159 * @ioc: per adapter object
5160 * @sas_expander: the sas_device object
5161 * Context: Calling function should acquire ioc->sas_node_lock.
5162 *
5163 * Removing object and freeing associated memory from the
5164 * ioc->sas_expander_list.
5165 *
5166 * Return nothing.
5167 */
5168static void
5169_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
5170 struct _sas_node *sas_expander)
5171{
5172 struct _sas_port *mpt2sas_port;
5173 struct _sas_device *sas_device;
5174 struct _sas_node *expander_sibling;
5175 unsigned long flags;
5176
5177 if (!sas_expander)
5178 return;
5179
5180 /* remove sibling ports attached to this expander */
5181 retry_device_search:
5182 list_for_each_entry(mpt2sas_port,
5183 &sas_expander->sas_port_list, port_list) {
5184 if (mpt2sas_port->remote_identify.device_type ==
5185 SAS_END_DEVICE) {
5186 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5187 sas_device =
5188 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5189 mpt2sas_port->remote_identify.sas_address);
5190 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5191 if (!sas_device)
5192 continue;
5193 _scsih_remove_device(ioc, sas_device->handle);
5194 goto retry_device_search;
5195 }
5196 }
5197
5198 retry_expander_search:
5199 list_for_each_entry(mpt2sas_port,
5200 &sas_expander->sas_port_list, port_list) {
5201
5202 if (mpt2sas_port->remote_identify.device_type ==
5203 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
5204 mpt2sas_port->remote_identify.device_type ==
5205 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER) {
5206
5207 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5208 expander_sibling =
5209 mpt2sas_scsih_expander_find_by_sas_address(
5210 ioc, mpt2sas_port->remote_identify.sas_address);
5211 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5212 if (!expander_sibling)
5213 continue;
5214 _scsih_expander_remove(ioc, expander_sibling->handle);
5215 goto retry_expander_search;
5216 }
5217 }
5218
5219 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
5220 sas_expander->parent_handle);
5221
5222 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
5223 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
5224 sas_expander->handle, (unsigned long long)
5225 sas_expander->sas_address);
5226
5227 list_del(&sas_expander->list);
5228 kfree(sas_expander->phy);
5229 kfree(sas_expander);
5230}
5231
5232/**
5233 * scsih_remove - detach and remove add host
5234 * @pdev: PCI device struct
5235 *
5236 * Return nothing.
5237 */
5238static void __devexit
5239scsih_remove(struct pci_dev *pdev)
5240{
5241 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5242 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5243 struct _sas_port *mpt2sas_port;
5244 struct _sas_device *sas_device;
5245 struct _sas_node *expander_sibling;
5246 struct workqueue_struct *wq;
5247 unsigned long flags;
5248
5249 ioc->remove_host = 1;
5250 _scsih_fw_event_off(ioc);
5251
5252 spin_lock_irqsave(&ioc->fw_event_lock, flags);
5253 wq = ioc->firmware_event_thread;
5254 ioc->firmware_event_thread = NULL;
5255 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
5256 if (wq)
5257 destroy_workqueue(wq);
5258
5259 /* free ports attached to the sas_host */
5260 retry_again:
5261 list_for_each_entry(mpt2sas_port,
5262 &ioc->sas_hba.sas_port_list, port_list) {
5263 if (mpt2sas_port->remote_identify.device_type ==
5264 SAS_END_DEVICE) {
5265 sas_device =
5266 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5267 mpt2sas_port->remote_identify.sas_address);
5268 if (sas_device) {
5269 _scsih_remove_device(ioc, sas_device->handle);
5270 goto retry_again;
5271 }
5272 } else {
5273 expander_sibling =
5274 mpt2sas_scsih_expander_find_by_sas_address(ioc,
5275 mpt2sas_port->remote_identify.sas_address);
5276 if (expander_sibling) {
5277 _scsih_expander_remove(ioc,
5278 expander_sibling->handle);
5279 goto retry_again;
5280 }
5281 }
5282 }
5283
5284 /* free phys attached to the sas_host */
5285 if (ioc->sas_hba.num_phys) {
5286 kfree(ioc->sas_hba.phy);
5287 ioc->sas_hba.phy = NULL;
5288 ioc->sas_hba.num_phys = 0;
5289 }
5290
5291 sas_remove_host(shost);
5292 mpt2sas_base_detach(ioc);
5293 list_del(&ioc->list);
5294 scsi_remove_host(shost);
5295 scsi_host_put(shost);
5296}
5297
5298/**
5299 * _scsih_probe_boot_devices - reports 1st device
5300 * @ioc: per adapter object
5301 *
5302 * If specified in bios page 2, this routine reports the 1st
5303 * device scsi-ml or sas transport for persistent boot device
5304 * purposes. Please refer to function _scsih_determine_boot_device()
5305 */
5306static void
5307_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
5308{
5309 u8 is_raid;
5310 void *device;
5311 struct _sas_device *sas_device;
5312 struct _raid_device *raid_device;
5313 u16 handle, parent_handle;
5314 u64 sas_address;
5315 unsigned long flags;
5316 int rc;
5317
5318 device = NULL;
5319 if (ioc->req_boot_device.device) {
5320 device = ioc->req_boot_device.device;
5321 is_raid = ioc->req_boot_device.is_raid;
5322 } else if (ioc->req_alt_boot_device.device) {
5323 device = ioc->req_alt_boot_device.device;
5324 is_raid = ioc->req_alt_boot_device.is_raid;
5325 } else if (ioc->current_boot_device.device) {
5326 device = ioc->current_boot_device.device;
5327 is_raid = ioc->current_boot_device.is_raid;
5328 }
5329
5330 if (!device)
5331 return;
5332
5333 if (is_raid) {
5334 raid_device = device;
5335 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5336 raid_device->id, 0);
5337 if (rc)
5338 _scsih_raid_device_remove(ioc, raid_device);
5339 } else {
5340 sas_device = device;
5341 handle = sas_device->handle;
5342 parent_handle = sas_device->parent_handle;
5343 sas_address = sas_device->sas_address;
5344 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5345 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5346 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5347 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
5348 sas_device->parent_handle)) {
5349 _scsih_sas_device_remove(ioc, sas_device);
5350 } else if (!sas_device->starget) {
5351 mpt2sas_transport_port_remove(ioc, sas_address,
5352 parent_handle);
5353 _scsih_sas_device_remove(ioc, sas_device);
5354 }
5355 }
5356}
5357
5358/**
5359 * _scsih_probe_raid - reporting raid volumes to scsi-ml
5360 * @ioc: per adapter object
5361 *
5362 * Called during initial loading of the driver.
5363 */
5364static void
5365_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
5366{
5367 struct _raid_device *raid_device, *raid_next;
5368 int rc;
5369
5370 list_for_each_entry_safe(raid_device, raid_next,
5371 &ioc->raid_device_list, list) {
5372 if (raid_device->starget)
5373 continue;
5374 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5375 raid_device->id, 0);
5376 if (rc)
5377 _scsih_raid_device_remove(ioc, raid_device);
5378 }
5379}
5380
5381/**
5382 * _scsih_probe_sas - reporting raid volumes to sas transport
5383 * @ioc: per adapter object
5384 *
5385 * Called during initial loading of the driver.
5386 */
5387static void
5388_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
5389{
5390 struct _sas_device *sas_device, *next;
5391 unsigned long flags;
5392 u16 handle, parent_handle;
5393 u64 sas_address;
5394
5395 /* SAS Device List */
5396 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
5397 list) {
5398 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5399 list_move_tail(&sas_device->list, &ioc->sas_device_list);
5400 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5401
5402 handle = sas_device->handle;
5403 parent_handle = sas_device->parent_handle;
5404 sas_address = sas_device->sas_address;
5405 if (!mpt2sas_transport_port_add(ioc, handle, parent_handle)) {
5406 _scsih_sas_device_remove(ioc, sas_device);
5407 } else if (!sas_device->starget) {
5408 mpt2sas_transport_port_remove(ioc, sas_address,
5409 parent_handle);
5410 _scsih_sas_device_remove(ioc, sas_device);
5411 }
5412 }
5413}
5414
5415/**
5416 * _scsih_probe_devices - probing for devices
5417 * @ioc: per adapter object
5418 *
5419 * Called during initial loading of the driver.
5420 */
5421static void
5422_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
5423{
5424 u16 volume_mapping_flags =
5425 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
5426 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
5427
5428 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
5429 return; /* return when IOC doesn't support initiator mode */
5430
5431 _scsih_probe_boot_devices(ioc);
5432
5433 if (ioc->ir_firmware) {
5434 if ((volume_mapping_flags &
5435 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
5436 _scsih_probe_sas(ioc);
5437 _scsih_probe_raid(ioc);
5438 } else {
5439 _scsih_probe_raid(ioc);
5440 _scsih_probe_sas(ioc);
5441 }
5442 } else
5443 _scsih_probe_sas(ioc);
5444}
5445
5446/**
5447 * scsih_probe - attach and add scsi host
5448 * @pdev: PCI device struct
5449 * @id: pci device id
5450 *
5451 * Returns 0 success, anything else error.
5452 */
5453static int
5454scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
5455{
5456 struct MPT2SAS_ADAPTER *ioc;
5457 struct Scsi_Host *shost;
5458
5459 shost = scsi_host_alloc(&scsih_driver_template,
5460 sizeof(struct MPT2SAS_ADAPTER));
5461 if (!shost)
5462 return -ENODEV;
5463
5464 /* init local params */
5465 ioc = shost_priv(shost);
5466 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
5467 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06005468 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06005469 ioc->shost = shost;
5470 ioc->id = mpt_ids++;
5471 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
5472 ioc->pdev = pdev;
5473 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
5474 ioc->tm_cb_idx = tm_cb_idx;
5475 ioc->ctl_cb_idx = ctl_cb_idx;
5476 ioc->base_cb_idx = base_cb_idx;
5477 ioc->transport_cb_idx = transport_cb_idx;
5478 ioc->config_cb_idx = config_cb_idx;
5479 ioc->logging_level = logging_level;
5480 /* misc semaphores and spin locks */
5481 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
5482 spin_lock_init(&ioc->scsi_lookup_lock);
5483 spin_lock_init(&ioc->sas_device_lock);
5484 spin_lock_init(&ioc->sas_node_lock);
5485 spin_lock_init(&ioc->fw_event_lock);
5486 spin_lock_init(&ioc->raid_device_lock);
5487
5488 INIT_LIST_HEAD(&ioc->sas_device_list);
5489 INIT_LIST_HEAD(&ioc->sas_device_init_list);
5490 INIT_LIST_HEAD(&ioc->sas_expander_list);
5491 INIT_LIST_HEAD(&ioc->fw_event_list);
5492 INIT_LIST_HEAD(&ioc->raid_device_list);
5493 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
5494
5495 /* init shost parameters */
5496 shost->max_cmd_len = 16;
5497 shost->max_lun = max_lun;
5498 shost->transportt = mpt2sas_transport_template;
5499 shost->unique_id = ioc->id;
5500
5501 if ((scsi_add_host(shost, &pdev->dev))) {
5502 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5503 ioc->name, __FILE__, __LINE__, __func__);
5504 list_del(&ioc->list);
5505 goto out_add_shost_fail;
5506 }
5507
5508 /* event thread */
5509 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
5510 "fw_event%d", ioc->id);
5511 ioc->firmware_event_thread = create_singlethread_workqueue(
5512 ioc->firmware_event_name);
5513 if (!ioc->firmware_event_thread) {
5514 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5515 ioc->name, __FILE__, __LINE__, __func__);
5516 goto out_thread_fail;
5517 }
5518
5519 ioc->wait_for_port_enable_to_complete = 1;
5520 if ((mpt2sas_base_attach(ioc))) {
5521 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5522 ioc->name, __FILE__, __LINE__, __func__);
5523 goto out_attach_fail;
5524 }
5525
5526 ioc->wait_for_port_enable_to_complete = 0;
5527 _scsih_probe_devices(ioc);
5528 return 0;
5529
5530 out_attach_fail:
5531 destroy_workqueue(ioc->firmware_event_thread);
5532 out_thread_fail:
5533 list_del(&ioc->list);
5534 scsi_remove_host(shost);
5535 out_add_shost_fail:
5536 return -ENODEV;
5537}
5538
5539#ifdef CONFIG_PM
5540/**
5541 * scsih_suspend - power management suspend main entry point
5542 * @pdev: PCI device struct
5543 * @state: PM state change to (usually PCI_D3)
5544 *
5545 * Returns 0 success, anything else error.
5546 */
5547static int
5548scsih_suspend(struct pci_dev *pdev, pm_message_t state)
5549{
5550 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5551 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5552 u32 device_state;
5553
5554 flush_scheduled_work();
5555 scsi_block_requests(shost);
5556 device_state = pci_choose_state(pdev, state);
5557 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
5558 "operating state [D%d]\n", ioc->name, pdev,
5559 pci_name(pdev), device_state);
5560
5561 mpt2sas_base_free_resources(ioc);
5562 pci_save_state(pdev);
5563 pci_disable_device(pdev);
5564 pci_set_power_state(pdev, device_state);
5565 return 0;
5566}
5567
5568/**
5569 * scsih_resume - power management resume main entry point
5570 * @pdev: PCI device struct
5571 *
5572 * Returns 0 success, anything else error.
5573 */
5574static int
5575scsih_resume(struct pci_dev *pdev)
5576{
5577 struct Scsi_Host *shost = pci_get_drvdata(pdev);
5578 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
5579 u32 device_state = pdev->current_state;
5580 int r;
5581
5582 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
5583 "operating state [D%d]\n", ioc->name, pdev,
5584 pci_name(pdev), device_state);
5585
5586 pci_set_power_state(pdev, PCI_D0);
5587 pci_enable_wake(pdev, PCI_D0, 0);
5588 pci_restore_state(pdev);
5589 ioc->pdev = pdev;
5590 r = mpt2sas_base_map_resources(ioc);
5591 if (r)
5592 return r;
5593
5594 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
5595 scsi_unblock_requests(shost);
5596 return 0;
5597}
5598#endif /* CONFIG_PM */
5599
5600
5601static struct pci_driver scsih_driver = {
5602 .name = MPT2SAS_DRIVER_NAME,
5603 .id_table = scsih_pci_table,
5604 .probe = scsih_probe,
5605 .remove = __devexit_p(scsih_remove),
5606#ifdef CONFIG_PM
5607 .suspend = scsih_suspend,
5608 .resume = scsih_resume,
5609#endif
5610};
5611
5612
5613/**
5614 * scsih_init - main entry point for this driver.
5615 *
5616 * Returns 0 success, anything else error.
5617 */
5618static int __init
5619scsih_init(void)
5620{
5621 int error;
5622
5623 mpt_ids = 0;
5624 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
5625 MPT2SAS_DRIVER_VERSION);
5626
5627 mpt2sas_transport_template =
5628 sas_attach_transport(&mpt2sas_transport_functions);
5629 if (!mpt2sas_transport_template)
5630 return -ENODEV;
5631
5632 mpt2sas_base_initialize_callback_handler();
5633
5634 /* queuecommand callback hander */
5635 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(scsih_io_done);
5636
5637 /* task managment callback handler */
5638 tm_cb_idx = mpt2sas_base_register_callback_handler(scsih_tm_done);
5639
5640 /* base internal commands callback handler */
5641 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
5642
5643 /* transport internal commands callback handler */
5644 transport_cb_idx = mpt2sas_base_register_callback_handler(
5645 mpt2sas_transport_done);
5646
5647 /* configuration page API internal commands callback handler */
5648 config_cb_idx = mpt2sas_base_register_callback_handler(
5649 mpt2sas_config_done);
5650
5651 /* ctl module callback handler */
5652 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
5653
5654 mpt2sas_ctl_init();
5655
5656 error = pci_register_driver(&scsih_driver);
5657 if (error)
5658 sas_release_transport(mpt2sas_transport_template);
5659
5660 return error;
5661}
5662
5663/**
5664 * scsih_exit - exit point for this driver (when it is a module).
5665 *
5666 * Returns 0 success, anything else error.
5667 */
5668static void __exit
5669scsih_exit(void)
5670{
5671 printk(KERN_INFO "mpt2sas version %s unloading\n",
5672 MPT2SAS_DRIVER_VERSION);
5673
5674 pci_unregister_driver(&scsih_driver);
5675
5676 sas_release_transport(mpt2sas_transport_template);
5677 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
5678 mpt2sas_base_release_callback_handler(tm_cb_idx);
5679 mpt2sas_base_release_callback_handler(base_cb_idx);
5680 mpt2sas_base_release_callback_handler(transport_cb_idx);
5681 mpt2sas_base_release_callback_handler(config_cb_idx);
5682 mpt2sas_base_release_callback_handler(ctl_cb_idx);
5683
5684 mpt2sas_ctl_exit();
5685}
5686
5687module_init(scsih_init);
5688module_exit(scsih_exit);