[SCSI] mpt2sas: Target Reset will be issued from Interrupt context.
[linux-2.6-block.git] / drivers / scsi / mpt2sas / mpt2sas_transport.c
CommitLineData
635374e7
EM
1/*
2 * SAS Transport Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_transport.c
19d3ebe3 5 * Copyright (C) 2007-2009 LSI Corporation
635374e7
EM
6 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
44#include <linux/module.h>
45#include <linux/kernel.h>
46#include <linux/init.h>
47#include <linux/errno.h>
48#include <linux/sched.h>
49#include <linux/workqueue.h>
50#include <linux/delay.h>
51#include <linux/pci.h>
52
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
57#include <scsi/scsi_transport_sas.h>
58#include <scsi/scsi_dbg.h>
59
60#include "mpt2sas_base.h"
61/**
62 * _transport_sas_node_find_by_handle - sas node search
63 * @ioc: per adapter object
64 * @handle: expander or hba handle (assigned by firmware)
65 * Context: Calling function should acquire ioc->sas_node_lock.
66 *
67 * Search for either hba phys or expander device based on handle, then returns
68 * the sas_node object.
69 */
70static struct _sas_node *
71_transport_sas_node_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
72{
73 int i;
74
75 for (i = 0; i < ioc->sas_hba.num_phys; i++)
76 if (ioc->sas_hba.phy[i].handle == handle)
77 return &ioc->sas_hba;
78
79 return mpt2sas_scsih_expander_find_by_handle(ioc, handle);
80}
81
82/**
83 * _transport_convert_phy_link_rate -
84 * @link_rate: link rate returned from mpt firmware
85 *
86 * Convert link_rate from mpi fusion into sas_transport form.
87 */
88static enum sas_linkrate
89_transport_convert_phy_link_rate(u8 link_rate)
90{
91 enum sas_linkrate rc;
92
93 switch (link_rate) {
94 case MPI2_SAS_NEG_LINK_RATE_1_5:
95 rc = SAS_LINK_RATE_1_5_GBPS;
96 break;
97 case MPI2_SAS_NEG_LINK_RATE_3_0:
98 rc = SAS_LINK_RATE_3_0_GBPS;
99 break;
100 case MPI2_SAS_NEG_LINK_RATE_6_0:
101 rc = SAS_LINK_RATE_6_0_GBPS;
102 break;
103 case MPI2_SAS_NEG_LINK_RATE_PHY_DISABLED:
104 rc = SAS_PHY_DISABLED;
105 break;
106 case MPI2_SAS_NEG_LINK_RATE_NEGOTIATION_FAILED:
107 rc = SAS_LINK_RATE_FAILED;
108 break;
109 case MPI2_SAS_NEG_LINK_RATE_PORT_SELECTOR:
110 rc = SAS_SATA_PORT_SELECTOR;
111 break;
112 case MPI2_SAS_NEG_LINK_RATE_SMP_RESET_IN_PROGRESS:
113 rc = SAS_PHY_RESET_IN_PROGRESS;
114 break;
115 default:
116 case MPI2_SAS_NEG_LINK_RATE_SATA_OOB_COMPLETE:
117 case MPI2_SAS_NEG_LINK_RATE_UNKNOWN_LINK_RATE:
118 rc = SAS_LINK_RATE_UNKNOWN;
119 break;
120 }
121 return rc;
122}
123
124/**
125 * _transport_set_identify - set identify for phys and end devices
126 * @ioc: per adapter object
127 * @handle: device handle
128 * @identify: sas identify info
129 *
130 * Populates sas identify info.
131 *
132 * Returns 0 for success, non-zero for failure.
133 */
134static int
135_transport_set_identify(struct MPT2SAS_ADAPTER *ioc, u16 handle,
136 struct sas_identify *identify)
137{
138 Mpi2SasDevicePage0_t sas_device_pg0;
139 Mpi2ConfigReply_t mpi_reply;
140 u32 device_info;
141 u32 ioc_status;
142
155dd4c7
KD
143 if (ioc->shost_recovery) {
144 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
145 __func__, ioc->name);
146 return -EFAULT;
147 }
148
635374e7
EM
149 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
150 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
155dd4c7 152
635374e7 153 ioc->name, __FILE__, __LINE__, __func__);
155dd4c7 154 return -ENXIO;
635374e7
EM
155 }
156
157 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
158 MPI2_IOCSTATUS_MASK;
159 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
160 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
161 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
162 __FILE__, __LINE__, __func__);
155dd4c7 163 return -EIO;
635374e7
EM
164 }
165
166 memset(identify, 0, sizeof(identify));
167 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
168
169 /* sas_address */
170 identify->sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
171
172 /* device_type */
173 switch (device_info & MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) {
174 case MPI2_SAS_DEVICE_INFO_NO_DEVICE:
175 identify->device_type = SAS_PHY_UNUSED;
176 break;
177 case MPI2_SAS_DEVICE_INFO_END_DEVICE:
178 identify->device_type = SAS_END_DEVICE;
179 break;
180 case MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER:
181 identify->device_type = SAS_EDGE_EXPANDER_DEVICE;
182 break;
183 case MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER:
184 identify->device_type = SAS_FANOUT_EXPANDER_DEVICE;
185 break;
186 }
187
188 /* initiator_port_protocols */
189 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_INITIATOR)
190 identify->initiator_port_protocols |= SAS_PROTOCOL_SSP;
191 if (device_info & MPI2_SAS_DEVICE_INFO_STP_INITIATOR)
192 identify->initiator_port_protocols |= SAS_PROTOCOL_STP;
193 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_INITIATOR)
194 identify->initiator_port_protocols |= SAS_PROTOCOL_SMP;
195 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_HOST)
196 identify->initiator_port_protocols |= SAS_PROTOCOL_SATA;
197
198 /* target_port_protocols */
199 if (device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET)
200 identify->target_port_protocols |= SAS_PROTOCOL_SSP;
201 if (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
202 identify->target_port_protocols |= SAS_PROTOCOL_STP;
203 if (device_info & MPI2_SAS_DEVICE_INFO_SMP_TARGET)
204 identify->target_port_protocols |= SAS_PROTOCOL_SMP;
205 if (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
206 identify->target_port_protocols |= SAS_PROTOCOL_SATA;
207
208 return 0;
209}
210
211/**
212 * mpt2sas_transport_done - internal transport layer callback handler.
213 * @ioc: per adapter object
214 * @smid: system request message index
7b936b02 215 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
216 * @reply: reply message frame(lower 32bit addr)
217 *
218 * Callback handler when sending internal generated transport cmds.
219 * The callback index passed is `ioc->transport_cb_idx`
220 *
77e63ed4
KD
221 * Return 1 meaning mf should be freed from _base_interrupt
222 * 0 means the mf is freed from this function.
635374e7 223 */
77e63ed4 224u8
7b936b02 225mpt2sas_transport_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
635374e7
EM
226 u32 reply)
227{
228 MPI2DefaultReply_t *mpi_reply;
229
230 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
231 if (ioc->transport_cmds.status == MPT2_CMD_NOT_USED)
77e63ed4 232 return 1;
635374e7 233 if (ioc->transport_cmds.smid != smid)
77e63ed4 234 return 1;
635374e7
EM
235 ioc->transport_cmds.status |= MPT2_CMD_COMPLETE;
236 if (mpi_reply) {
237 memcpy(ioc->transport_cmds.reply, mpi_reply,
238 mpi_reply->MsgLength*4);
239 ioc->transport_cmds.status |= MPT2_CMD_REPLY_VALID;
240 }
241 ioc->transport_cmds.status &= ~MPT2_CMD_PENDING;
242 complete(&ioc->transport_cmds.done);
77e63ed4 243 return 1;
635374e7
EM
244}
245
246/* report manufacture request structure */
247struct rep_manu_request{
248 u8 smp_frame_type;
249 u8 function;
250 u8 reserved;
251 u8 request_length;
252};
253
254/* report manufacture reply structure */
255struct rep_manu_reply{
256 u8 smp_frame_type; /* 0x41 */
257 u8 function; /* 0x01 */
258 u8 function_result;
259 u8 response_length;
260 u16 expander_change_count;
261 u8 reserved0[2];
262 u8 sas_format:1;
263 u8 reserved1:7;
264 u8 reserved2[3];
265 u8 vendor_id[SAS_EXPANDER_VENDOR_ID_LEN];
266 u8 product_id[SAS_EXPANDER_PRODUCT_ID_LEN];
267 u8 product_rev[SAS_EXPANDER_PRODUCT_REV_LEN];
268 u8 component_vendor_id[SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN];
269 u16 component_id;
270 u8 component_revision_id;
271 u8 reserved3;
272 u8 vendor_specific[8];
273};
274
275/**
d5d135b3 276 * _transport_expander_report_manufacture - obtain SMP report_manufacture
635374e7
EM
277 * @ioc: per adapter object
278 * @sas_address: expander sas address
279 * @edev: the sas_expander_device object
280 *
281 * Fills in the sas_expander_device object when SMP port is created.
282 *
283 * Returns 0 for success, non-zero for failure.
284 */
285static int
d5d135b3 286_transport_expander_report_manufacture(struct MPT2SAS_ADAPTER *ioc,
635374e7
EM
287 u64 sas_address, struct sas_expander_device *edev)
288{
289 Mpi2SmpPassthroughRequest_t *mpi_request;
290 Mpi2SmpPassthroughReply_t *mpi_reply;
291 struct rep_manu_reply *manufacture_reply;
292 struct rep_manu_request *manufacture_request;
293 int rc;
294 u16 smid;
295 u32 ioc_state;
296 unsigned long timeleft;
297 void *psge;
298 u32 sgl_flags;
299 u8 issue_reset = 0;
635374e7
EM
300 void *data_out = NULL;
301 dma_addr_t data_out_dma;
302 u32 sz;
303 u64 *sas_address_le;
304 u16 wait_state_count;
305
155dd4c7 306 if (ioc->shost_recovery) {
635374e7
EM
307 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
308 __func__, ioc->name);
309 return -EFAULT;
310 }
635374e7
EM
311
312 mutex_lock(&ioc->transport_cmds.mutex);
313
314 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
315 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n",
316 ioc->name, __func__);
317 rc = -EAGAIN;
318 goto out;
319 }
320 ioc->transport_cmds.status = MPT2_CMD_PENDING;
321
322 wait_state_count = 0;
323 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
324 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
325 if (wait_state_count++ == 10) {
326 printk(MPT2SAS_ERR_FMT
327 "%s: failed due to ioc not operational\n",
328 ioc->name, __func__);
329 rc = -EFAULT;
330 goto out;
331 }
332 ssleep(1);
333 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
334 printk(MPT2SAS_INFO_FMT "%s: waiting for "
335 "operational state(count=%d)\n", ioc->name,
336 __func__, wait_state_count);
337 }
338 if (wait_state_count)
339 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
340 ioc->name, __func__);
341
342 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
343 if (!smid) {
344 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
345 ioc->name, __func__);
346 rc = -EAGAIN;
347 goto out;
348 }
349
350 rc = 0;
351 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
352 ioc->transport_cmds.smid = smid;
353
354 sz = sizeof(struct rep_manu_request) + sizeof(struct rep_manu_reply);
355 data_out = pci_alloc_consistent(ioc->pdev, sz, &data_out_dma);
356
357 if (!data_out) {
358 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
359 __LINE__, __func__);
360 rc = -ENOMEM;
361 mpt2sas_base_free_smid(ioc, smid);
362 goto out;
363 }
364
365 manufacture_request = data_out;
366 manufacture_request->smp_frame_type = 0x40;
367 manufacture_request->function = 1;
368 manufacture_request->reserved = 0;
369 manufacture_request->request_length = 0;
370
371 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
372 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
373 mpi_request->PhysicalPort = 0xFF;
7b936b02
KD
374 mpi_request->VF_ID = 0; /* TODO */
375 mpi_request->VP_ID = 0;
635374e7
EM
376 sas_address_le = (u64 *)&mpi_request->SASAddress;
377 *sas_address_le = cpu_to_le64(sas_address);
378 mpi_request->RequestDataLength = sizeof(struct rep_manu_request);
379 psge = &mpi_request->SGL;
380
381 /* WRITE sgel first */
382 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
383 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
384 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
385 ioc->base_add_sg_single(psge, sgl_flags |
386 sizeof(struct rep_manu_request), data_out_dma);
387
388 /* incr sgel */
389 psge += ioc->sge_size;
390
391 /* READ sgel last */
392 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
393 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
394 MPI2_SGE_FLAGS_END_OF_LIST);
395 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
396 ioc->base_add_sg_single(psge, sgl_flags |
397 sizeof(struct rep_manu_reply), data_out_dma +
398 sizeof(struct rep_manu_request));
399
400 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
401 "send to sas_addr(0x%016llx)\n", ioc->name,
402 (unsigned long long)sas_address));
7b936b02 403 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
404 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
405 10*HZ);
406
407 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
408 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
409 ioc->name, __func__);
410 _debug_dump_mf(mpi_request,
411 sizeof(Mpi2SmpPassthroughRequest_t)/4);
412 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
413 issue_reset = 1;
414 goto issue_host_reset;
415 }
416
417 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "report_manufacture - "
418 "complete\n", ioc->name));
419
420 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
421 u8 *tmp;
422
423 mpi_reply = ioc->transport_cmds.reply;
424
425 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
426 "report_manufacture - reply data transfer size(%d)\n",
427 ioc->name, le16_to_cpu(mpi_reply->ResponseDataLength)));
428
429 if (le16_to_cpu(mpi_reply->ResponseDataLength) !=
430 sizeof(struct rep_manu_reply))
431 goto out;
432
433 manufacture_reply = data_out + sizeof(struct rep_manu_request);
434 strncpy(edev->vendor_id, manufacture_reply->vendor_id,
435 SAS_EXPANDER_VENDOR_ID_LEN);
436 strncpy(edev->product_id, manufacture_reply->product_id,
437 SAS_EXPANDER_PRODUCT_ID_LEN);
438 strncpy(edev->product_rev, manufacture_reply->product_rev,
439 SAS_EXPANDER_PRODUCT_REV_LEN);
440 edev->level = manufacture_reply->sas_format;
441 if (manufacture_reply->sas_format) {
442 strncpy(edev->component_vendor_id,
443 manufacture_reply->component_vendor_id,
444 SAS_EXPANDER_COMPONENT_VENDOR_ID_LEN);
445 tmp = (u8 *)&manufacture_reply->component_id;
446 edev->component_id = tmp[0] << 8 | tmp[1];
447 edev->component_revision_id =
448 manufacture_reply->component_revision_id;
449 }
450 } else
451 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
452 "report_manufacture - no reply\n", ioc->name));
453
454 issue_host_reset:
455 if (issue_reset)
456 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
457 FORCE_BIG_HAMMER);
458 out:
459 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
460 if (data_out)
461 pci_free_consistent(ioc->pdev, sz, data_out, data_out_dma);
462
463 mutex_unlock(&ioc->transport_cmds.mutex);
464 return rc;
465}
466
467/**
468 * mpt2sas_transport_port_add - insert port to the list
469 * @ioc: per adapter object
470 * @handle: handle of attached device
471 * @parent_handle: parent handle(either hba or expander)
472 * Context: This function will acquire ioc->sas_node_lock.
473 *
474 * Adding new port object to the sas_node->sas_port_list.
475 *
476 * Returns mpt2sas_port.
477 */
478struct _sas_port *
479mpt2sas_transport_port_add(struct MPT2SAS_ADAPTER *ioc, u16 handle,
480 u16 parent_handle)
481{
482 struct _sas_phy *mpt2sas_phy, *next;
483 struct _sas_port *mpt2sas_port;
484 unsigned long flags;
485 struct _sas_node *sas_node;
486 struct sas_rphy *rphy;
487 int i;
488 struct sas_port *port;
489
490 if (!parent_handle)
491 return NULL;
492
493 mpt2sas_port = kzalloc(sizeof(struct _sas_port),
494 GFP_KERNEL);
495 if (!mpt2sas_port) {
496 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
497 ioc->name, __FILE__, __LINE__, __func__);
498 return NULL;
499 }
500
501 INIT_LIST_HEAD(&mpt2sas_port->port_list);
502 INIT_LIST_HEAD(&mpt2sas_port->phy_list);
503 spin_lock_irqsave(&ioc->sas_node_lock, flags);
504 sas_node = _transport_sas_node_find_by_handle(ioc, parent_handle);
505 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
506
507 if (!sas_node) {
508 printk(MPT2SAS_ERR_FMT "%s: Could not find parent(0x%04x)!\n",
509 ioc->name, __func__, parent_handle);
510 goto out_fail;
511 }
512
513 mpt2sas_port->handle = parent_handle;
514 mpt2sas_port->sas_address = sas_node->sas_address;
515 if ((_transport_set_identify(ioc, handle,
516 &mpt2sas_port->remote_identify))) {
517 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
518 ioc->name, __FILE__, __LINE__, __func__);
519 goto out_fail;
520 }
521
522 if (mpt2sas_port->remote_identify.device_type == SAS_PHY_UNUSED) {
523 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
524 ioc->name, __FILE__, __LINE__, __func__);
525 goto out_fail;
526 }
527
528 for (i = 0; i < sas_node->num_phys; i++) {
529 if (sas_node->phy[i].remote_identify.sas_address !=
530 mpt2sas_port->remote_identify.sas_address)
531 continue;
532 list_add_tail(&sas_node->phy[i].port_siblings,
533 &mpt2sas_port->phy_list);
534 mpt2sas_port->num_phys++;
535 }
536
537 if (!mpt2sas_port->num_phys) {
538 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
539 ioc->name, __FILE__, __LINE__, __func__);
540 goto out_fail;
541 }
542
543 port = sas_port_alloc_num(sas_node->parent_dev);
544 if ((sas_port_add(port))) {
545 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
546 ioc->name, __FILE__, __LINE__, __func__);
547 goto out_fail;
548 }
549
550 list_for_each_entry(mpt2sas_phy, &mpt2sas_port->phy_list,
551 port_siblings) {
552 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
553 dev_printk(KERN_INFO, &port->dev, "add: handle(0x%04x)"
554 ", sas_addr(0x%016llx), phy(%d)\n", handle,
555 (unsigned long long)
556 mpt2sas_port->remote_identify.sas_address,
557 mpt2sas_phy->phy_id);
558 sas_port_add_phy(port, mpt2sas_phy->phy);
559 }
560
561 mpt2sas_port->port = port;
562 if (mpt2sas_port->remote_identify.device_type == SAS_END_DEVICE)
563 rphy = sas_end_device_alloc(port);
564 else
565 rphy = sas_expander_alloc(port,
566 mpt2sas_port->remote_identify.device_type);
567
568 rphy->identify = mpt2sas_port->remote_identify;
569 if ((sas_rphy_add(rphy))) {
570 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
571 ioc->name, __FILE__, __LINE__, __func__);
572 }
573 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
574 dev_printk(KERN_INFO, &rphy->dev, "add: handle(0x%04x), "
575 "sas_addr(0x%016llx)\n", handle,
576 (unsigned long long)
577 mpt2sas_port->remote_identify.sas_address);
578 mpt2sas_port->rphy = rphy;
579 spin_lock_irqsave(&ioc->sas_node_lock, flags);
580 list_add_tail(&mpt2sas_port->port_list, &sas_node->sas_port_list);
581 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
582
583 /* fill in report manufacture */
584 if (mpt2sas_port->remote_identify.device_type ==
585 MPI2_SAS_DEVICE_INFO_EDGE_EXPANDER ||
586 mpt2sas_port->remote_identify.device_type ==
587 MPI2_SAS_DEVICE_INFO_FANOUT_EXPANDER)
d5d135b3 588 _transport_expander_report_manufacture(ioc,
635374e7
EM
589 mpt2sas_port->remote_identify.sas_address,
590 rphy_to_expander_device(rphy));
591
592 return mpt2sas_port;
593
594 out_fail:
595 list_for_each_entry_safe(mpt2sas_phy, next, &mpt2sas_port->phy_list,
596 port_siblings)
597 list_del(&mpt2sas_phy->port_siblings);
598 kfree(mpt2sas_port);
599 return NULL;
600}
601
602/**
603 * mpt2sas_transport_port_remove - remove port from the list
604 * @ioc: per adapter object
605 * @sas_address: sas address of attached device
606 * @parent_handle: handle to the upstream parent(either hba or expander)
607 * Context: This function will acquire ioc->sas_node_lock.
608 *
609 * Removing object and freeing associated memory from the
610 * ioc->sas_port_list.
611 *
612 * Return nothing.
613 */
614void
615mpt2sas_transport_port_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
616 u16 parent_handle)
617{
618 int i;
619 unsigned long flags;
620 struct _sas_port *mpt2sas_port, *next;
621 struct _sas_node *sas_node;
622 u8 found = 0;
623 struct _sas_phy *mpt2sas_phy, *next_phy;
624
625 spin_lock_irqsave(&ioc->sas_node_lock, flags);
626 sas_node = _transport_sas_node_find_by_handle(ioc, parent_handle);
627 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
628 if (!sas_node)
629 return;
630 list_for_each_entry_safe(mpt2sas_port, next, &sas_node->sas_port_list,
631 port_list) {
632 if (mpt2sas_port->remote_identify.sas_address != sas_address)
633 continue;
634 found = 1;
635 list_del(&mpt2sas_port->port_list);
636 goto out;
637 }
638 out:
639 if (!found)
640 return;
641
642 for (i = 0; i < sas_node->num_phys; i++) {
643 if (sas_node->phy[i].remote_identify.sas_address == sas_address)
644 memset(&sas_node->phy[i].remote_identify, 0 ,
645 sizeof(struct sas_identify));
646 }
647
648 list_for_each_entry_safe(mpt2sas_phy, next_phy,
649 &mpt2sas_port->phy_list, port_siblings) {
650 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
651 dev_printk(KERN_INFO, &mpt2sas_port->port->dev,
652 "remove: parent_handle(0x%04x), "
653 "sas_addr(0x%016llx), phy(%d)\n", parent_handle,
654 (unsigned long long)
655 mpt2sas_port->remote_identify.sas_address,
656 mpt2sas_phy->phy_id);
657 sas_port_delete_phy(mpt2sas_port->port, mpt2sas_phy->phy);
658 list_del(&mpt2sas_phy->port_siblings);
659 }
660 sas_port_delete(mpt2sas_port->port);
661 kfree(mpt2sas_port);
662}
663
664/**
665 * mpt2sas_transport_add_host_phy - report sas_host phy to transport
666 * @ioc: per adapter object
667 * @mpt2sas_phy: mpt2sas per phy object
668 * @phy_pg0: sas phy page 0
669 * @parent_dev: parent device class object
670 *
671 * Returns 0 for success, non-zero for failure.
672 */
673int
674mpt2sas_transport_add_host_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
675 *mpt2sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev)
676{
677 struct sas_phy *phy;
678 int phy_index = mpt2sas_phy->phy_id;
679
680
681 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
682 phy = sas_phy_alloc(parent_dev, phy_index);
683 if (!phy) {
684 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
685 ioc->name, __FILE__, __LINE__, __func__);
686 return -1;
687 }
688 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
689 &mpt2sas_phy->identify))) {
690 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
691 ioc->name, __FILE__, __LINE__, __func__);
692 return -1;
693 }
694 phy->identify = mpt2sas_phy->identify;
695 mpt2sas_phy->attached_handle = le16_to_cpu(phy_pg0.AttachedDevHandle);
696 if (mpt2sas_phy->attached_handle)
697 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
698 &mpt2sas_phy->remote_identify);
699 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
700 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
701 phy_pg0.NegotiatedLinkRate & MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
702 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
703 phy_pg0.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
704 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
705 phy_pg0.HwLinkRate >> 4);
706 phy->minimum_linkrate = _transport_convert_phy_link_rate(
707 phy_pg0.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
708 phy->maximum_linkrate = _transport_convert_phy_link_rate(
709 phy_pg0.ProgrammedLinkRate >> 4);
710
711 if ((sas_phy_add(phy))) {
712 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
713 ioc->name, __FILE__, __LINE__, __func__);
714 sas_phy_free(phy);
715 return -1;
716 }
717 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
718 dev_printk(KERN_INFO, &phy->dev,
719 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
720 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
721 mpt2sas_phy->handle, (unsigned long long)
722 mpt2sas_phy->identify.sas_address,
723 mpt2sas_phy->attached_handle,
724 (unsigned long long)
725 mpt2sas_phy->remote_identify.sas_address);
726 mpt2sas_phy->phy = phy;
727 return 0;
728}
729
730
731/**
732 * mpt2sas_transport_add_expander_phy - report expander phy to transport
733 * @ioc: per adapter object
734 * @mpt2sas_phy: mpt2sas per phy object
735 * @expander_pg1: expander page 1
736 * @parent_dev: parent device class object
737 *
738 * Returns 0 for success, non-zero for failure.
739 */
740int
741mpt2sas_transport_add_expander_phy(struct MPT2SAS_ADAPTER *ioc, struct _sas_phy
742 *mpt2sas_phy, Mpi2ExpanderPage1_t expander_pg1, struct device *parent_dev)
743{
744 struct sas_phy *phy;
745 int phy_index = mpt2sas_phy->phy_id;
746
747 INIT_LIST_HEAD(&mpt2sas_phy->port_siblings);
748 phy = sas_phy_alloc(parent_dev, phy_index);
749 if (!phy) {
750 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
751 ioc->name, __FILE__, __LINE__, __func__);
752 return -1;
753 }
754 if ((_transport_set_identify(ioc, mpt2sas_phy->handle,
755 &mpt2sas_phy->identify))) {
756 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
757 ioc->name, __FILE__, __LINE__, __func__);
758 return -1;
759 }
760 phy->identify = mpt2sas_phy->identify;
761 mpt2sas_phy->attached_handle =
762 le16_to_cpu(expander_pg1.AttachedDevHandle);
763 if (mpt2sas_phy->attached_handle)
764 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
765 &mpt2sas_phy->remote_identify);
766 phy->identify.phy_identifier = mpt2sas_phy->phy_id;
767 phy->negotiated_linkrate = _transport_convert_phy_link_rate(
768 expander_pg1.NegotiatedLinkRate &
769 MPI2_SAS_NEG_LINK_RATE_MASK_PHYSICAL);
770 phy->minimum_linkrate_hw = _transport_convert_phy_link_rate(
771 expander_pg1.HwLinkRate & MPI2_SAS_HWRATE_MIN_RATE_MASK);
772 phy->maximum_linkrate_hw = _transport_convert_phy_link_rate(
773 expander_pg1.HwLinkRate >> 4);
774 phy->minimum_linkrate = _transport_convert_phy_link_rate(
775 expander_pg1.ProgrammedLinkRate & MPI2_SAS_PRATE_MIN_RATE_MASK);
776 phy->maximum_linkrate = _transport_convert_phy_link_rate(
777 expander_pg1.ProgrammedLinkRate >> 4);
778
779 if ((sas_phy_add(phy))) {
780 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
781 ioc->name, __FILE__, __LINE__, __func__);
782 sas_phy_free(phy);
783 return -1;
784 }
785 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
786 dev_printk(KERN_INFO, &phy->dev,
787 "add: handle(0x%04x), sas_addr(0x%016llx)\n"
788 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
789 mpt2sas_phy->handle, (unsigned long long)
790 mpt2sas_phy->identify.sas_address,
791 mpt2sas_phy->attached_handle,
792 (unsigned long long)
793 mpt2sas_phy->remote_identify.sas_address);
794 mpt2sas_phy->phy = phy;
795 return 0;
796}
797
798/**
cc0f5207 799 * mpt2sas_transport_update_links - refreshing phy link changes
635374e7
EM
800 * @ioc: per adapter object
801 * @handle: handle to sas_host or expander
802 * @attached_handle: attached device handle
803 * @phy_numberv: phy number
804 * @link_rate: new link rate
805 *
806 * Returns nothing.
807 */
808void
cc0f5207 809mpt2sas_transport_update_links(struct MPT2SAS_ADAPTER *ioc,
635374e7
EM
810 u16 handle, u16 attached_handle, u8 phy_number, u8 link_rate)
811{
812 unsigned long flags;
813 struct _sas_node *sas_node;
814 struct _sas_phy *mpt2sas_phy;
815
155dd4c7
KD
816 if (ioc->shost_recovery) {
817 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
818 __func__, ioc->name);
819 return;
820 }
821
635374e7
EM
822 spin_lock_irqsave(&ioc->sas_node_lock, flags);
823 sas_node = _transport_sas_node_find_by_handle(ioc, handle);
824 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
825 if (!sas_node)
826 return;
827
828 mpt2sas_phy = &sas_node->phy[phy_number];
829 mpt2sas_phy->attached_handle = attached_handle;
830 if (attached_handle && (link_rate >= MPI2_SAS_NEG_LINK_RATE_1_5))
831 _transport_set_identify(ioc, mpt2sas_phy->attached_handle,
832 &mpt2sas_phy->remote_identify);
833 else
834 memset(&mpt2sas_phy->remote_identify, 0 , sizeof(struct
835 sas_identify));
836
837 if (mpt2sas_phy->phy)
838 mpt2sas_phy->phy->negotiated_linkrate =
839 _transport_convert_phy_link_rate(link_rate);
840
841 if ((ioc->logging_level & MPT_DEBUG_TRANSPORT))
842 dev_printk(KERN_INFO, &mpt2sas_phy->phy->dev,
843 "refresh: handle(0x%04x), sas_addr(0x%016llx),\n"
844 "\tlink_rate(0x%02x), phy(%d)\n"
845 "\tattached_handle(0x%04x), sas_addr(0x%016llx)\n",
846 handle, (unsigned long long)
847 mpt2sas_phy->identify.sas_address, link_rate,
848 phy_number, attached_handle,
849 (unsigned long long)
850 mpt2sas_phy->remote_identify.sas_address);
851}
852
853static inline void *
854phy_to_ioc(struct sas_phy *phy)
855{
856 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
857 return shost_priv(shost);
858}
859
860static inline void *
861rphy_to_ioc(struct sas_rphy *rphy)
862{
863 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
864 return shost_priv(shost);
865}
866
867/**
d5d135b3 868 * _transport_get_linkerrors -
635374e7
EM
869 * @phy: The sas phy object
870 *
871 * Only support sas_host direct attached phys.
872 * Returns 0 for success, non-zero for failure.
873 *
874 */
875static int
d5d135b3 876_transport_get_linkerrors(struct sas_phy *phy)
635374e7
EM
877{
878 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
879 struct _sas_phy *mpt2sas_phy;
880 Mpi2ConfigReply_t mpi_reply;
881 Mpi2SasPhyPage1_t phy_pg1;
882 int i;
883
884 for (i = 0, mpt2sas_phy = NULL; i < ioc->sas_hba.num_phys &&
885 !mpt2sas_phy; i++) {
886 if (ioc->sas_hba.phy[i].phy != phy)
887 continue;
888 mpt2sas_phy = &ioc->sas_hba.phy[i];
889 }
890
891 if (!mpt2sas_phy) /* this phy not on sas_host */
892 return -EINVAL;
893
894 if ((mpt2sas_config_get_phy_pg1(ioc, &mpi_reply, &phy_pg1,
895 mpt2sas_phy->phy_id))) {
896 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
897 ioc->name, __FILE__, __LINE__, __func__);
898 return -ENXIO;
899 }
900
901 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
902 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
903 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
904 mpt2sas_phy->phy_id,
905 le16_to_cpu(mpi_reply.IOCStatus),
906 le32_to_cpu(mpi_reply.IOCLogInfo));
907
908 phy->invalid_dword_count = le32_to_cpu(phy_pg1.InvalidDwordCount);
909 phy->running_disparity_error_count =
910 le32_to_cpu(phy_pg1.RunningDisparityErrorCount);
911 phy->loss_of_dword_sync_count =
912 le32_to_cpu(phy_pg1.LossDwordSynchCount);
913 phy->phy_reset_problem_count =
914 le32_to_cpu(phy_pg1.PhyResetProblemCount);
915 return 0;
916}
917
918/**
d5d135b3 919 * _transport_get_enclosure_identifier -
635374e7
EM
920 * @phy: The sas phy object
921 *
922 * Obtain the enclosure logical id for an expander.
923 * Returns 0 for success, non-zero for failure.
924 */
925static int
d5d135b3 926_transport_get_enclosure_identifier(struct sas_rphy *rphy, u64 *identifier)
635374e7
EM
927{
928 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
929 struct _sas_node *sas_expander;
930 unsigned long flags;
931
932 spin_lock_irqsave(&ioc->sas_node_lock, flags);
933 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
934 rphy->identify.sas_address);
935 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
936
937 if (!sas_expander)
938 return -ENXIO;
939
940 *identifier = sas_expander->enclosure_logical_id;
941 return 0;
942}
943
944/**
d5d135b3 945 * _transport_get_bay_identifier -
635374e7
EM
946 * @phy: The sas phy object
947 *
948 * Returns the slot id for a device that resides inside an enclosure.
949 */
950static int
d5d135b3 951_transport_get_bay_identifier(struct sas_rphy *rphy)
635374e7
EM
952{
953 struct MPT2SAS_ADAPTER *ioc = rphy_to_ioc(rphy);
954 struct _sas_device *sas_device;
955 unsigned long flags;
956
957 spin_lock_irqsave(&ioc->sas_device_lock, flags);
958 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
959 rphy->identify.sas_address);
960 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
961
962 if (!sas_device)
963 return -ENXIO;
964
965 return sas_device->slot;
966}
967
968/**
d5d135b3 969 * _transport_phy_reset -
635374e7
EM
970 * @phy: The sas phy object
971 * @hard_reset:
972 *
973 * Only support sas_host direct attached phys.
974 * Returns 0 for success, non-zero for failure.
975 */
976static int
d5d135b3 977_transport_phy_reset(struct sas_phy *phy, int hard_reset)
635374e7
EM
978{
979 struct MPT2SAS_ADAPTER *ioc = phy_to_ioc(phy);
980 struct _sas_phy *mpt2sas_phy;
981 Mpi2SasIoUnitControlReply_t mpi_reply;
982 Mpi2SasIoUnitControlRequest_t mpi_request;
983 int i;
984
985 for (i = 0, mpt2sas_phy = NULL; i < ioc->sas_hba.num_phys &&
986 !mpt2sas_phy; i++) {
987 if (ioc->sas_hba.phy[i].phy != phy)
988 continue;
989 mpt2sas_phy = &ioc->sas_hba.phy[i];
990 }
991
992 if (!mpt2sas_phy) /* this phy not on sas_host */
993 return -EINVAL;
994
995 memset(&mpi_request, 0, sizeof(Mpi2SasIoUnitControlReply_t));
996 mpi_request.Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
997 mpi_request.Operation = hard_reset ?
998 MPI2_SAS_OP_PHY_HARD_RESET : MPI2_SAS_OP_PHY_LINK_RESET;
999 mpi_request.PhyNum = mpt2sas_phy->phy_id;
1000
1001 if ((mpt2sas_base_sas_iounit_control(ioc, &mpi_reply, &mpi_request))) {
1002 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1003 ioc->name, __FILE__, __LINE__, __func__);
1004 return -ENXIO;
1005 }
1006
1007 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo)
1008 printk(MPT2SAS_INFO_FMT "phy(%d), ioc_status"
1009 "(0x%04x), loginfo(0x%08x)\n", ioc->name,
1010 mpt2sas_phy->phy_id,
1011 le16_to_cpu(mpi_reply.IOCStatus),
1012 le32_to_cpu(mpi_reply.IOCLogInfo));
1013
1014 return 0;
1015}
1016
1017/**
d5d135b3 1018 * _transport_smp_handler - transport portal for smp passthru
635374e7
EM
1019 * @shost: shost object
1020 * @rphy: sas transport rphy object
1021 * @req:
1022 *
1023 * This used primarily for smp_utils.
1024 * Example:
1025 * smp_rep_general /sys/class/bsg/expander-5:0
1026 */
1027static int
d5d135b3 1028_transport_smp_handler(struct Scsi_Host *shost, struct sas_rphy *rphy,
635374e7
EM
1029 struct request *req)
1030{
1031 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1032 Mpi2SmpPassthroughRequest_t *mpi_request;
1033 Mpi2SmpPassthroughReply_t *mpi_reply;
1034 int rc;
1035 u16 smid;
1036 u32 ioc_state;
1037 unsigned long timeleft;
1038 void *psge;
1039 u32 sgl_flags;
1040 u8 issue_reset = 0;
635374e7
EM
1041 dma_addr_t dma_addr_in = 0;
1042 dma_addr_t dma_addr_out = 0;
1043 u16 wait_state_count;
1044 struct request *rsp = req->next_rq;
1045
1046 if (!rsp) {
1047 printk(MPT2SAS_ERR_FMT "%s: the smp response space is "
1048 "missing\n", ioc->name, __func__);
1049 return -EINVAL;
1050 }
1051
1052 /* do we need to support multiple segments? */
1053 if (req->bio->bi_vcnt > 1 || rsp->bio->bi_vcnt > 1) {
1054 printk(MPT2SAS_ERR_FMT "%s: multiple segments req %u %u, "
1055 "rsp %u %u\n", ioc->name, __func__, req->bio->bi_vcnt,
b0790410 1056 blk_rq_bytes(req), rsp->bio->bi_vcnt, blk_rq_bytes(rsp));
635374e7
EM
1057 return -EINVAL;
1058 }
1059
155dd4c7 1060 if (ioc->shost_recovery) {
635374e7
EM
1061 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
1062 __func__, ioc->name);
1063 return -EFAULT;
1064 }
635374e7
EM
1065
1066 rc = mutex_lock_interruptible(&ioc->transport_cmds.mutex);
1067 if (rc)
1068 return rc;
1069
1070 if (ioc->transport_cmds.status != MPT2_CMD_NOT_USED) {
1071 printk(MPT2SAS_ERR_FMT "%s: transport_cmds in use\n", ioc->name,
1072 __func__);
1073 rc = -EAGAIN;
1074 goto out;
1075 }
1076 ioc->transport_cmds.status = MPT2_CMD_PENDING;
1077
1078 wait_state_count = 0;
1079 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1080 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1081 if (wait_state_count++ == 10) {
1082 printk(MPT2SAS_ERR_FMT
1083 "%s: failed due to ioc not operational\n",
1084 ioc->name, __func__);
1085 rc = -EFAULT;
1086 goto out;
1087 }
1088 ssleep(1);
1089 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1090 printk(MPT2SAS_INFO_FMT "%s: waiting for "
1091 "operational state(count=%d)\n", ioc->name,
1092 __func__, wait_state_count);
1093 }
1094 if (wait_state_count)
1095 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
1096 ioc->name, __func__);
1097
1098 smid = mpt2sas_base_get_smid(ioc, ioc->transport_cb_idx);
1099 if (!smid) {
1100 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1101 ioc->name, __func__);
1102 rc = -EAGAIN;
1103 goto out;
1104 }
1105
1106 rc = 0;
1107 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1108 ioc->transport_cmds.smid = smid;
1109
1110 memset(mpi_request, 0, sizeof(Mpi2SmpPassthroughRequest_t));
1111 mpi_request->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
1112 mpi_request->PhysicalPort = 0xFF;
7b936b02
KD
1113 mpi_request->VF_ID = 0; /* TODO */
1114 mpi_request->VP_ID = 0;
635374e7
EM
1115 *((u64 *)&mpi_request->SASAddress) = (rphy) ?
1116 cpu_to_le64(rphy->identify.sas_address) :
1117 cpu_to_le64(ioc->sas_hba.sas_address);
b0790410 1118 mpi_request->RequestDataLength = cpu_to_le16(blk_rq_bytes(req) - 4);
635374e7
EM
1119 psge = &mpi_request->SGL;
1120
1121 /* WRITE sgel first */
1122 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1123 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1124 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1125 dma_addr_out = pci_map_single(ioc->pdev, bio_data(req->bio),
b0790410 1126 blk_rq_bytes(req), PCI_DMA_BIDIRECTIONAL);
635374e7
EM
1127 if (!dma_addr_out) {
1128 mpt2sas_base_free_smid(ioc, le16_to_cpu(smid));
1129 goto unmap;
1130 }
1131
b0790410 1132 ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(req) - 4),
635374e7
EM
1133 dma_addr_out);
1134
1135 /* incr sgel */
1136 psge += ioc->sge_size;
1137
1138 /* READ sgel last */
1139 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1140 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1141 MPI2_SGE_FLAGS_END_OF_LIST);
1142 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
b0790410
TH
1143 dma_addr_in = pci_map_single(ioc->pdev, bio_data(rsp->bio),
1144 blk_rq_bytes(rsp), PCI_DMA_BIDIRECTIONAL);
635374e7
EM
1145 if (!dma_addr_in) {
1146 mpt2sas_base_free_smid(ioc, le16_to_cpu(smid));
1147 goto unmap;
1148 }
1149
b0790410 1150 ioc->base_add_sg_single(psge, sgl_flags | (blk_rq_bytes(rsp) + 4),
635374e7
EM
1151 dma_addr_in);
1152
1153 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
1154 "sending smp request\n", ioc->name, __func__));
1155
7b936b02 1156 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
1157 timeleft = wait_for_completion_timeout(&ioc->transport_cmds.done,
1158 10*HZ);
1159
1160 if (!(ioc->transport_cmds.status & MPT2_CMD_COMPLETE)) {
1161 printk(MPT2SAS_ERR_FMT "%s : timeout\n",
1162 __func__, ioc->name);
1163 _debug_dump_mf(mpi_request,
1164 sizeof(Mpi2SmpPassthroughRequest_t)/4);
1165 if (!(ioc->transport_cmds.status & MPT2_CMD_RESET))
1166 issue_reset = 1;
1167 goto issue_host_reset;
1168 }
1169
1170 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s - "
1171 "complete\n", ioc->name, __func__));
1172
1173 if (ioc->transport_cmds.status & MPT2_CMD_REPLY_VALID) {
1174
1175 mpi_reply = ioc->transport_cmds.reply;
1176
1177 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
1178 "%s - reply data transfer size(%d)\n",
1179 ioc->name, __func__,
1180 le16_to_cpu(mpi_reply->ResponseDataLength)));
1181
1182 memcpy(req->sense, mpi_reply, sizeof(*mpi_reply));
1183 req->sense_len = sizeof(*mpi_reply);
5f49f631
TH
1184 req->resid_len = 0;
1185 rsp->resid_len -= mpi_reply->ResponseDataLength;
635374e7
EM
1186 } else {
1187 dtransportprintk(ioc, printk(MPT2SAS_DEBUG_FMT
1188 "%s - no reply\n", ioc->name, __func__));
1189 rc = -ENXIO;
1190 }
1191
1192 issue_host_reset:
1193 if (issue_reset) {
1194 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1195 FORCE_BIG_HAMMER);
1196 rc = -ETIMEDOUT;
1197 }
1198
1199 unmap:
1200 if (dma_addr_out)
b0790410 1201 pci_unmap_single(ioc->pdev, dma_addr_out, blk_rq_bytes(req),
635374e7
EM
1202 PCI_DMA_BIDIRECTIONAL);
1203 if (dma_addr_in)
b0790410 1204 pci_unmap_single(ioc->pdev, dma_addr_in, blk_rq_bytes(rsp),
635374e7
EM
1205 PCI_DMA_BIDIRECTIONAL);
1206
1207 out:
1208 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
1209 mutex_unlock(&ioc->transport_cmds.mutex);
1210 return rc;
1211}
1212
1213struct sas_function_template mpt2sas_transport_functions = {
d5d135b3
EM
1214 .get_linkerrors = _transport_get_linkerrors,
1215 .get_enclosure_identifier = _transport_get_enclosure_identifier,
1216 .get_bay_identifier = _transport_get_bay_identifier,
1217 .phy_reset = _transport_phy_reset,
1218 .smp_handler = _transport_smp_handler,
635374e7
EM
1219};
1220
1221struct scsi_transport_template *mpt2sas_transport_template;