]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/ibmvscsi/ibmvfc.c
[SCSI] ibmvfc: Fix error reporting for some FC errors
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / ibmvscsi / ibmvfc.c
CommitLineData
072b91f9
BK
1/*
2 * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
3 *
4 * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
5 *
6 * Copyright (C) IBM Corporation, 2008
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (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 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/dma-mapping.h>
27#include <linux/dmapool.h>
28#include <linux/delay.h>
29#include <linux/interrupt.h>
30#include <linux/kthread.h>
31#include <linux/of.h>
32#include <linux/stringify.h>
33#include <asm/firmware.h>
34#include <asm/irq.h>
35#include <asm/vio.h>
36#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_tcq.h>
41#include <scsi/scsi_transport_fc.h>
42#include "ibmvfc.h"
43
44static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
45static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
46static unsigned int max_lun = IBMVFC_MAX_LUN;
47static unsigned int max_targets = IBMVFC_MAX_TARGETS;
48static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
49static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
50static unsigned int dev_loss_tmo = IBMVFC_DEV_LOSS_TMO;
51static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
52static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
53static LIST_HEAD(ibmvfc_head);
54static DEFINE_SPINLOCK(ibmvfc_driver_lock);
55static struct scsi_transport_template *ibmvfc_transport_template;
56
57MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
58MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
59MODULE_LICENSE("GPL");
60MODULE_VERSION(IBMVFC_DRIVER_VERSION);
61
62module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
63MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
64 "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
65module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
66MODULE_PARM_DESC(default_timeout,
67 "Default timeout in seconds for initialization and EH commands. "
68 "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
69module_param_named(max_requests, max_requests, uint, S_IRUGO);
70MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
71 "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
72module_param_named(max_lun, max_lun, uint, S_IRUGO);
73MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
74 "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
75module_param_named(max_targets, max_targets, uint, S_IRUGO);
76MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
77 "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
78module_param_named(disc_threads, disc_threads, uint, S_IRUGO | S_IWUSR);
79MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
80 "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
81module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
82MODULE_PARM_DESC(debug, "Enable driver debug information. "
83 "[Default=" __stringify(IBMVFC_DEBUG) "]");
84module_param_named(dev_loss_tmo, dev_loss_tmo, uint, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(dev_loss_tmo, "Maximum number of seconds that the FC "
86 "transport should insulate the loss of a remote port. Once this "
87 "value is exceeded, the scsi target is removed. "
88 "[Default=" __stringify(IBMVFC_DEV_LOSS_TMO) "]");
89module_param_named(log_level, log_level, uint, 0);
90MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
91 "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
92
93static const struct {
94 u16 status;
95 u16 error;
96 u8 result;
97 u8 retry;
98 int log;
99 char *name;
100} cmd_status [] = {
101 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
102 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
103 { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
104 { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_NO_CONNECT, 1, 1, "network down" },
105 { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
106 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
107 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
108 { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
109 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
110 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
111 { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
112 { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
113 { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 0, 0, "link halted" },
114 { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
115
116 { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
117 { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
118 { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ABORT, 0, 1, "invalid parameter" },
119 { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ABORT, 0, 1, "missing parameter" },
120 { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
121 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ABORT, 0, 1, "transaction cancelled" },
122 { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ABORT, 0, 1, "transaction cancelled implicit" },
123 { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
124 { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
125
126 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
127 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
128 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
129 { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
130 { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
131 { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
132 { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
133 { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
134 { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
135 { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
136 { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
137
138 { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
139};
140
141static void ibmvfc_npiv_login(struct ibmvfc_host *);
142static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
143static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
144static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
145
146static const char *unknown_error = "unknown error";
147
148#ifdef CONFIG_SCSI_IBMVFC_TRACE
149/**
150 * ibmvfc_trc_start - Log a start trace entry
151 * @evt: ibmvfc event struct
152 *
153 **/
154static void ibmvfc_trc_start(struct ibmvfc_event *evt)
155{
156 struct ibmvfc_host *vhost = evt->vhost;
157 struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
158 struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
159 struct ibmvfc_trace_entry *entry;
160
161 entry = &vhost->trace[vhost->trace_index++];
162 entry->evt = evt;
163 entry->time = jiffies;
164 entry->fmt = evt->crq.format;
165 entry->type = IBMVFC_TRC_START;
166
167 switch (entry->fmt) {
168 case IBMVFC_CMD_FORMAT:
169 entry->op_code = vfc_cmd->iu.cdb[0];
170 entry->scsi_id = vfc_cmd->tgt_scsi_id;
171 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
172 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
173 entry->u.start.xfer_len = vfc_cmd->iu.xfer_len;
174 break;
175 case IBMVFC_MAD_FORMAT:
176 entry->op_code = mad->opcode;
177 break;
178 default:
179 break;
180 };
181}
182
183/**
184 * ibmvfc_trc_end - Log an end trace entry
185 * @evt: ibmvfc event struct
186 *
187 **/
188static void ibmvfc_trc_end(struct ibmvfc_event *evt)
189{
190 struct ibmvfc_host *vhost = evt->vhost;
191 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
192 struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
193 struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
194
195 entry->evt = evt;
196 entry->time = jiffies;
197 entry->fmt = evt->crq.format;
198 entry->type = IBMVFC_TRC_END;
199
200 switch (entry->fmt) {
201 case IBMVFC_CMD_FORMAT:
202 entry->op_code = vfc_cmd->iu.cdb[0];
203 entry->scsi_id = vfc_cmd->tgt_scsi_id;
204 entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
205 entry->tmf_flags = vfc_cmd->iu.tmf_flags;
206 entry->u.end.status = vfc_cmd->status;
207 entry->u.end.error = vfc_cmd->error;
208 entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
209 entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
210 entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
211 break;
212 case IBMVFC_MAD_FORMAT:
213 entry->op_code = mad->opcode;
214 entry->u.end.status = mad->status;
215 break;
216 default:
217 break;
218
219 };
220}
221
222#else
223#define ibmvfc_trc_start(evt) do { } while (0)
224#define ibmvfc_trc_end(evt) do { } while (0)
225#endif
226
227/**
228 * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
229 * @status: status / error class
230 * @error: error
231 *
232 * Return value:
233 * index into cmd_status / -EINVAL on failure
234 **/
235static int ibmvfc_get_err_index(u16 status, u16 error)
236{
237 int i;
238
239 for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
240 if ((cmd_status[i].status & status) == cmd_status[i].status &&
241 cmd_status[i].error == error)
242 return i;
243
244 return -EINVAL;
245}
246
247/**
248 * ibmvfc_get_cmd_error - Find the error description for the fcp response
249 * @status: status / error class
250 * @error: error
251 *
252 * Return value:
253 * error description string
254 **/
255static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
256{
257 int rc = ibmvfc_get_err_index(status, error);
258 if (rc >= 0)
259 return cmd_status[rc].name;
260 return unknown_error;
261}
262
263/**
264 * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
265 * @vfc_cmd: ibmvfc command struct
266 *
267 * Return value:
268 * SCSI result value to return for completed command
269 **/
270static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
271{
272 int err;
273 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
274 int fc_rsp_len = rsp->fcp_rsp_len;
275
276 if ((rsp->flags & FCP_RSP_LEN_VALID) &&
277 ((!fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
278 rsp->data.info.rsp_code))
279 return DID_ERROR << 16;
280
072b91f9
BK
281 err = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
282 if (err >= 0)
283 return rsp->scsi_status | (cmd_status[err].result << 16);
284 return rsp->scsi_status | (DID_ERROR << 16);
285}
286
287/**
288 * ibmvfc_retry_cmd - Determine if error status is retryable
289 * @status: status / error class
290 * @error: error
291 *
292 * Return value:
293 * 1 if error should be retried / 0 if it should not
294 **/
295static int ibmvfc_retry_cmd(u16 status, u16 error)
296{
297 int rc = ibmvfc_get_err_index(status, error);
298
299 if (rc >= 0)
300 return cmd_status[rc].retry;
301 return 1;
302}
303
304static const char *unknown_fc_explain = "unknown fc explain";
305
306static const struct {
307 u16 fc_explain;
308 char *name;
309} ls_explain [] = {
310 { 0x00, "no additional explanation" },
311 { 0x01, "service parameter error - options" },
312 { 0x03, "service parameter error - initiator control" },
313 { 0x05, "service parameter error - recipient control" },
314 { 0x07, "service parameter error - received data field size" },
315 { 0x09, "service parameter error - concurrent seq" },
316 { 0x0B, "service parameter error - credit" },
317 { 0x0D, "invalid N_Port/F_Port_Name" },
318 { 0x0E, "invalid node/Fabric Name" },
319 { 0x0F, "invalid common service parameters" },
320 { 0x11, "invalid association header" },
321 { 0x13, "association header required" },
322 { 0x15, "invalid originator S_ID" },
323 { 0x17, "invalid OX_ID-RX-ID combination" },
324 { 0x19, "command (request) already in progress" },
325 { 0x1E, "N_Port Login requested" },
326 { 0x1F, "Invalid N_Port_ID" },
327};
328
329static const struct {
330 u16 fc_explain;
331 char *name;
332} gs_explain [] = {
333 { 0x00, "no additional explanation" },
334 { 0x01, "port identifier not registered" },
335 { 0x02, "port name not registered" },
336 { 0x03, "node name not registered" },
337 { 0x04, "class of service not registered" },
338 { 0x06, "initial process associator not registered" },
339 { 0x07, "FC-4 TYPEs not registered" },
340 { 0x08, "symbolic port name not registered" },
341 { 0x09, "symbolic node name not registered" },
342 { 0x0A, "port type not registered" },
343 { 0xF0, "authorization exception" },
344 { 0xF1, "authentication exception" },
345 { 0xF2, "data base full" },
346 { 0xF3, "data base empty" },
347 { 0xF4, "processing request" },
348 { 0xF5, "unable to verify connection" },
349 { 0xF6, "devices not in a common zone" },
350};
351
352/**
353 * ibmvfc_get_ls_explain - Return the FC Explain description text
354 * @status: FC Explain status
355 *
356 * Returns:
357 * error string
358 **/
359static const char *ibmvfc_get_ls_explain(u16 status)
360{
361 int i;
362
363 for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
364 if (ls_explain[i].fc_explain == status)
365 return ls_explain[i].name;
366
367 return unknown_fc_explain;
368}
369
370/**
371 * ibmvfc_get_gs_explain - Return the FC Explain description text
372 * @status: FC Explain status
373 *
374 * Returns:
375 * error string
376 **/
377static const char *ibmvfc_get_gs_explain(u16 status)
378{
379 int i;
380
381 for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
382 if (gs_explain[i].fc_explain == status)
383 return gs_explain[i].name;
384
385 return unknown_fc_explain;
386}
387
388static const struct {
389 enum ibmvfc_fc_type fc_type;
390 char *name;
391} fc_type [] = {
392 { IBMVFC_FABRIC_REJECT, "fabric reject" },
393 { IBMVFC_PORT_REJECT, "port reject" },
394 { IBMVFC_LS_REJECT, "ELS reject" },
395 { IBMVFC_FABRIC_BUSY, "fabric busy" },
396 { IBMVFC_PORT_BUSY, "port busy" },
397 { IBMVFC_BASIC_REJECT, "basic reject" },
398};
399
400static const char *unknown_fc_type = "unknown fc type";
401
402/**
403 * ibmvfc_get_fc_type - Return the FC Type description text
404 * @status: FC Type error status
405 *
406 * Returns:
407 * error string
408 **/
409static const char *ibmvfc_get_fc_type(u16 status)
410{
411 int i;
412
413 for (i = 0; i < ARRAY_SIZE(fc_type); i++)
414 if (fc_type[i].fc_type == status)
415 return fc_type[i].name;
416
417 return unknown_fc_type;
418}
419
420/**
421 * ibmvfc_set_tgt_action - Set the next init action for the target
422 * @tgt: ibmvfc target struct
423 * @action: action to perform
424 *
425 **/
426static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
427 enum ibmvfc_target_action action)
428{
429 switch (tgt->action) {
430 case IBMVFC_TGT_ACTION_DEL_RPORT:
431 break;
432 default:
433 tgt->action = action;
434 break;
435 }
436}
437
438/**
439 * ibmvfc_set_host_state - Set the state for the host
440 * @vhost: ibmvfc host struct
441 * @state: state to set host to
442 *
443 * Returns:
444 * 0 if state changed / non-zero if not changed
445 **/
446static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
447 enum ibmvfc_host_state state)
448{
449 int rc = 0;
450
451 switch (vhost->state) {
452 case IBMVFC_HOST_OFFLINE:
453 rc = -EINVAL;
454 break;
455 default:
456 vhost->state = state;
457 break;
458 };
459
460 return rc;
461}
462
463/**
464 * ibmvfc_set_host_action - Set the next init action for the host
465 * @vhost: ibmvfc host struct
466 * @action: action to perform
467 *
468 **/
469static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
470 enum ibmvfc_host_action action)
471{
472 switch (action) {
473 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
474 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
475 vhost->action = action;
476 break;
477 case IBMVFC_HOST_ACTION_INIT_WAIT:
478 if (vhost->action == IBMVFC_HOST_ACTION_INIT)
479 vhost->action = action;
480 break;
481 case IBMVFC_HOST_ACTION_QUERY:
482 switch (vhost->action) {
483 case IBMVFC_HOST_ACTION_INIT_WAIT:
484 case IBMVFC_HOST_ACTION_NONE:
485 case IBMVFC_HOST_ACTION_TGT_ADD:
486 vhost->action = action;
487 break;
488 default:
489 break;
490 };
491 break;
492 case IBMVFC_HOST_ACTION_TGT_INIT:
493 if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
494 vhost->action = action;
495 break;
496 case IBMVFC_HOST_ACTION_INIT:
497 case IBMVFC_HOST_ACTION_TGT_DEL:
498 case IBMVFC_HOST_ACTION_QUERY_TGTS:
499 case IBMVFC_HOST_ACTION_TGT_ADD:
500 case IBMVFC_HOST_ACTION_NONE:
501 default:
502 vhost->action = action;
503 break;
504 };
505}
506
507/**
508 * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
509 * @vhost: ibmvfc host struct
510 *
511 * Return value:
512 * nothing
513 **/
514static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
515{
516 if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
2d0da2a4
BK
517 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
518 scsi_block_requests(vhost->host);
519 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
520 }
072b91f9
BK
521 } else
522 vhost->reinit = 1;
523
524 wake_up(&vhost->work_wait_q);
525}
526
527/**
528 * ibmvfc_link_down - Handle a link down event from the adapter
529 * @vhost: ibmvfc host struct
530 * @state: ibmvfc host state to enter
531 *
532 **/
533static void ibmvfc_link_down(struct ibmvfc_host *vhost,
534 enum ibmvfc_host_state state)
535{
536 struct ibmvfc_target *tgt;
537
538 ENTER;
539 scsi_block_requests(vhost->host);
540 list_for_each_entry(tgt, &vhost->targets, queue)
541 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
542 ibmvfc_set_host_state(vhost, state);
543 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
544 vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
545 wake_up(&vhost->work_wait_q);
546 LEAVE;
547}
548
549/**
550 * ibmvfc_init_host - Start host initialization
551 * @vhost: ibmvfc host struct
cf6f10d7 552 * @relogin: is this a re-login?
072b91f9
BK
553 *
554 * Return value:
555 * nothing
556 **/
cf6f10d7 557static void ibmvfc_init_host(struct ibmvfc_host *vhost, int relogin)
072b91f9
BK
558{
559 struct ibmvfc_target *tgt;
560
561 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
562 if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
563 dev_err(vhost->dev,
564 "Host initialization retries exceeded. Taking adapter offline\n");
565 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
566 return;
567 }
568 }
569
570 if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
cf6f10d7
BK
571 if (!relogin) {
572 memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
573 vhost->async_crq.cur = 0;
574 }
575
072b91f9
BK
576 list_for_each_entry(tgt, &vhost->targets, queue)
577 tgt->need_login = 1;
578 scsi_block_requests(vhost->host);
579 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
580 vhost->job_step = ibmvfc_npiv_login;
581 wake_up(&vhost->work_wait_q);
582 }
583}
584
585/**
586 * ibmvfc_send_crq - Send a CRQ
587 * @vhost: ibmvfc host struct
588 * @word1: the first 64 bits of the data
589 * @word2: the second 64 bits of the data
590 *
591 * Return value:
592 * 0 on success / other on failure
593 **/
594static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
595{
596 struct vio_dev *vdev = to_vio_dev(vhost->dev);
597 return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
598}
599
600/**
601 * ibmvfc_send_crq_init - Send a CRQ init message
602 * @vhost: ibmvfc host struct
603 *
604 * Return value:
605 * 0 on success / other on failure
606 **/
607static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
608{
609 ibmvfc_dbg(vhost, "Sending CRQ init\n");
610 return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
611}
612
613/**
614 * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
615 * @vhost: ibmvfc host struct
616 *
617 * Return value:
618 * 0 on success / other on failure
619 **/
620static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
621{
622 ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
623 return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
624}
625
626/**
627 * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
628 * @vhost: ibmvfc host struct
629 *
630 * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
631 * the crq with the hypervisor.
632 **/
633static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
634{
635 long rc;
636 struct vio_dev *vdev = to_vio_dev(vhost->dev);
637 struct ibmvfc_crq_queue *crq = &vhost->crq;
638
639 ibmvfc_dbg(vhost, "Releasing CRQ\n");
640 free_irq(vdev->irq, vhost);
641 do {
642 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
643 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
644
645 vhost->state = IBMVFC_NO_CRQ;
646 dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
647 free_page((unsigned long)crq->msgs);
648}
649
650/**
651 * ibmvfc_reenable_crq_queue - reenables the CRQ
652 * @vhost: ibmvfc host struct
653 *
654 * Return value:
655 * 0 on success / other on failure
656 **/
657static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
658{
659 int rc;
660 struct vio_dev *vdev = to_vio_dev(vhost->dev);
661
662 /* Re-enable the CRQ */
663 do {
664 rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
665 } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
666
667 if (rc)
668 dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
669
670 return rc;
671}
672
673/**
674 * ibmvfc_reset_crq - resets a crq after a failure
675 * @vhost: ibmvfc host struct
676 *
677 * Return value:
678 * 0 on success / other on failure
679 **/
680static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
681{
682 int rc;
683 struct vio_dev *vdev = to_vio_dev(vhost->dev);
684 struct ibmvfc_crq_queue *crq = &vhost->crq;
685
686 /* Close the CRQ */
687 do {
688 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
689 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
690
691 vhost->state = IBMVFC_NO_CRQ;
692 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
693
694 /* Clean out the queue */
695 memset(crq->msgs, 0, PAGE_SIZE);
696 crq->cur = 0;
697
698 /* And re-open it again */
699 rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
700 crq->msg_token, PAGE_SIZE);
701
702 if (rc == H_CLOSED)
703 /* Adapter is good, but other end is not ready */
704 dev_warn(vhost->dev, "Partner adapter not ready\n");
705 else if (rc != 0)
706 dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
707
708 return rc;
709}
710
711/**
712 * ibmvfc_valid_event - Determines if event is valid.
713 * @pool: event_pool that contains the event
714 * @evt: ibmvfc event to be checked for validity
715 *
716 * Return value:
717 * 1 if event is valid / 0 if event is not valid
718 **/
719static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
720 struct ibmvfc_event *evt)
721{
722 int index = evt - pool->events;
723 if (index < 0 || index >= pool->size) /* outside of bounds */
724 return 0;
725 if (evt != pool->events + index) /* unaligned */
726 return 0;
727 return 1;
728}
729
730/**
731 * ibmvfc_free_event - Free the specified event
732 * @evt: ibmvfc_event to be freed
733 *
734 **/
735static void ibmvfc_free_event(struct ibmvfc_event *evt)
736{
737 struct ibmvfc_host *vhost = evt->vhost;
738 struct ibmvfc_event_pool *pool = &vhost->pool;
739
740 BUG_ON(!ibmvfc_valid_event(pool, evt));
741 BUG_ON(atomic_inc_return(&evt->free) != 1);
742 list_add_tail(&evt->queue, &vhost->free);
743}
744
745/**
746 * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
747 * @evt: ibmvfc event struct
748 *
749 * This function does not setup any error status, that must be done
750 * before this function gets called.
751 **/
752static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
753{
754 struct scsi_cmnd *cmnd = evt->cmnd;
755
756 if (cmnd) {
757 scsi_dma_unmap(cmnd);
758 cmnd->scsi_done(cmnd);
759 }
760
761 ibmvfc_free_event(evt);
762}
763
764/**
765 * ibmvfc_fail_request - Fail request with specified error code
766 * @evt: ibmvfc event struct
767 * @error_code: error code to fail request with
768 *
769 * Return value:
770 * none
771 **/
772static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
773{
774 if (evt->cmnd) {
775 evt->cmnd->result = (error_code << 16);
776 evt->done = ibmvfc_scsi_eh_done;
777 } else
778 evt->xfer_iu->mad_common.status = IBMVFC_MAD_DRIVER_FAILED;
779
780 list_del(&evt->queue);
781 del_timer(&evt->timer);
782 ibmvfc_trc_end(evt);
783 evt->done(evt);
784}
785
786/**
787 * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
788 * @vhost: ibmvfc host struct
789 * @error_code: error code to fail requests with
790 *
791 * Return value:
792 * none
793 **/
794static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
795{
796 struct ibmvfc_event *evt, *pos;
797
798 ibmvfc_dbg(vhost, "Purging all requests\n");
799 list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
800 ibmvfc_fail_request(evt, error_code);
801}
802
803/**
804 * __ibmvfc_reset_host - Reset the connection to the server (no locking)
805 * @vhost: struct ibmvfc host to reset
806 **/
807static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
808{
809 int rc;
810
811 scsi_block_requests(vhost->host);
812 ibmvfc_purge_requests(vhost, DID_ERROR);
813 if ((rc = ibmvfc_reset_crq(vhost)) ||
814 (rc = ibmvfc_send_crq_init(vhost)) ||
815 (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
816 dev_err(vhost->dev, "Error after reset rc=%d\n", rc);
817 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
818 } else
819 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
820}
821
822/**
823 * ibmvfc_reset_host - Reset the connection to the server
824 * @vhost: struct ibmvfc host to reset
825 **/
826static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
827{
828 unsigned long flags;
829
830 spin_lock_irqsave(vhost->host->host_lock, flags);
831 __ibmvfc_reset_host(vhost);
832 spin_unlock_irqrestore(vhost->host->host_lock, flags);
833}
834
835/**
836 * ibmvfc_retry_host_init - Retry host initialization if allowed
837 * @vhost: ibmvfc host struct
838 *
839 **/
840static void ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
841{
842 if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
843 if (++vhost->init_retries > IBMVFC_MAX_INIT_RETRIES) {
844 dev_err(vhost->dev,
845 "Host initialization retries exceeded. Taking adapter offline\n");
846 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
847 } else if (vhost->init_retries == IBMVFC_MAX_INIT_RETRIES)
848 __ibmvfc_reset_host(vhost);
849 else
850 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
851 }
852
853 wake_up(&vhost->work_wait_q);
854}
855
856/**
b3c10489 857 * __ibmvfc_get_target - Find the specified scsi_target (no locking)
072b91f9
BK
858 * @starget: scsi target struct
859 *
860 * Return value:
861 * ibmvfc_target struct / NULL if not found
862 **/
b3c10489 863static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
072b91f9
BK
864{
865 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
866 struct ibmvfc_host *vhost = shost_priv(shost);
867 struct ibmvfc_target *tgt;
868
869 list_for_each_entry(tgt, &vhost->targets, queue)
b3c10489
BK
870 if (tgt->target_id == starget->id) {
871 kref_get(&tgt->kref);
072b91f9 872 return tgt;
b3c10489 873 }
072b91f9
BK
874 return NULL;
875}
876
877/**
b3c10489 878 * ibmvfc_get_target - Find the specified scsi_target
072b91f9
BK
879 * @starget: scsi target struct
880 *
881 * Return value:
882 * ibmvfc_target struct / NULL if not found
883 **/
b3c10489 884static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
072b91f9
BK
885{
886 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
887 struct ibmvfc_target *tgt;
888 unsigned long flags;
889
890 spin_lock_irqsave(shost->host_lock, flags);
b3c10489 891 tgt = __ibmvfc_get_target(starget);
072b91f9
BK
892 spin_unlock_irqrestore(shost->host_lock, flags);
893 return tgt;
894}
895
896/**
897 * ibmvfc_get_host_speed - Get host port speed
898 * @shost: scsi host struct
899 *
900 * Return value:
901 * none
902 **/
903static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
904{
905 struct ibmvfc_host *vhost = shost_priv(shost);
906 unsigned long flags;
907
908 spin_lock_irqsave(shost->host_lock, flags);
909 if (vhost->state == IBMVFC_ACTIVE) {
910 switch (vhost->login_buf->resp.link_speed / 100) {
911 case 1:
912 fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
913 break;
914 case 2:
915 fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
916 break;
917 case 4:
918 fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
919 break;
920 case 8:
921 fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
922 break;
923 case 10:
924 fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
925 break;
926 case 16:
927 fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
928 break;
929 default:
930 ibmvfc_log(vhost, 3, "Unknown port speed: %ld Gbit\n",
931 vhost->login_buf->resp.link_speed / 100);
932 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
933 break;
934 }
935 } else
936 fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
937 spin_unlock_irqrestore(shost->host_lock, flags);
938}
939
940/**
941 * ibmvfc_get_host_port_state - Get host port state
942 * @shost: scsi host struct
943 *
944 * Return value:
945 * none
946 **/
947static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
948{
949 struct ibmvfc_host *vhost = shost_priv(shost);
950 unsigned long flags;
951
952 spin_lock_irqsave(shost->host_lock, flags);
953 switch (vhost->state) {
954 case IBMVFC_INITIALIZING:
955 case IBMVFC_ACTIVE:
956 fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
957 break;
958 case IBMVFC_LINK_DOWN:
959 fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
960 break;
961 case IBMVFC_LINK_DEAD:
962 case IBMVFC_HOST_OFFLINE:
963 fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
964 break;
965 case IBMVFC_HALTED:
966 fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
967 break;
0ae808e0
BK
968 case IBMVFC_NO_CRQ:
969 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
970 break;
072b91f9
BK
971 default:
972 ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
973 fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
974 break;
975 }
976 spin_unlock_irqrestore(shost->host_lock, flags);
977}
978
979/**
980 * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
981 * @rport: rport struct
982 * @timeout: timeout value
983 *
984 * Return value:
985 * none
986 **/
987static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
988{
989 if (timeout)
990 rport->dev_loss_tmo = timeout;
991 else
992 rport->dev_loss_tmo = 1;
993}
994
b3c10489
BK
995/**
996 * ibmvfc_release_tgt - Free memory allocated for a target
997 * @kref: kref struct
998 *
999 **/
1000static void ibmvfc_release_tgt(struct kref *kref)
1001{
1002 struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
1003 kfree(tgt);
1004}
1005
072b91f9
BK
1006/**
1007 * ibmvfc_get_starget_node_name - Get SCSI target's node name
1008 * @starget: scsi target struct
1009 *
1010 * Return value:
1011 * none
1012 **/
1013static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
1014{
b3c10489 1015 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
072b91f9 1016 fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
b3c10489
BK
1017 if (tgt)
1018 kref_put(&tgt->kref, ibmvfc_release_tgt);
072b91f9
BK
1019}
1020
1021/**
1022 * ibmvfc_get_starget_port_name - Get SCSI target's port name
1023 * @starget: scsi target struct
1024 *
1025 * Return value:
1026 * none
1027 **/
1028static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
1029{
b3c10489 1030 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
072b91f9 1031 fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
b3c10489
BK
1032 if (tgt)
1033 kref_put(&tgt->kref, ibmvfc_release_tgt);
072b91f9
BK
1034}
1035
1036/**
1037 * ibmvfc_get_starget_port_id - Get SCSI target's port ID
1038 * @starget: scsi target struct
1039 *
1040 * Return value:
1041 * none
1042 **/
1043static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
1044{
b3c10489 1045 struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
072b91f9 1046 fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
b3c10489
BK
1047 if (tgt)
1048 kref_put(&tgt->kref, ibmvfc_release_tgt);
072b91f9
BK
1049}
1050
1051/**
1052 * ibmvfc_wait_while_resetting - Wait while the host resets
1053 * @vhost: ibmvfc host struct
1054 *
1055 * Return value:
1056 * 0 on success / other on failure
1057 **/
1058static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
1059{
1060 long timeout = wait_event_timeout(vhost->init_wait_q,
3eddc569
BK
1061 ((vhost->state == IBMVFC_ACTIVE ||
1062 vhost->state == IBMVFC_HOST_OFFLINE ||
1063 vhost->state == IBMVFC_LINK_DEAD) &&
1064 vhost->action == IBMVFC_HOST_ACTION_NONE),
072b91f9
BK
1065 (init_timeout * HZ));
1066
1067 return timeout ? 0 : -EIO;
1068}
1069
1070/**
1071 * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
1072 * @shost: scsi host struct
1073 *
1074 * Return value:
1075 * 0 on success / other on failure
1076 **/
1077static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
1078{
1079 struct ibmvfc_host *vhost = shost_priv(shost);
1080
1081 dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
1082 ibmvfc_reset_host(vhost);
1083 return ibmvfc_wait_while_resetting(vhost);
1084}
1085
1086/**
1087 * ibmvfc_gather_partition_info - Gather info about the LPAR
1088 *
1089 * Return value:
1090 * none
1091 **/
1092static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
1093{
1094 struct device_node *rootdn;
1095 const char *name;
1096 const unsigned int *num;
1097
1098 rootdn = of_find_node_by_path("/");
1099 if (!rootdn)
1100 return;
1101
1102 name = of_get_property(rootdn, "ibm,partition-name", NULL);
1103 if (name)
1104 strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
1105 num = of_get_property(rootdn, "ibm,partition-no", NULL);
1106 if (num)
1107 vhost->partition_number = *num;
1108 of_node_put(rootdn);
1109}
1110
1111/**
1112 * ibmvfc_set_login_info - Setup info for NPIV login
1113 * @vhost: ibmvfc host struct
1114 *
1115 * Return value:
1116 * none
1117 **/
1118static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
1119{
1120 struct ibmvfc_npiv_login *login_info = &vhost->login_info;
1121 struct device_node *of_node = vhost->dev->archdata.of_node;
1122 const char *location;
1123
1124 memset(login_info, 0, sizeof(*login_info));
1125
1126 login_info->ostype = IBMVFC_OS_LINUX;
1127 login_info->max_dma_len = IBMVFC_MAX_SECTORS << 9;
1128 login_info->max_payload = sizeof(struct ibmvfc_fcp_cmd_iu);
1129 login_info->max_response = sizeof(struct ibmvfc_fcp_rsp);
1130 login_info->partition_num = vhost->partition_number;
1131 login_info->vfc_frame_version = 1;
1132 login_info->fcp_version = 3;
1133 if (vhost->client_migrated)
1134 login_info->flags = IBMVFC_CLIENT_MIGRATED;
1135
1136 login_info->max_cmds = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1137 login_info->capabilities = IBMVFC_CAN_MIGRATE;
1138 login_info->async.va = vhost->async_crq.msg_token;
52d7e861 1139 login_info->async.len = vhost->async_crq.size * sizeof(*vhost->async_crq.msgs);
072b91f9
BK
1140 strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
1141 strncpy(login_info->device_name,
1142 vhost->host->shost_gendev.bus_id, IBMVFC_MAX_NAME);
1143
1144 location = of_get_property(of_node, "ibm,loc-code", NULL);
1145 location = location ? location : vhost->dev->bus_id;
1146 strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
1147}
1148
1149/**
1150 * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
1151 * @vhost: ibmvfc host who owns the event pool
1152 *
1153 * Returns zero on success.
1154 **/
1155static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
1156{
1157 int i;
1158 struct ibmvfc_event_pool *pool = &vhost->pool;
1159
1160 ENTER;
1161 pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
1162 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
1163 if (!pool->events)
1164 return -ENOMEM;
1165
1166 pool->iu_storage = dma_alloc_coherent(vhost->dev,
1167 pool->size * sizeof(*pool->iu_storage),
1168 &pool->iu_token, 0);
1169
1170 if (!pool->iu_storage) {
1171 kfree(pool->events);
1172 return -ENOMEM;
1173 }
1174
1175 for (i = 0; i < pool->size; ++i) {
1176 struct ibmvfc_event *evt = &pool->events[i];
1177 atomic_set(&evt->free, 1);
1178 evt->crq.valid = 0x80;
1179 evt->crq.ioba = pool->iu_token + (sizeof(*evt->xfer_iu) * i);
1180 evt->xfer_iu = pool->iu_storage + i;
1181 evt->vhost = vhost;
1182 evt->ext_list = NULL;
1183 list_add_tail(&evt->queue, &vhost->free);
1184 }
1185
1186 LEAVE;
1187 return 0;
1188}
1189
1190/**
1191 * ibmvfc_free_event_pool - Frees memory of the event pool of a host
1192 * @vhost: ibmvfc host who owns the event pool
1193 *
1194 **/
1195static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
1196{
1197 int i;
1198 struct ibmvfc_event_pool *pool = &vhost->pool;
1199
1200 ENTER;
1201 for (i = 0; i < pool->size; ++i) {
1202 list_del(&pool->events[i].queue);
1203 BUG_ON(atomic_read(&pool->events[i].free) != 1);
1204 if (pool->events[i].ext_list)
1205 dma_pool_free(vhost->sg_pool,
1206 pool->events[i].ext_list,
1207 pool->events[i].ext_list_token);
1208 }
1209
1210 kfree(pool->events);
1211 dma_free_coherent(vhost->dev,
1212 pool->size * sizeof(*pool->iu_storage),
1213 pool->iu_storage, pool->iu_token);
1214 LEAVE;
1215}
1216
1217/**
1218 * ibmvfc_get_event - Gets the next free event in pool
1219 * @vhost: ibmvfc host struct
1220 *
1221 * Returns a free event from the pool.
1222 **/
1223static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
1224{
1225 struct ibmvfc_event *evt;
1226
1227 BUG_ON(list_empty(&vhost->free));
1228 evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
1229 atomic_set(&evt->free, 0);
1230 list_del(&evt->queue);
1231 return evt;
1232}
1233
1234/**
1235 * ibmvfc_init_event - Initialize fields in an event struct that are always
1236 * required.
1237 * @evt: The event
1238 * @done: Routine to call when the event is responded to
1239 * @format: SRP or MAD format
1240 **/
1241static void ibmvfc_init_event(struct ibmvfc_event *evt,
1242 void (*done) (struct ibmvfc_event *), u8 format)
1243{
1244 evt->cmnd = NULL;
1245 evt->sync_iu = NULL;
1246 evt->crq.format = format;
1247 evt->done = done;
1248}
1249
1250/**
1251 * ibmvfc_map_sg_list - Initialize scatterlist
1252 * @scmd: scsi command struct
1253 * @nseg: number of scatterlist segments
1254 * @md: memory descriptor list to initialize
1255 **/
1256static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
1257 struct srp_direct_buf *md)
1258{
1259 int i;
1260 struct scatterlist *sg;
1261
1262 scsi_for_each_sg(scmd, sg, nseg, i) {
1263 md[i].va = sg_dma_address(sg);
1264 md[i].len = sg_dma_len(sg);
1265 md[i].key = 0;
1266 }
1267}
1268
1269/**
1270 * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
1271 * @scmd: Scsi_Cmnd with the scatterlist
1272 * @evt: ibmvfc event struct
1273 * @vfc_cmd: vfc_cmd that contains the memory descriptor
1274 * @dev: device for which to map dma memory
1275 *
1276 * Returns:
1277 * 0 on success / non-zero on failure
1278 **/
1279static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
1280 struct ibmvfc_event *evt,
1281 struct ibmvfc_cmd *vfc_cmd, struct device *dev)
1282{
1283
1284 int sg_mapped;
1285 struct srp_direct_buf *data = &vfc_cmd->ioba;
1286 struct ibmvfc_host *vhost = dev_get_drvdata(dev);
1287
1288 sg_mapped = scsi_dma_map(scmd);
1289 if (!sg_mapped) {
1290 vfc_cmd->flags |= IBMVFC_NO_MEM_DESC;
1291 return 0;
1292 } else if (unlikely(sg_mapped < 0)) {
1293 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1294 scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
1295 return sg_mapped;
1296 }
1297
1298 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1299 vfc_cmd->flags |= IBMVFC_WRITE;
1300 vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
1301 } else {
1302 vfc_cmd->flags |= IBMVFC_READ;
1303 vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
1304 }
1305
1306 if (sg_mapped == 1) {
1307 ibmvfc_map_sg_list(scmd, sg_mapped, data);
1308 return 0;
1309 }
1310
1311 vfc_cmd->flags |= IBMVFC_SCATTERLIST;
1312
1313 if (!evt->ext_list) {
1314 evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
1315 &evt->ext_list_token);
1316
1317 if (!evt->ext_list) {
1318 scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
1319 return -ENOMEM;
1320 }
1321 }
1322
1323 ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
1324
1325 data->va = evt->ext_list_token;
1326 data->len = sg_mapped * sizeof(struct srp_direct_buf);
1327 data->key = 0;
1328 return 0;
1329}
1330
1331/**
1332 * ibmvfc_timeout - Internal command timeout handler
1333 * @evt: struct ibmvfc_event that timed out
1334 *
1335 * Called when an internally generated command times out
1336 **/
1337static void ibmvfc_timeout(struct ibmvfc_event *evt)
1338{
1339 struct ibmvfc_host *vhost = evt->vhost;
1340 dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
1341 ibmvfc_reset_host(vhost);
1342}
1343
1344/**
1345 * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
1346 * @evt: event to be sent
1347 * @vhost: ibmvfc host struct
1348 * @timeout: timeout in seconds - 0 means do not time command
1349 *
1350 * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
1351 **/
1352static int ibmvfc_send_event(struct ibmvfc_event *evt,
1353 struct ibmvfc_host *vhost, unsigned long timeout)
1354{
1355 u64 *crq_as_u64 = (u64 *) &evt->crq;
1356 int rc;
1357
1358 /* Copy the IU into the transfer area */
1359 *evt->xfer_iu = evt->iu;
1360 if (evt->crq.format == IBMVFC_CMD_FORMAT)
1361 evt->xfer_iu->cmd.tag = (u64)evt;
1362 else if (evt->crq.format == IBMVFC_MAD_FORMAT)
1363 evt->xfer_iu->mad_common.tag = (u64)evt;
1364 else
1365 BUG();
1366
1367 list_add_tail(&evt->queue, &vhost->sent);
1368 init_timer(&evt->timer);
1369
1370 if (timeout) {
1371 evt->timer.data = (unsigned long) evt;
1372 evt->timer.expires = jiffies + (timeout * HZ);
1373 evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
1374 add_timer(&evt->timer);
1375 }
1376
1377 if ((rc = ibmvfc_send_crq(vhost, crq_as_u64[0], crq_as_u64[1]))) {
1378 list_del(&evt->queue);
1379 del_timer(&evt->timer);
1380
1381 /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
1382 * Firmware will send a CRQ with a transport event (0xFF) to
1383 * tell this client what has happened to the transport. This
1384 * will be handled in ibmvfc_handle_crq()
1385 */
1386 if (rc == H_CLOSED) {
1387 if (printk_ratelimit())
1388 dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
1389 if (evt->cmnd)
1390 scsi_dma_unmap(evt->cmnd);
1391 ibmvfc_free_event(evt);
1392 return SCSI_MLQUEUE_HOST_BUSY;
1393 }
1394
1395 dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
1396 if (evt->cmnd) {
1397 evt->cmnd->result = DID_ERROR << 16;
1398 evt->done = ibmvfc_scsi_eh_done;
1399 } else
1400 evt->xfer_iu->mad_common.status = IBMVFC_MAD_CRQ_ERROR;
1401
1402 evt->done(evt);
1403 } else
1404 ibmvfc_trc_start(evt);
1405
1406 return 0;
1407}
1408
1409/**
1410 * ibmvfc_log_error - Log an error for the failed command if appropriate
1411 * @evt: ibmvfc event to log
1412 *
1413 **/
1414static void ibmvfc_log_error(struct ibmvfc_event *evt)
1415{
1416 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1417 struct ibmvfc_host *vhost = evt->vhost;
1418 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1419 struct scsi_cmnd *cmnd = evt->cmnd;
1420 const char *err = unknown_error;
1421 int index = ibmvfc_get_err_index(vfc_cmd->status, vfc_cmd->error);
1422 int logerr = 0;
1423 int rsp_code = 0;
1424
1425 if (index >= 0) {
1426 logerr = cmd_status[index].log;
1427 err = cmd_status[index].name;
1428 }
1429
0ae808e0 1430 if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
072b91f9
BK
1431 return;
1432
1433 if (rsp->flags & FCP_RSP_LEN_VALID)
1434 rsp_code = rsp->data.info.rsp_code;
1435
1436 scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
1437 "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
1438 cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
1439 rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
1440}
1441
1442/**
1443 * ibmvfc_scsi_done - Handle responses from commands
1444 * @evt: ibmvfc event to be handled
1445 *
1446 * Used as a callback when sending scsi cmds.
1447 **/
1448static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
1449{
1450 struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
1451 struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
1452 struct scsi_cmnd *cmnd = evt->cmnd;
2bac406d
BK
1453 u32 rsp_len = 0;
1454 u32 sense_len = rsp->fcp_sense_len;
072b91f9
BK
1455
1456 if (cmnd) {
1457 if (vfc_cmd->response_flags & IBMVFC_ADAPTER_RESID_VALID)
1458 scsi_set_resid(cmnd, vfc_cmd->adapter_resid);
1459 else if (rsp->flags & FCP_RESID_UNDER)
1460 scsi_set_resid(cmnd, rsp->fcp_resid);
1461 else
1462 scsi_set_resid(cmnd, 0);
1463
1464 if (vfc_cmd->status) {
1465 cmnd->result = ibmvfc_get_err_result(vfc_cmd);
1466
1467 if (rsp->flags & FCP_RSP_LEN_VALID)
1468 rsp_len = rsp->fcp_rsp_len;
1469 if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
1470 sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
2bac406d 1471 if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
072b91f9
BK
1472 memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
1473
50119dad
BK
1474 if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
1475 cmnd->result = (DID_ERROR << 16);
1476
072b91f9
BK
1477 ibmvfc_log_error(evt);
1478 }
1479
1480 if (!cmnd->result &&
1481 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
1482 cmnd->result = (DID_ERROR << 16);
1483
1484 scsi_dma_unmap(cmnd);
1485 cmnd->scsi_done(cmnd);
1486 }
1487
1488 ibmvfc_free_event(evt);
1489}
1490
1491/**
1492 * ibmvfc_host_chkready - Check if the host can accept commands
1493 * @vhost: struct ibmvfc host
1494 *
1495 * Returns:
1496 * 1 if host can accept command / 0 if not
1497 **/
1498static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
1499{
1500 int result = 0;
1501
1502 switch (vhost->state) {
1503 case IBMVFC_LINK_DEAD:
1504 case IBMVFC_HOST_OFFLINE:
1505 result = DID_NO_CONNECT << 16;
1506 break;
1507 case IBMVFC_NO_CRQ:
1508 case IBMVFC_INITIALIZING:
1509 case IBMVFC_HALTED:
1510 case IBMVFC_LINK_DOWN:
1511 result = DID_REQUEUE << 16;
1512 break;
1513 case IBMVFC_ACTIVE:
1514 result = 0;
1515 break;
1516 };
1517
1518 return result;
1519}
1520
1521/**
1522 * ibmvfc_queuecommand - The queuecommand function of the scsi template
1523 * @cmnd: struct scsi_cmnd to be executed
1524 * @done: Callback function to be called when cmnd is completed
1525 *
1526 * Returns:
1527 * 0 on success / other on failure
1528 **/
1529static int ibmvfc_queuecommand(struct scsi_cmnd *cmnd,
1530 void (*done) (struct scsi_cmnd *))
1531{
1532 struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
1533 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1534 struct ibmvfc_cmd *vfc_cmd;
1535 struct ibmvfc_event *evt;
1536 u8 tag[2];
1537 int rc;
1538
1539 if (unlikely((rc = fc_remote_port_chkready(rport))) ||
1540 unlikely((rc = ibmvfc_host_chkready(vhost)))) {
1541 cmnd->result = rc;
1542 done(cmnd);
1543 return 0;
1544 }
1545
1546 cmnd->result = (DID_OK << 16);
1547 evt = ibmvfc_get_event(vhost);
1548 ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
1549 evt->cmnd = cmnd;
1550 cmnd->scsi_done = done;
1551 vfc_cmd = &evt->iu.cmd;
1552 memset(vfc_cmd, 0, sizeof(*vfc_cmd));
1553 vfc_cmd->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1554 vfc_cmd->resp.len = sizeof(vfc_cmd->rsp);
1555 vfc_cmd->frame_type = IBMVFC_SCSI_FCP_TYPE;
1556 vfc_cmd->payload_len = sizeof(vfc_cmd->iu);
1557 vfc_cmd->resp_len = sizeof(vfc_cmd->rsp);
1558 vfc_cmd->cancel_key = (unsigned long)cmnd->device->hostdata;
1559 vfc_cmd->tgt_scsi_id = rport->port_id;
1560 if ((rport->supported_classes & FC_COS_CLASS3) &&
1561 (fc_host_supported_classes(vhost->host) & FC_COS_CLASS3))
1562 vfc_cmd->flags = IBMVFC_CLASS_3_ERR;
1563 vfc_cmd->iu.xfer_len = scsi_bufflen(cmnd);
1564 int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
1565 memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
1566
1567 if (scsi_populate_tag_msg(cmnd, tag)) {
1568 vfc_cmd->task_tag = tag[1];
1569 switch (tag[0]) {
1570 case MSG_SIMPLE_TAG:
1571 vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
1572 break;
1573 case MSG_HEAD_TAG:
1574 vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
1575 break;
1576 case MSG_ORDERED_TAG:
1577 vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
1578 break;
1579 };
1580 }
1581
1582 if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
1583 return ibmvfc_send_event(evt, vhost, 0);
1584
1585 ibmvfc_free_event(evt);
1586 if (rc == -ENOMEM)
1587 return SCSI_MLQUEUE_HOST_BUSY;
1588
1589 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1590 scmd_printk(KERN_ERR, cmnd,
1591 "Failed to map DMA buffer for command. rc=%d\n", rc);
1592
1593 cmnd->result = DID_ERROR << 16;
1594 done(cmnd);
1595 return 0;
1596}
1597
1598/**
1599 * ibmvfc_sync_completion - Signal that a synchronous command has completed
1600 * @evt: ibmvfc event struct
1601 *
1602 **/
1603static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
1604{
1605 /* copy the response back */
1606 if (evt->sync_iu)
1607 *evt->sync_iu = *evt->xfer_iu;
1608
1609 complete(&evt->comp);
1610}
1611
1612/**
1613 * ibmvfc_reset_device - Reset the device with the specified reset type
1614 * @sdev: scsi device to reset
1615 * @type: reset type
1616 * @desc: reset type description for log messages
1617 *
1618 * Returns:
1619 * 0 on success / other on failure
1620 **/
1621static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
1622{
1623 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1624 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1625 struct ibmvfc_cmd *tmf;
1626 struct ibmvfc_event *evt;
1627 union ibmvfc_iu rsp_iu;
1628 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1629 int rsp_rc = -EBUSY;
1630 unsigned long flags;
1631 int rsp_code = 0;
1632
1633 spin_lock_irqsave(vhost->host->host_lock, flags);
1634 if (vhost->state == IBMVFC_ACTIVE) {
1635 evt = ibmvfc_get_event(vhost);
1636 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1637
1638 tmf = &evt->iu.cmd;
1639 memset(tmf, 0, sizeof(*tmf));
1640 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1641 tmf->resp.len = sizeof(tmf->rsp);
1642 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1643 tmf->payload_len = sizeof(tmf->iu);
1644 tmf->resp_len = sizeof(tmf->rsp);
1645 tmf->cancel_key = (unsigned long)sdev->hostdata;
1646 tmf->tgt_scsi_id = rport->port_id;
1647 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1648 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1649 tmf->iu.tmf_flags = type;
1650 evt->sync_iu = &rsp_iu;
1651
1652 init_completion(&evt->comp);
1653 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1654 }
1655 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1656
1657 if (rsp_rc != 0) {
1658 sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
1659 desc, rsp_rc);
1660 return -EIO;
1661 }
1662
1663 sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
1664 wait_for_completion(&evt->comp);
1665
1666 if (rsp_iu.cmd.status) {
1667 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1668 rsp_code = fc_rsp->data.info.rsp_code;
1669
1670 sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
1671 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1672 desc, ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1673 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1674 fc_rsp->scsi_status);
1675 rsp_rc = -EIO;
1676 } else
1677 sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
1678
1679 spin_lock_irqsave(vhost->host->host_lock, flags);
1680 ibmvfc_free_event(evt);
1681 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1682 return rsp_rc;
1683}
1684
1685/**
1686 * ibmvfc_abort_task_set - Abort outstanding commands to the device
1687 * @sdev: scsi device to abort commands
1688 *
1689 * This sends an Abort Task Set to the VIOS for the specified device. This does
1690 * NOT send any cancel to the VIOS. That must be done separately.
1691 *
1692 * Returns:
1693 * 0 on success / other on failure
1694 **/
1695static int ibmvfc_abort_task_set(struct scsi_device *sdev)
1696{
1697 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1698 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1699 struct ibmvfc_cmd *tmf;
1700 struct ibmvfc_event *evt, *found_evt;
1701 union ibmvfc_iu rsp_iu;
1702 struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
1703 int rsp_rc = -EBUSY;
1704 unsigned long flags;
1705 int rsp_code = 0;
1706
1707 spin_lock_irqsave(vhost->host->host_lock, flags);
1708 found_evt = NULL;
1709 list_for_each_entry(evt, &vhost->sent, queue) {
1710 if (evt->cmnd && evt->cmnd->device == sdev) {
1711 found_evt = evt;
1712 break;
1713 }
1714 }
1715
1716 if (!found_evt) {
1717 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1718 sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
1719 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1720 return 0;
1721 }
1722
1723 if (vhost->state == IBMVFC_ACTIVE) {
1724 evt = ibmvfc_get_event(vhost);
1725 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
1726
1727 tmf = &evt->iu.cmd;
1728 memset(tmf, 0, sizeof(*tmf));
1729 tmf->resp.va = (u64)evt->crq.ioba + offsetof(struct ibmvfc_cmd, rsp);
1730 tmf->resp.len = sizeof(tmf->rsp);
1731 tmf->frame_type = IBMVFC_SCSI_FCP_TYPE;
1732 tmf->payload_len = sizeof(tmf->iu);
1733 tmf->resp_len = sizeof(tmf->rsp);
1734 tmf->cancel_key = (unsigned long)sdev->hostdata;
1735 tmf->tgt_scsi_id = rport->port_id;
1736 int_to_scsilun(sdev->lun, &tmf->iu.lun);
1737 tmf->flags = (IBMVFC_NO_MEM_DESC | IBMVFC_TMF);
1738 tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
1739 evt->sync_iu = &rsp_iu;
1740
1741 init_completion(&evt->comp);
1742 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1743 }
1744
1745 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1746
1747 if (rsp_rc != 0) {
1748 sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
1749 return -EIO;
1750 }
1751
1752 sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
1753 wait_for_completion(&evt->comp);
1754
1755 if (rsp_iu.cmd.status) {
1756 if (fc_rsp->flags & FCP_RSP_LEN_VALID)
1757 rsp_code = fc_rsp->data.info.rsp_code;
1758
1759 sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
1760 "flags: %x fcp_rsp: %x, scsi_status: %x\n",
1761 ibmvfc_get_cmd_error(rsp_iu.cmd.status, rsp_iu.cmd.error),
1762 rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
1763 fc_rsp->scsi_status);
1764 rsp_rc = -EIO;
1765 } else
1766 sdev_printk(KERN_INFO, sdev, "Abort successful\n");
1767
1768 spin_lock_irqsave(vhost->host->host_lock, flags);
1769 ibmvfc_free_event(evt);
1770 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1771 return rsp_rc;
1772}
1773
1774/**
1775 * ibmvfc_cancel_all - Cancel all outstanding commands to the device
1776 * @sdev: scsi device to cancel commands
1777 * @type: type of error recovery being performed
1778 *
1779 * This sends a cancel to the VIOS for the specified device. This does
1780 * NOT send any abort to the actual device. That must be done separately.
1781 *
1782 * Returns:
1783 * 0 on success / other on failure
1784 **/
1785static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
1786{
1787 struct ibmvfc_host *vhost = shost_priv(sdev->host);
1788 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1789 struct ibmvfc_tmf *tmf;
1790 struct ibmvfc_event *evt, *found_evt;
1791 union ibmvfc_iu rsp;
1792 int rsp_rc = -EBUSY;
1793 unsigned long flags;
1794 u16 status;
1795
1796 ENTER;
1797 spin_lock_irqsave(vhost->host->host_lock, flags);
1798 found_evt = NULL;
1799 list_for_each_entry(evt, &vhost->sent, queue) {
1800 if (evt->cmnd && evt->cmnd->device == sdev) {
1801 found_evt = evt;
1802 break;
1803 }
1804 }
1805
1806 if (!found_evt) {
1807 if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
1808 sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
1809 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1810 return 0;
1811 }
1812
1813 if (vhost->state == IBMVFC_ACTIVE) {
1814 evt = ibmvfc_get_event(vhost);
1815 ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
1816
1817 tmf = &evt->iu.tmf;
1818 memset(tmf, 0, sizeof(*tmf));
1819 tmf->common.version = 1;
1820 tmf->common.opcode = IBMVFC_TMF_MAD;
1821 tmf->common.length = sizeof(*tmf);
1822 tmf->scsi_id = rport->port_id;
1823 int_to_scsilun(sdev->lun, &tmf->lun);
1824 tmf->flags = (type | IBMVFC_TMF_LUA_VALID);
1825 tmf->cancel_key = (unsigned long)sdev->hostdata;
1826 tmf->my_cancel_key = (IBMVFC_TMF_CANCEL_KEY | (unsigned long)sdev->hostdata);
1827
1828 evt->sync_iu = &rsp;
1829 init_completion(&evt->comp);
1830 rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
1831 }
1832
1833 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1834
1835 if (rsp_rc != 0) {
1836 sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
1837 return -EIO;
1838 }
1839
1840 sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
1841
1842 wait_for_completion(&evt->comp);
1843 status = rsp.mad_common.status;
1844 spin_lock_irqsave(vhost->host->host_lock, flags);
1845 ibmvfc_free_event(evt);
1846 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1847
1848 if (status != IBMVFC_MAD_SUCCESS) {
1849 sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
1850 return -EIO;
1851 }
1852
1853 sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
1854 return 0;
1855}
1856
1857/**
1858 * ibmvfc_eh_abort_handler - Abort a command
1859 * @cmd: scsi command to abort
1860 *
1861 * Returns:
1862 * SUCCESS / FAILED
1863 **/
1864static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
1865{
1866 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1867 struct ibmvfc_event *evt, *pos;
1868 int cancel_rc, abort_rc;
1869 unsigned long flags;
1870
1871 ENTER;
1872 ibmvfc_wait_while_resetting(vhost);
1873 cancel_rc = ibmvfc_cancel_all(cmd->device, IBMVFC_TMF_ABORT_TASK_SET);
1874 abort_rc = ibmvfc_abort_task_set(cmd->device);
1875
1876 if (!cancel_rc && !abort_rc) {
1877 spin_lock_irqsave(vhost->host->host_lock, flags);
1878 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
1879 if (evt->cmnd && evt->cmnd->device == cmd->device)
1880 ibmvfc_fail_request(evt, DID_ABORT);
1881 }
1882 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1883 LEAVE;
1884 return SUCCESS;
1885 }
1886
1887 LEAVE;
1888 return FAILED;
1889}
1890
1891/**
1892 * ibmvfc_eh_device_reset_handler - Reset a single LUN
1893 * @cmd: scsi command struct
1894 *
1895 * Returns:
1896 * SUCCESS / FAILED
1897 **/
1898static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
1899{
1900 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1901 struct ibmvfc_event *evt, *pos;
1902 int cancel_rc, reset_rc;
1903 unsigned long flags;
1904
1905 ENTER;
1906 ibmvfc_wait_while_resetting(vhost);
1907 cancel_rc = ibmvfc_cancel_all(cmd->device, IBMVFC_TMF_LUN_RESET);
1908 reset_rc = ibmvfc_reset_device(cmd->device, IBMVFC_LUN_RESET, "LUN");
1909
1910 if (!cancel_rc && !reset_rc) {
1911 spin_lock_irqsave(vhost->host->host_lock, flags);
1912 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
1913 if (evt->cmnd && evt->cmnd->device == cmd->device)
1914 ibmvfc_fail_request(evt, DID_ABORT);
1915 }
1916 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1917 LEAVE;
1918 return SUCCESS;
1919 }
1920
1921 LEAVE;
1922 return FAILED;
1923}
1924
1925/**
1926 * ibmvfc_dev_cancel_all - Device iterated cancel all function
1927 * @sdev: scsi device struct
1928 * @data: return code
1929 *
1930 **/
1931static void ibmvfc_dev_cancel_all(struct scsi_device *sdev, void *data)
1932{
1933 unsigned long *rc = data;
1934 *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
1935}
1936
1937/**
1938 * ibmvfc_dev_abort_all - Device iterated abort task set function
1939 * @sdev: scsi device struct
1940 * @data: return code
1941 *
1942 **/
1943static void ibmvfc_dev_abort_all(struct scsi_device *sdev, void *data)
1944{
1945 unsigned long *rc = data;
1946 *rc |= ibmvfc_abort_task_set(sdev);
1947}
1948
1949/**
1950 * ibmvfc_eh_target_reset_handler - Reset the target
1951 * @cmd: scsi command struct
1952 *
1953 * Returns:
1954 * SUCCESS / FAILED
1955 **/
1956static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
1957{
1958 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1959 struct scsi_target *starget = scsi_target(cmd->device);
1960 struct ibmvfc_event *evt, *pos;
1961 int reset_rc;
1962 unsigned long cancel_rc = 0;
1963 unsigned long flags;
1964
1965 ENTER;
1966 ibmvfc_wait_while_resetting(vhost);
1967 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
1968 reset_rc = ibmvfc_reset_device(cmd->device, IBMVFC_TARGET_RESET, "target");
1969
1970 if (!cancel_rc && !reset_rc) {
1971 spin_lock_irqsave(vhost->host->host_lock, flags);
1972 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
1973 if (evt->cmnd && scsi_target(evt->cmnd->device) == starget)
1974 ibmvfc_fail_request(evt, DID_ABORT);
1975 }
1976 spin_unlock_irqrestore(vhost->host->host_lock, flags);
1977 LEAVE;
1978 return SUCCESS;
1979 }
1980
1981 LEAVE;
1982 return FAILED;
1983}
1984
1985/**
1986 * ibmvfc_eh_host_reset_handler - Reset the connection to the server
1987 * @cmd: struct scsi_cmnd having problems
1988 *
1989 **/
1990static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
1991{
1992 int rc;
1993 struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
1994
1995 dev_err(vhost->dev, "Resetting connection due to error recovery\n");
1996 rc = ibmvfc_issue_fc_host_lip(vhost->host);
1997 return rc ? FAILED : SUCCESS;
1998}
1999
2000/**
2001 * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
2002 * @rport: rport struct
2003 *
2004 * Return value:
2005 * none
2006 **/
2007static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
2008{
2009 struct scsi_target *starget = to_scsi_target(&rport->dev);
2010 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
2011 struct ibmvfc_host *vhost = shost_priv(shost);
2012 struct ibmvfc_event *evt, *pos;
2013 unsigned long cancel_rc = 0;
2014 unsigned long abort_rc = 0;
2015 unsigned long flags;
2016
2017 ENTER;
2018 starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all);
2019 starget_for_each_device(starget, &abort_rc, ibmvfc_dev_abort_all);
2020
2021 if (!cancel_rc && !abort_rc) {
2022 spin_lock_irqsave(shost->host_lock, flags);
2023 list_for_each_entry_safe(evt, pos, &vhost->sent, queue) {
2024 if (evt->cmnd && scsi_target(evt->cmnd->device) == starget)
2025 ibmvfc_fail_request(evt, DID_ABORT);
2026 }
2027 spin_unlock_irqrestore(shost->host_lock, flags);
2028 } else
2029 ibmvfc_issue_fc_host_lip(shost);
072b91f9
BK
2030 LEAVE;
2031}
2032
2033static const struct {
2034 enum ibmvfc_async_event ae;
2035 const char *desc;
2036} ae_desc [] = {
2037 { IBMVFC_AE_ELS_PLOGI, "PLOGI" },
2038 { IBMVFC_AE_ELS_LOGO, "LOGO" },
2039 { IBMVFC_AE_ELS_PRLO, "PRLO" },
2040 { IBMVFC_AE_SCN_NPORT, "N-Port SCN" },
2041 { IBMVFC_AE_SCN_GROUP, "Group SCN" },
2042 { IBMVFC_AE_SCN_DOMAIN, "Domain SCN" },
2043 { IBMVFC_AE_SCN_FABRIC, "Fabric SCN" },
2044 { IBMVFC_AE_LINK_UP, "Link Up" },
2045 { IBMVFC_AE_LINK_DOWN, "Link Down" },
2046 { IBMVFC_AE_LINK_DEAD, "Link Dead" },
2047 { IBMVFC_AE_HALT, "Halt" },
2048 { IBMVFC_AE_RESUME, "Resume" },
2049 { IBMVFC_AE_ADAPTER_FAILED, "Adapter Failed" },
2050};
2051
2052static const char *unknown_ae = "Unknown async";
2053
2054/**
2055 * ibmvfc_get_ae_desc - Get text description for async event
2056 * @ae: async event
2057 *
2058 **/
2059static const char *ibmvfc_get_ae_desc(u64 ae)
2060{
2061 int i;
2062
2063 for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
2064 if (ae_desc[i].ae == ae)
2065 return ae_desc[i].desc;
2066
2067 return unknown_ae;
2068}
2069
2070/**
2071 * ibmvfc_handle_async - Handle an async event from the adapter
2072 * @crq: crq to process
2073 * @vhost: ibmvfc host struct
2074 *
2075 **/
2076static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
2077 struct ibmvfc_host *vhost)
2078{
2079 const char *desc = ibmvfc_get_ae_desc(crq->event);
2080
3e339946
BK
2081 ibmvfc_log(vhost, 3, "%s event received. scsi_id: %lx, wwpn: %lx,"
2082 " node_name: %lx\n", desc, crq->scsi_id, crq->wwpn, crq->node_name);
072b91f9
BK
2083
2084 switch (crq->event) {
2085 case IBMVFC_AE_LINK_UP:
2086 case IBMVFC_AE_RESUME:
2087 vhost->events_to_log |= IBMVFC_AE_LINKUP;
cf6f10d7 2088 ibmvfc_init_host(vhost, 1);
072b91f9
BK
2089 break;
2090 case IBMVFC_AE_SCN_FABRIC:
2091 vhost->events_to_log |= IBMVFC_AE_RSCN;
cf6f10d7 2092 ibmvfc_init_host(vhost, 1);
072b91f9
BK
2093 break;
2094 case IBMVFC_AE_SCN_NPORT:
2095 case IBMVFC_AE_SCN_GROUP:
2096 case IBMVFC_AE_SCN_DOMAIN:
2097 vhost->events_to_log |= IBMVFC_AE_RSCN;
2098 case IBMVFC_AE_ELS_LOGO:
2099 case IBMVFC_AE_ELS_PRLO:
2100 case IBMVFC_AE_ELS_PLOGI:
2101 ibmvfc_reinit_host(vhost);
2102 break;
2103 case IBMVFC_AE_LINK_DOWN:
2104 case IBMVFC_AE_ADAPTER_FAILED:
2105 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2106 break;
2107 case IBMVFC_AE_LINK_DEAD:
2108 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2109 break;
2110 case IBMVFC_AE_HALT:
2111 ibmvfc_link_down(vhost, IBMVFC_HALTED);
2112 break;
2113 default:
2114 dev_err(vhost->dev, "Unknown async event received: %ld\n", crq->event);
2115 break;
2116 };
2117}
2118
2119/**
2120 * ibmvfc_handle_crq - Handles and frees received events in the CRQ
2121 * @crq: Command/Response queue
2122 * @vhost: ibmvfc host struct
2123 *
2124 **/
2125static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
2126{
2127 long rc;
2128 struct ibmvfc_event *evt = (struct ibmvfc_event *)crq->ioba;
2129
2130 switch (crq->valid) {
2131 case IBMVFC_CRQ_INIT_RSP:
2132 switch (crq->format) {
2133 case IBMVFC_CRQ_INIT:
2134 dev_info(vhost->dev, "Partner initialized\n");
2135 /* Send back a response */
2136 rc = ibmvfc_send_crq_init_complete(vhost);
2137 if (rc == 0)
cf6f10d7 2138 ibmvfc_init_host(vhost, 0);
072b91f9
BK
2139 else
2140 dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
2141 break;
2142 case IBMVFC_CRQ_INIT_COMPLETE:
2143 dev_info(vhost->dev, "Partner initialization complete\n");
cf6f10d7 2144 ibmvfc_init_host(vhost, 0);
072b91f9
BK
2145 break;
2146 default:
2147 dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
2148 }
2149 return;
2150 case IBMVFC_CRQ_XPORT_EVENT:
2151 vhost->state = IBMVFC_NO_CRQ;
2152 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
2153 if (crq->format == IBMVFC_PARTITION_MIGRATED) {
2154 /* We need to re-setup the interpartition connection */
2155 dev_info(vhost->dev, "Re-enabling adapter\n");
2156 vhost->client_migrated = 1;
2157 ibmvfc_purge_requests(vhost, DID_REQUEUE);
2158 if ((rc = ibmvfc_reenable_crq_queue(vhost)) ||
2159 (rc = ibmvfc_send_crq_init(vhost))) {
2160 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2161 dev_err(vhost->dev, "Error after enable (rc=%ld)\n", rc);
2162 } else
2163 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2164 } else {
2165 dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
2166
2167 ibmvfc_purge_requests(vhost, DID_ERROR);
2168 if ((rc = ibmvfc_reset_crq(vhost)) ||
2169 (rc = ibmvfc_send_crq_init(vhost))) {
2170 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
2171 dev_err(vhost->dev, "Error after reset (rc=%ld)\n", rc);
2172 } else
2173 ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
2174 }
2175 return;
2176 case IBMVFC_CRQ_CMD_RSP:
2177 break;
2178 default:
2179 dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
2180 return;
2181 }
2182
2183 if (crq->format == IBMVFC_ASYNC_EVENT)
2184 return;
2185
2186 /* The only kind of payload CRQs we should get are responses to
2187 * things we send. Make sure this response is to something we
2188 * actually sent
2189 */
2190 if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
2191 dev_err(vhost->dev, "Returned correlation_token 0x%08lx is invalid!\n",
2192 crq->ioba);
2193 return;
2194 }
2195
2196 if (unlikely(atomic_read(&evt->free))) {
2197 dev_err(vhost->dev, "Received duplicate correlation_token 0x%08lx!\n",
2198 crq->ioba);
2199 return;
2200 }
2201
2202 del_timer(&evt->timer);
2203 list_del(&evt->queue);
2204 ibmvfc_trc_end(evt);
2205 evt->done(evt);
2206}
2207
2208/**
2209 * ibmvfc_scan_finished - Check if the device scan is done.
2210 * @shost: scsi host struct
2211 * @time: current elapsed time
2212 *
2213 * Returns:
2214 * 0 if scan is not done / 1 if scan is done
2215 **/
2216static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2217{
2218 unsigned long flags;
2219 struct ibmvfc_host *vhost = shost_priv(shost);
2220 int done = 0;
2221
2222 spin_lock_irqsave(shost->host_lock, flags);
2223 if (time >= (init_timeout * HZ)) {
2224 dev_info(vhost->dev, "Scan taking longer than %d seconds, "
2225 "continuing initialization\n", init_timeout);
2226 done = 1;
2227 }
2228
2229 if (vhost->state != IBMVFC_NO_CRQ && vhost->action == IBMVFC_HOST_ACTION_NONE)
2230 done = 1;
2231 spin_unlock_irqrestore(shost->host_lock, flags);
2232 return done;
2233}
2234
2235/**
2236 * ibmvfc_slave_alloc - Setup the device's task set value
2237 * @sdev: struct scsi_device device to configure
2238 *
2239 * Set the device's task set value so that error handling works as
2240 * expected.
2241 *
2242 * Returns:
2243 * 0 on success / -ENXIO if device does not exist
2244 **/
2245static int ibmvfc_slave_alloc(struct scsi_device *sdev)
2246{
2247 struct Scsi_Host *shost = sdev->host;
2248 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
2249 struct ibmvfc_host *vhost = shost_priv(shost);
2250 unsigned long flags = 0;
2251
2252 if (!rport || fc_remote_port_chkready(rport))
2253 return -ENXIO;
2254
2255 spin_lock_irqsave(shost->host_lock, flags);
2256 sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
2257 spin_unlock_irqrestore(shost->host_lock, flags);
2258 return 0;
2259}
2260
2261/**
2262 * ibmvfc_slave_configure - Configure the device
2263 * @sdev: struct scsi_device device to configure
2264 *
2265 * Enable allow_restart for a device if it is a disk. Adjust the
2266 * queue_depth here also.
2267 *
2268 * Returns:
2269 * 0
2270 **/
2271static int ibmvfc_slave_configure(struct scsi_device *sdev)
2272{
2273 struct Scsi_Host *shost = sdev->host;
2274 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
2275 unsigned long flags = 0;
2276
2277 spin_lock_irqsave(shost->host_lock, flags);
2278 if (sdev->type == TYPE_DISK)
2279 sdev->allow_restart = 1;
2280
2281 if (sdev->tagged_supported) {
2282 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2283 scsi_activate_tcq(sdev, sdev->queue_depth);
2284 } else
2285 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2286
2287 rport->dev_loss_tmo = dev_loss_tmo;
2288 spin_unlock_irqrestore(shost->host_lock, flags);
2289 return 0;
2290}
2291
2292/**
2293 * ibmvfc_change_queue_depth - Change the device's queue depth
2294 * @sdev: scsi device struct
2295 * @qdepth: depth to set
2296 *
2297 * Return value:
2298 * actual depth set
2299 **/
2300static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth)
2301{
2302 if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
2303 qdepth = IBMVFC_MAX_CMDS_PER_LUN;
2304
2305 scsi_adjust_queue_depth(sdev, 0, qdepth);
2306 return sdev->queue_depth;
2307}
2308
2309/**
2310 * ibmvfc_change_queue_type - Change the device's queue type
2311 * @sdev: scsi device struct
2312 * @tag_type: type of tags to use
2313 *
2314 * Return value:
2315 * actual queue type set
2316 **/
2317static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
2318{
2319 if (sdev->tagged_supported) {
2320 scsi_set_tag_type(sdev, tag_type);
2321
2322 if (tag_type)
2323 scsi_activate_tcq(sdev, sdev->queue_depth);
2324 else
2325 scsi_deactivate_tcq(sdev, sdev->queue_depth);
2326 } else
2327 tag_type = 0;
2328
2329 return tag_type;
2330}
2331
2332static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
2333 struct device_attribute *attr, char *buf)
2334{
2335 struct Scsi_Host *shost = class_to_shost(dev);
2336 struct ibmvfc_host *vhost = shost_priv(shost);
2337
2338 return snprintf(buf, PAGE_SIZE, "%s\n",
2339 vhost->login_buf->resp.partition_name);
2340}
2341
2342static struct device_attribute ibmvfc_host_partition_name = {
2343 .attr = {
2344 .name = "partition_name",
2345 .mode = S_IRUGO,
2346 },
2347 .show = ibmvfc_show_host_partition_name,
2348};
2349
2350static ssize_t ibmvfc_show_host_device_name(struct device *dev,
2351 struct device_attribute *attr, char *buf)
2352{
2353 struct Scsi_Host *shost = class_to_shost(dev);
2354 struct ibmvfc_host *vhost = shost_priv(shost);
2355
2356 return snprintf(buf, PAGE_SIZE, "%s\n",
2357 vhost->login_buf->resp.device_name);
2358}
2359
2360static struct device_attribute ibmvfc_host_device_name = {
2361 .attr = {
2362 .name = "device_name",
2363 .mode = S_IRUGO,
2364 },
2365 .show = ibmvfc_show_host_device_name,
2366};
2367
2368static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
2369 struct device_attribute *attr, char *buf)
2370{
2371 struct Scsi_Host *shost = class_to_shost(dev);
2372 struct ibmvfc_host *vhost = shost_priv(shost);
2373
2374 return snprintf(buf, PAGE_SIZE, "%s\n",
2375 vhost->login_buf->resp.port_loc_code);
2376}
2377
2378static struct device_attribute ibmvfc_host_loc_code = {
2379 .attr = {
2380 .name = "port_loc_code",
2381 .mode = S_IRUGO,
2382 },
2383 .show = ibmvfc_show_host_loc_code,
2384};
2385
2386static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
2387 struct device_attribute *attr, char *buf)
2388{
2389 struct Scsi_Host *shost = class_to_shost(dev);
2390 struct ibmvfc_host *vhost = shost_priv(shost);
2391
2392 return snprintf(buf, PAGE_SIZE, "%s\n",
2393 vhost->login_buf->resp.drc_name);
2394}
2395
2396static struct device_attribute ibmvfc_host_drc_name = {
2397 .attr = {
2398 .name = "drc_name",
2399 .mode = S_IRUGO,
2400 },
2401 .show = ibmvfc_show_host_drc_name,
2402};
2403
2404static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
2405 struct device_attribute *attr, char *buf)
2406{
2407 struct Scsi_Host *shost = class_to_shost(dev);
2408 struct ibmvfc_host *vhost = shost_priv(shost);
2409 return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
2410}
2411
2412static struct device_attribute ibmvfc_host_npiv_version = {
2413 .attr = {
2414 .name = "npiv_version",
2415 .mode = S_IRUGO,
2416 },
2417 .show = ibmvfc_show_host_npiv_version,
2418};
2419
2420/**
2421 * ibmvfc_show_log_level - Show the adapter's error logging level
2422 * @dev: class device struct
2423 * @buf: buffer
2424 *
2425 * Return value:
2426 * number of bytes printed to buffer
2427 **/
2428static ssize_t ibmvfc_show_log_level(struct device *dev,
2429 struct device_attribute *attr, char *buf)
2430{
2431 struct Scsi_Host *shost = class_to_shost(dev);
2432 struct ibmvfc_host *vhost = shost_priv(shost);
2433 unsigned long flags = 0;
2434 int len;
2435
2436 spin_lock_irqsave(shost->host_lock, flags);
2437 len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
2438 spin_unlock_irqrestore(shost->host_lock, flags);
2439 return len;
2440}
2441
2442/**
2443 * ibmvfc_store_log_level - Change the adapter's error logging level
2444 * @dev: class device struct
2445 * @buf: buffer
2446 *
2447 * Return value:
2448 * number of bytes printed to buffer
2449 **/
2450static ssize_t ibmvfc_store_log_level(struct device *dev,
2451 struct device_attribute *attr,
2452 const char *buf, size_t count)
2453{
2454 struct Scsi_Host *shost = class_to_shost(dev);
2455 struct ibmvfc_host *vhost = shost_priv(shost);
2456 unsigned long flags = 0;
2457
2458 spin_lock_irqsave(shost->host_lock, flags);
2459 vhost->log_level = simple_strtoul(buf, NULL, 10);
2460 spin_unlock_irqrestore(shost->host_lock, flags);
2461 return strlen(buf);
2462}
2463
2464static struct device_attribute ibmvfc_log_level_attr = {
2465 .attr = {
2466 .name = "log_level",
2467 .mode = S_IRUGO | S_IWUSR,
2468 },
2469 .show = ibmvfc_show_log_level,
2470 .store = ibmvfc_store_log_level
2471};
2472
2473#ifdef CONFIG_SCSI_IBMVFC_TRACE
2474/**
2475 * ibmvfc_read_trace - Dump the adapter trace
2476 * @kobj: kobject struct
2477 * @bin_attr: bin_attribute struct
2478 * @buf: buffer
2479 * @off: offset
2480 * @count: buffer size
2481 *
2482 * Return value:
2483 * number of bytes printed to buffer
2484 **/
2485static ssize_t ibmvfc_read_trace(struct kobject *kobj,
2486 struct bin_attribute *bin_attr,
2487 char *buf, loff_t off, size_t count)
2488{
2489 struct device *dev = container_of(kobj, struct device, kobj);
2490 struct Scsi_Host *shost = class_to_shost(dev);
2491 struct ibmvfc_host *vhost = shost_priv(shost);
2492 unsigned long flags = 0;
2493 int size = IBMVFC_TRACE_SIZE;
2494 char *src = (char *)vhost->trace;
2495
2496 if (off > size)
2497 return 0;
2498 if (off + count > size) {
2499 size -= off;
2500 count = size;
2501 }
2502
2503 spin_lock_irqsave(shost->host_lock, flags);
2504 memcpy(buf, &src[off], count);
2505 spin_unlock_irqrestore(shost->host_lock, flags);
2506 return count;
2507}
2508
2509static struct bin_attribute ibmvfc_trace_attr = {
2510 .attr = {
2511 .name = "trace",
2512 .mode = S_IRUGO,
2513 },
2514 .size = 0,
2515 .read = ibmvfc_read_trace,
2516};
2517#endif
2518
2519static struct device_attribute *ibmvfc_attrs[] = {
2520 &ibmvfc_host_partition_name,
2521 &ibmvfc_host_device_name,
2522 &ibmvfc_host_loc_code,
2523 &ibmvfc_host_drc_name,
2524 &ibmvfc_host_npiv_version,
2525 &ibmvfc_log_level_attr,
2526 NULL
2527};
2528
2529static struct scsi_host_template driver_template = {
2530 .module = THIS_MODULE,
2531 .name = "IBM POWER Virtual FC Adapter",
2532 .proc_name = IBMVFC_NAME,
2533 .queuecommand = ibmvfc_queuecommand,
2534 .eh_abort_handler = ibmvfc_eh_abort_handler,
2535 .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
2536 .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
2537 .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
2538 .slave_alloc = ibmvfc_slave_alloc,
2539 .slave_configure = ibmvfc_slave_configure,
2540 .scan_finished = ibmvfc_scan_finished,
2541 .change_queue_depth = ibmvfc_change_queue_depth,
2542 .change_queue_type = ibmvfc_change_queue_type,
2543 .cmd_per_lun = 16,
2544 .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
2545 .this_id = -1,
2546 .sg_tablesize = SG_ALL,
2547 .max_sectors = IBMVFC_MAX_SECTORS,
2548 .use_clustering = ENABLE_CLUSTERING,
2549 .shost_attrs = ibmvfc_attrs,
2550};
2551
2552/**
2553 * ibmvfc_next_async_crq - Returns the next entry in async queue
2554 * @vhost: ibmvfc host struct
2555 *
2556 * Returns:
2557 * Pointer to next entry in queue / NULL if empty
2558 **/
2559static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
2560{
2561 struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
2562 struct ibmvfc_async_crq *crq;
2563
2564 crq = &async_crq->msgs[async_crq->cur];
2565 if (crq->valid & 0x80) {
2566 if (++async_crq->cur == async_crq->size)
2567 async_crq->cur = 0;
2568 } else
2569 crq = NULL;
2570
2571 return crq;
2572}
2573
2574/**
2575 * ibmvfc_next_crq - Returns the next entry in message queue
2576 * @vhost: ibmvfc host struct
2577 *
2578 * Returns:
2579 * Pointer to next entry in queue / NULL if empty
2580 **/
2581static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
2582{
2583 struct ibmvfc_crq_queue *queue = &vhost->crq;
2584 struct ibmvfc_crq *crq;
2585
2586 crq = &queue->msgs[queue->cur];
2587 if (crq->valid & 0x80) {
2588 if (++queue->cur == queue->size)
2589 queue->cur = 0;
2590 } else
2591 crq = NULL;
2592
2593 return crq;
2594}
2595
2596/**
2597 * ibmvfc_interrupt - Interrupt handler
2598 * @irq: number of irq to handle, not used
2599 * @dev_instance: ibmvfc_host that received interrupt
2600 *
2601 * Returns:
2602 * IRQ_HANDLED
2603 **/
2604static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
2605{
2606 struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
2607 struct vio_dev *vdev = to_vio_dev(vhost->dev);
2608 struct ibmvfc_crq *crq;
2609 struct ibmvfc_async_crq *async;
2610 unsigned long flags;
2611 int done = 0;
2612
2613 spin_lock_irqsave(vhost->host->host_lock, flags);
2614 vio_disable_interrupts(to_vio_dev(vhost->dev));
2615 while (!done) {
2616 /* Pull all the valid messages off the CRQ */
2617 while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2618 ibmvfc_handle_crq(crq, vhost);
2619 crq->valid = 0;
2620 }
2621
2622 /* Pull all the valid messages off the async CRQ */
2623 while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2624 ibmvfc_handle_async(async, vhost);
2625 async->valid = 0;
2626 }
2627
2628 vio_enable_interrupts(vdev);
2629 if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
2630 vio_disable_interrupts(vdev);
2631 ibmvfc_handle_crq(crq, vhost);
2632 crq->valid = 0;
2633 } else if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
2634 vio_disable_interrupts(vdev);
2635 ibmvfc_handle_async(async, vhost);
2636 crq->valid = 0;
2637 } else
2638 done = 1;
2639 }
2640
2641 spin_unlock_irqrestore(vhost->host->host_lock, flags);
2642 return IRQ_HANDLED;
2643}
2644
2645/**
2646 * ibmvfc_init_tgt - Set the next init job step for the target
2647 * @tgt: ibmvfc target struct
2648 * @job_step: job step to perform
2649 *
2650 **/
2651static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
2652 void (*job_step) (struct ibmvfc_target *))
2653{
2654 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
2655 tgt->job_step = job_step;
2656 wake_up(&tgt->vhost->work_wait_q);
2657}
2658
2659/**
2660 * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
2661 * @tgt: ibmvfc target struct
2662 * @job_step: initialization job step
2663 *
2664 **/
2665static void ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
2666 void (*job_step) (struct ibmvfc_target *))
2667{
2668 if (++tgt->init_retries > IBMVFC_MAX_INIT_RETRIES) {
2669 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2670 wake_up(&tgt->vhost->work_wait_q);
2671 } else
2672 ibmvfc_init_tgt(tgt, job_step);
2673}
2674
072b91f9
BK
2675/**
2676 * ibmvfc_tgt_prli_done - Completion handler for Process Login
2677 * @evt: ibmvfc event struct
2678 *
2679 **/
2680static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
2681{
2682 struct ibmvfc_target *tgt = evt->tgt;
2683 struct ibmvfc_host *vhost = evt->vhost;
2684 struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
2685 u32 status = rsp->common.status;
2686
2687 vhost->discovery_threads--;
2688 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2689 switch (status) {
2690 case IBMVFC_MAD_SUCCESS:
2691 tgt_dbg(tgt, "Process Login succeeded\n");
2692 tgt->need_login = 0;
2693 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_ADD_RPORT);
2694 break;
2695 case IBMVFC_MAD_DRIVER_FAILED:
2696 break;
2697 case IBMVFC_MAD_CRQ_ERROR:
2698 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2699 break;
2700 case IBMVFC_MAD_FAILED:
2701 default:
2702 tgt_err(tgt, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
2703 ibmvfc_get_cmd_error(rsp->status, rsp->error),
2704 rsp->status, rsp->error, status);
2705 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2706 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
2707 break;
2708 };
2709
2710 kref_put(&tgt->kref, ibmvfc_release_tgt);
2711 ibmvfc_free_event(evt);
2712 wake_up(&vhost->work_wait_q);
2713}
2714
2715/**
2716 * ibmvfc_tgt_send_prli - Send a process login
2717 * @tgt: ibmvfc target struct
2718 *
2719 **/
2720static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
2721{
2722 struct ibmvfc_process_login *prli;
2723 struct ibmvfc_host *vhost = tgt->vhost;
2724 struct ibmvfc_event *evt;
2725
2726 if (vhost->discovery_threads >= disc_threads)
2727 return;
2728
2729 kref_get(&tgt->kref);
2730 evt = ibmvfc_get_event(vhost);
2731 vhost->discovery_threads++;
2732 ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
2733 evt->tgt = tgt;
2734 prli = &evt->iu.prli;
2735 memset(prli, 0, sizeof(*prli));
2736 prli->common.version = 1;
2737 prli->common.opcode = IBMVFC_PROCESS_LOGIN;
2738 prli->common.length = sizeof(*prli);
2739 prli->scsi_id = tgt->scsi_id;
2740
2741 prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
2742 prli->parms.flags = IBMVFC_PRLI_EST_IMG_PAIR;
2743 prli->parms.service_parms = IBMVFC_PRLI_INITIATOR_FUNC;
2744
2745 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2746 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2747 vhost->discovery_threads--;
2748 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2749 kref_put(&tgt->kref, ibmvfc_release_tgt);
2750 } else
2751 tgt_dbg(tgt, "Sent process login\n");
2752}
2753
2754/**
2755 * ibmvfc_tgt_plogi_done - Completion handler for Port Login
2756 * @evt: ibmvfc event struct
2757 *
2758 **/
2759static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
2760{
2761 struct ibmvfc_target *tgt = evt->tgt;
2762 struct ibmvfc_host *vhost = evt->vhost;
2763 struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
2764 u32 status = rsp->common.status;
2765
2766 vhost->discovery_threads--;
2767 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2768 switch (status) {
2769 case IBMVFC_MAD_SUCCESS:
2770 tgt_dbg(tgt, "Port Login succeeded\n");
2771 if (tgt->ids.port_name &&
2772 tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
2773 vhost->reinit = 1;
2774 tgt_dbg(tgt, "Port re-init required\n");
2775 break;
2776 }
2777 tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
2778 tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
2779 tgt->ids.port_id = tgt->scsi_id;
2780 tgt->ids.roles = FC_PORT_ROLE_FCP_TARGET;
2781 memcpy(&tgt->service_parms, &rsp->service_parms,
2782 sizeof(tgt->service_parms));
2783 memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
2784 sizeof(tgt->service_parms_change));
2785 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
2786 break;
2787 case IBMVFC_MAD_DRIVER_FAILED:
2788 break;
2789 case IBMVFC_MAD_CRQ_ERROR:
2790 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2791 break;
2792 case IBMVFC_MAD_FAILED:
2793 default:
2794 tgt_err(tgt, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2795 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
2796 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
2797 ibmvfc_get_ls_explain(rsp->fc_explain), rsp->fc_explain, status);
2798
2799 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
2800 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
2801 break;
2802 };
2803
2804 kref_put(&tgt->kref, ibmvfc_release_tgt);
2805 ibmvfc_free_event(evt);
2806 wake_up(&vhost->work_wait_q);
2807}
2808
2809/**
2810 * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
2811 * @tgt: ibmvfc target struct
2812 *
2813 **/
2814static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
2815{
2816 struct ibmvfc_port_login *plogi;
2817 struct ibmvfc_host *vhost = tgt->vhost;
2818 struct ibmvfc_event *evt;
2819
2820 if (vhost->discovery_threads >= disc_threads)
2821 return;
2822
2823 kref_get(&tgt->kref);
2824 evt = ibmvfc_get_event(vhost);
2825 vhost->discovery_threads++;
2826 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2827 ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
2828 evt->tgt = tgt;
2829 plogi = &evt->iu.plogi;
2830 memset(plogi, 0, sizeof(*plogi));
2831 plogi->common.version = 1;
2832 plogi->common.opcode = IBMVFC_PORT_LOGIN;
2833 plogi->common.length = sizeof(*plogi);
2834 plogi->scsi_id = tgt->scsi_id;
2835
2836 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2837 vhost->discovery_threads--;
2838 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2839 kref_put(&tgt->kref, ibmvfc_release_tgt);
2840 } else
2841 tgt_dbg(tgt, "Sent port login\n");
2842}
2843
2844/**
2845 * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
2846 * @evt: ibmvfc event struct
2847 *
2848 **/
2849static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
2850{
2851 struct ibmvfc_target *tgt = evt->tgt;
2852 struct ibmvfc_host *vhost = evt->vhost;
2853 struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
2854 u32 status = rsp->common.status;
2855
2856 vhost->discovery_threads--;
2857 ibmvfc_free_event(evt);
2858 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2859
2860 switch (status) {
2861 case IBMVFC_MAD_SUCCESS:
2862 tgt_dbg(tgt, "Implicit Logout succeeded\n");
2863 break;
2864 case IBMVFC_MAD_DRIVER_FAILED:
2865 kref_put(&tgt->kref, ibmvfc_release_tgt);
2866 wake_up(&vhost->work_wait_q);
2867 return;
2868 case IBMVFC_MAD_FAILED:
2869 default:
2870 tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
2871 break;
2872 };
2873
2874 if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
2875 ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
2876 else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
2877 tgt->scsi_id != tgt->new_scsi_id)
2878 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
2879 kref_put(&tgt->kref, ibmvfc_release_tgt);
2880 wake_up(&vhost->work_wait_q);
2881}
2882
2883/**
2884 * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
2885 * @tgt: ibmvfc target struct
2886 *
2887 **/
2888static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
2889{
2890 struct ibmvfc_implicit_logout *mad;
2891 struct ibmvfc_host *vhost = tgt->vhost;
2892 struct ibmvfc_event *evt;
2893
2894 if (vhost->discovery_threads >= disc_threads)
2895 return;
2896
2897 kref_get(&tgt->kref);
2898 evt = ibmvfc_get_event(vhost);
2899 vhost->discovery_threads++;
2900 ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
2901 evt->tgt = tgt;
2902 mad = &evt->iu.implicit_logout;
2903 memset(mad, 0, sizeof(*mad));
2904 mad->common.version = 1;
2905 mad->common.opcode = IBMVFC_IMPLICIT_LOGOUT;
2906 mad->common.length = sizeof(*mad);
2907 mad->old_scsi_id = tgt->scsi_id;
2908
2909 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
2910 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
2911 vhost->discovery_threads--;
2912 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2913 kref_put(&tgt->kref, ibmvfc_release_tgt);
2914 } else
2915 tgt_dbg(tgt, "Sent Implicit Logout\n");
2916}
2917
989b8545
BK
2918/**
2919 * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
2920 * @mad: ibmvfc passthru mad struct
2921 * @tgt: ibmvfc target struct
2922 *
2923 * Returns:
2924 * 1 if PLOGI needed / 0 if PLOGI not needed
2925 **/
2926static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
2927 struct ibmvfc_target *tgt)
2928{
2929 if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
2930 sizeof(tgt->ids.port_name)))
2931 return 1;
2932 if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
2933 sizeof(tgt->ids.node_name)))
2934 return 1;
2935 if (mad->fc_iu.response[6] != tgt->scsi_id)
2936 return 1;
2937 return 0;
2938}
2939
2940/**
2941 * ibmvfc_tgt_adisc_done - Completion handler for ADISC
2942 * @evt: ibmvfc event struct
2943 *
2944 **/
2945static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
2946{
2947 struct ibmvfc_target *tgt = evt->tgt;
2948 struct ibmvfc_host *vhost = evt->vhost;
2949 struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
2950 u32 status = mad->common.status;
2951 u8 fc_reason, fc_explain;
2952
2953 vhost->discovery_threads--;
2954 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
2955
2956 switch (status) {
2957 case IBMVFC_MAD_SUCCESS:
2958 tgt_dbg(tgt, "ADISC succeeded\n");
2959 if (ibmvfc_adisc_needs_plogi(mad, tgt))
2960 tgt->need_login = 1;
2961 break;
2962 case IBMVFC_MAD_DRIVER_FAILED:
2963 break;
2964 case IBMVFC_MAD_FAILED:
2965 default:
2966 tgt->need_login = 1;
2967 fc_reason = (mad->fc_iu.response[1] & 0x00ff0000) >> 16;
2968 fc_explain = (mad->fc_iu.response[1] & 0x0000ff00) >> 8;
2969 tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
2970 ibmvfc_get_cmd_error(mad->iu.status, mad->iu.error),
2971 mad->iu.status, mad->iu.error,
2972 ibmvfc_get_fc_type(fc_reason), fc_reason,
2973 ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
2974 break;
2975 };
2976
2977 kref_put(&tgt->kref, ibmvfc_release_tgt);
2978 ibmvfc_free_event(evt);
2979 wake_up(&vhost->work_wait_q);
2980}
2981
2982/**
2983 * ibmvfc_init_passthru - Initialize an event struct for FC passthru
2984 * @evt: ibmvfc event struct
2985 *
2986 **/
2987static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
2988{
2989 struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
2990
2991 memset(mad, 0, sizeof(*mad));
2992 mad->common.version = 1;
2993 mad->common.opcode = IBMVFC_PASSTHRU;
2994 mad->common.length = sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu);
2995 mad->cmd_ioba.va = (u64)evt->crq.ioba +
2996 offsetof(struct ibmvfc_passthru_mad, iu);
2997 mad->cmd_ioba.len = sizeof(mad->iu);
2998 mad->iu.cmd_len = sizeof(mad->fc_iu.payload);
2999 mad->iu.rsp_len = sizeof(mad->fc_iu.response);
3000 mad->iu.cmd.va = (u64)evt->crq.ioba +
3001 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3002 offsetof(struct ibmvfc_passthru_fc_iu, payload);
3003 mad->iu.cmd.len = sizeof(mad->fc_iu.payload);
3004 mad->iu.rsp.va = (u64)evt->crq.ioba +
3005 offsetof(struct ibmvfc_passthru_mad, fc_iu) +
3006 offsetof(struct ibmvfc_passthru_fc_iu, response);
3007 mad->iu.rsp.len = sizeof(mad->fc_iu.response);
3008}
3009
3010/**
3011 * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
3012 * @tgt: ibmvfc target struct
3013 *
3014 **/
3015static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
3016{
3017 struct ibmvfc_passthru_mad *mad;
3018 struct ibmvfc_host *vhost = tgt->vhost;
3019 struct ibmvfc_event *evt;
3020
3021 if (vhost->discovery_threads >= disc_threads)
3022 return;
3023
3024 kref_get(&tgt->kref);
3025 evt = ibmvfc_get_event(vhost);
3026 vhost->discovery_threads++;
3027 ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
3028 evt->tgt = tgt;
3029
3030 ibmvfc_init_passthru(evt);
3031 mad = &evt->iu.passthru;
3032 mad->iu.flags = IBMVFC_FC_ELS;
3033 mad->iu.scsi_id = tgt->scsi_id;
3034
3035 mad->fc_iu.payload[0] = IBMVFC_ADISC;
3036 memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
3037 sizeof(vhost->login_buf->resp.port_name));
3038 memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
3039 sizeof(vhost->login_buf->resp.node_name));
3040 mad->fc_iu.payload[6] = vhost->login_buf->resp.scsi_id & 0x00ffffff;
3041
3042 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3043 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3044 vhost->discovery_threads--;
3045 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3046 kref_put(&tgt->kref, ibmvfc_release_tgt);
3047 } else
3048 tgt_dbg(tgt, "Sent ADISC\n");
3049}
3050
072b91f9
BK
3051/**
3052 * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
3053 * @evt: ibmvfc event struct
3054 *
3055 **/
3056static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
3057{
3058 struct ibmvfc_target *tgt = evt->tgt;
3059 struct ibmvfc_host *vhost = evt->vhost;
3060 struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
3061 u32 status = rsp->common.status;
3062
3063 vhost->discovery_threads--;
3064 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3065 switch (status) {
3066 case IBMVFC_MAD_SUCCESS:
3067 tgt_dbg(tgt, "Query Target succeeded\n");
3068 tgt->new_scsi_id = rsp->scsi_id;
3069 if (rsp->scsi_id != tgt->scsi_id)
3070 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
989b8545
BK
3071 else
3072 ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
072b91f9
BK
3073 break;
3074 case IBMVFC_MAD_DRIVER_FAILED:
3075 break;
3076 case IBMVFC_MAD_CRQ_ERROR:
3077 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3078 break;
3079 case IBMVFC_MAD_FAILED:
3080 default:
3081 tgt_err(tgt, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
3082 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error,
3083 ibmvfc_get_fc_type(rsp->fc_type), rsp->fc_type,
3084 ibmvfc_get_gs_explain(rsp->fc_explain), rsp->fc_explain, status);
3085
3086 if ((rsp->status & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
3087 rsp->error == IBMVFC_UNABLE_TO_PERFORM_REQ &&
3088 rsp->fc_explain == IBMVFC_PORT_NAME_NOT_REG)
3089 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
3090 else if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3091 ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
3092 break;
3093 };
3094
3095 kref_put(&tgt->kref, ibmvfc_release_tgt);
3096 ibmvfc_free_event(evt);
3097 wake_up(&vhost->work_wait_q);
3098}
3099
3100/**
3101 * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
3102 * @tgt: ibmvfc target struct
3103 *
3104 **/
3105static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
3106{
3107 struct ibmvfc_query_tgt *query_tgt;
3108 struct ibmvfc_host *vhost = tgt->vhost;
3109 struct ibmvfc_event *evt;
3110
3111 if (vhost->discovery_threads >= disc_threads)
3112 return;
3113
3114 kref_get(&tgt->kref);
3115 evt = ibmvfc_get_event(vhost);
3116 vhost->discovery_threads++;
3117 evt->tgt = tgt;
3118 ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
3119 query_tgt = &evt->iu.query_tgt;
3120 memset(query_tgt, 0, sizeof(*query_tgt));
3121 query_tgt->common.version = 1;
3122 query_tgt->common.opcode = IBMVFC_QUERY_TARGET;
3123 query_tgt->common.length = sizeof(*query_tgt);
3124 query_tgt->wwpn = tgt->ids.port_name;
3125
3126 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
3127 if (ibmvfc_send_event(evt, vhost, default_timeout)) {
3128 vhost->discovery_threads--;
3129 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3130 kref_put(&tgt->kref, ibmvfc_release_tgt);
3131 } else
3132 tgt_dbg(tgt, "Sent Query Target\n");
3133}
3134
3135/**
3136 * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
3137 * @vhost: ibmvfc host struct
3138 * @scsi_id: SCSI ID to allocate target for
3139 *
3140 * Returns:
3141 * 0 on success / other on failure
3142 **/
3143static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
3144{
3145 struct ibmvfc_target *tgt;
3146 unsigned long flags;
3147
3148 spin_lock_irqsave(vhost->host->host_lock, flags);
3149 list_for_each_entry(tgt, &vhost->targets, queue) {
3150 if (tgt->scsi_id == scsi_id) {
3151 if (tgt->need_login)
3152 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3153 goto unlock_out;
3154 }
3155 }
3156 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3157
3158 tgt = mempool_alloc(vhost->tgt_pool, GFP_KERNEL);
3159 if (!tgt) {
3160 dev_err(vhost->dev, "Target allocation failure for scsi id %08lx\n",
3161 scsi_id);
3162 return -ENOMEM;
3163 }
3164
3165 tgt->scsi_id = scsi_id;
3166 tgt->new_scsi_id = scsi_id;
3167 tgt->vhost = vhost;
3168 tgt->need_login = 1;
3169 kref_init(&tgt->kref);
3170 ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
3171 spin_lock_irqsave(vhost->host->host_lock, flags);
3172 list_add_tail(&tgt->queue, &vhost->targets);
3173
3174unlock_out:
3175 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3176 return 0;
3177}
3178
3179/**
3180 * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
3181 * @vhost: ibmvfc host struct
3182 *
3183 * Returns:
3184 * 0 on success / other on failure
3185 **/
3186static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
3187{
3188 int i, rc;
3189
3190 for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
3191 rc = ibmvfc_alloc_target(vhost,
3192 vhost->disc_buf->scsi_id[i] & IBMVFC_DISC_TGT_SCSI_ID_MASK);
3193
3194 return rc;
3195}
3196
3197/**
3198 * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
3199 * @evt: ibmvfc event struct
3200 *
3201 **/
3202static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
3203{
3204 struct ibmvfc_host *vhost = evt->vhost;
3205 struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
3206 u32 mad_status = rsp->common.status;
3207
3208 switch (mad_status) {
3209 case IBMVFC_MAD_SUCCESS:
3210 ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
3211 vhost->num_targets = rsp->num_written;
3212 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
3213 break;
3214 case IBMVFC_MAD_FAILED:
3215 dev_err(vhost->dev, "Discover Targets failed: %s (%x:%x)\n",
3216 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3217 ibmvfc_retry_host_init(vhost);
3218 break;
3219 case IBMVFC_MAD_DRIVER_FAILED:
3220 break;
3221 default:
3222 dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
3223 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3224 break;
3225 }
3226
3227 ibmvfc_free_event(evt);
3228 wake_up(&vhost->work_wait_q);
3229}
3230
3231/**
3232 * ibmvfc_discover_targets - Send Discover Targets MAD
3233 * @vhost: ibmvfc host struct
3234 *
3235 **/
3236static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
3237{
3238 struct ibmvfc_discover_targets *mad;
3239 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3240
3241 ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
3242 mad = &evt->iu.discover_targets;
3243 memset(mad, 0, sizeof(*mad));
3244 mad->common.version = 1;
3245 mad->common.opcode = IBMVFC_DISC_TARGETS;
3246 mad->common.length = sizeof(*mad);
3247 mad->bufflen = vhost->disc_buf_sz;
3248 mad->buffer.va = vhost->disc_buf_dma;
3249 mad->buffer.len = vhost->disc_buf_sz;
3250 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3251
3252 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3253 ibmvfc_dbg(vhost, "Sent discover targets\n");
3254 else
3255 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3256}
3257
3258/**
3259 * ibmvfc_npiv_login_done - Completion handler for NPIV Login
3260 * @evt: ibmvfc event struct
3261 *
3262 **/
3263static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
3264{
3265 struct ibmvfc_host *vhost = evt->vhost;
3266 u32 mad_status = evt->xfer_iu->npiv_login.common.status;
3267 struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
3268 unsigned int npiv_max_sectors;
3269
3270 switch (mad_status) {
3271 case IBMVFC_MAD_SUCCESS:
3272 ibmvfc_free_event(evt);
3273 break;
3274 case IBMVFC_MAD_FAILED:
3275 dev_err(vhost->dev, "NPIV Login failed: %s (%x:%x)\n",
3276 ibmvfc_get_cmd_error(rsp->status, rsp->error), rsp->status, rsp->error);
3277 if (ibmvfc_retry_cmd(rsp->status, rsp->error))
3278 ibmvfc_retry_host_init(vhost);
3279 else
3280 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3281 ibmvfc_free_event(evt);
3282 return;
3283 case IBMVFC_MAD_CRQ_ERROR:
3284 ibmvfc_retry_host_init(vhost);
3285 case IBMVFC_MAD_DRIVER_FAILED:
3286 ibmvfc_free_event(evt);
3287 return;
3288 default:
3289 dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
3290 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3291 ibmvfc_free_event(evt);
3292 return;
3293 }
3294
3295 vhost->client_migrated = 0;
3296
3297 if (!(rsp->flags & IBMVFC_NATIVE_FC)) {
3298 dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
3299 rsp->flags);
3300 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3301 wake_up(&vhost->work_wait_q);
3302 return;
3303 }
3304
3305 if (rsp->max_cmds <= IBMVFC_NUM_INTERNAL_REQ) {
3306 dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
3307 rsp->max_cmds);
3308 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3309 wake_up(&vhost->work_wait_q);
3310 return;
3311 }
3312
3313 npiv_max_sectors = min((uint)(rsp->max_dma_len >> 9), IBMVFC_MAX_SECTORS);
3314 dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
3315 rsp->partition_name, rsp->device_name, rsp->port_loc_code,
3316 rsp->drc_name, npiv_max_sectors);
3317
3318 fc_host_fabric_name(vhost->host) = rsp->node_name;
3319 fc_host_node_name(vhost->host) = rsp->node_name;
3320 fc_host_port_name(vhost->host) = rsp->port_name;
3321 fc_host_port_id(vhost->host) = rsp->scsi_id;
3322 fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
3323 fc_host_supported_classes(vhost->host) = 0;
3324 if (rsp->service_parms.class1_parms[0] & 0x80000000)
3325 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
3326 if (rsp->service_parms.class2_parms[0] & 0x80000000)
3327 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
3328 if (rsp->service_parms.class3_parms[0] & 0x80000000)
3329 fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
3330 fc_host_maxframe_size(vhost->host) =
3331 rsp->service_parms.common.bb_rcv_sz & 0x0fff;
3332
3333 vhost->host->can_queue = rsp->max_cmds - IBMVFC_NUM_INTERNAL_REQ;
3334 vhost->host->max_sectors = npiv_max_sectors;
3335 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3336 wake_up(&vhost->work_wait_q);
3337}
3338
3339/**
3340 * ibmvfc_npiv_login - Sends NPIV login
3341 * @vhost: ibmvfc host struct
3342 *
3343 **/
3344static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
3345{
3346 struct ibmvfc_npiv_login_mad *mad;
3347 struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
3348
3349 ibmvfc_gather_partition_info(vhost);
3350 ibmvfc_set_login_info(vhost);
3351 ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
3352
3353 memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
3354 mad = &evt->iu.npiv_login;
3355 memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
3356 mad->common.version = 1;
3357 mad->common.opcode = IBMVFC_NPIV_LOGIN;
3358 mad->common.length = sizeof(struct ibmvfc_npiv_login_mad);
3359 mad->buffer.va = vhost->login_buf_dma;
3360 mad->buffer.len = sizeof(*vhost->login_buf);
3361
072b91f9
BK
3362 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
3363
3364 if (!ibmvfc_send_event(evt, vhost, default_timeout))
3365 ibmvfc_dbg(vhost, "Sent NPIV login\n");
3366 else
3367 ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
3368};
3369
3370/**
3371 * ibmvfc_dev_init_to_do - Is there target initialization work to do?
3372 * @vhost: ibmvfc host struct
3373 *
3374 * Returns:
3375 * 1 if work to do / 0 if not
3376 **/
3377static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
3378{
3379 struct ibmvfc_target *tgt;
3380
3381 list_for_each_entry(tgt, &vhost->targets, queue) {
3382 if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
3383 tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3384 return 1;
3385 }
3386
3387 return 0;
3388}
3389
3390/**
3391 * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
3392 * @vhost: ibmvfc host struct
3393 *
3394 * Returns:
3395 * 1 if work to do / 0 if not
3396 **/
3397static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3398{
3399 struct ibmvfc_target *tgt;
3400
3401 if (kthread_should_stop())
3402 return 1;
3403 switch (vhost->action) {
3404 case IBMVFC_HOST_ACTION_NONE:
3405 case IBMVFC_HOST_ACTION_INIT_WAIT:
3406 return 0;
3407 case IBMVFC_HOST_ACTION_TGT_INIT:
3408 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3409 if (vhost->discovery_threads == disc_threads)
3410 return 0;
3411 list_for_each_entry(tgt, &vhost->targets, queue)
3412 if (tgt->action == IBMVFC_TGT_ACTION_INIT)
3413 return 1;
3414 list_for_each_entry(tgt, &vhost->targets, queue)
3415 if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
3416 return 0;
3417 return 1;
3418 case IBMVFC_HOST_ACTION_INIT:
3419 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3420 case IBMVFC_HOST_ACTION_TGT_ADD:
3421 case IBMVFC_HOST_ACTION_TGT_DEL:
3422 case IBMVFC_HOST_ACTION_QUERY:
3423 default:
3424 break;
3425 };
3426
3427 return 1;
3428}
3429
3430/**
3431 * ibmvfc_work_to_do - Is there task level work to do?
3432 * @vhost: ibmvfc host struct
3433 *
3434 * Returns:
3435 * 1 if work to do / 0 if not
3436 **/
3437static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
3438{
3439 unsigned long flags;
3440 int rc;
3441
3442 spin_lock_irqsave(vhost->host->host_lock, flags);
3443 rc = __ibmvfc_work_to_do(vhost);
3444 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3445 return rc;
3446}
3447
3448/**
3449 * ibmvfc_log_ae - Log async events if necessary
3450 * @vhost: ibmvfc host struct
3451 * @events: events to log
3452 *
3453 **/
3454static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
3455{
3456 if (events & IBMVFC_AE_RSCN)
3457 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
3458 if ((events & IBMVFC_AE_LINKDOWN) &&
3459 vhost->state >= IBMVFC_HALTED)
3460 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
3461 if ((events & IBMVFC_AE_LINKUP) &&
3462 vhost->state == IBMVFC_INITIALIZING)
3463 fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
3464}
3465
3466/**
3467 * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
3468 * @tgt: ibmvfc target struct
3469 *
3470 **/
3471static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
3472{
3473 struct ibmvfc_host *vhost = tgt->vhost;
3474 struct fc_rport *rport;
3475 unsigned long flags;
3476
3477 tgt_dbg(tgt, "Adding rport\n");
3478 rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
3479 spin_lock_irqsave(vhost->host->host_lock, flags);
3480 tgt->rport = rport;
3481 ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
3482 if (rport) {
3483 tgt_dbg(tgt, "rport add succeeded\n");
3484 rport->maxframe_size = tgt->service_parms.common.bb_rcv_sz & 0x0fff;
3485 rport->supported_classes = 0;
52d7e861 3486 tgt->target_id = rport->scsi_target_id;
072b91f9
BK
3487 if (tgt->service_parms.class1_parms[0] & 0x80000000)
3488 rport->supported_classes |= FC_COS_CLASS1;
3489 if (tgt->service_parms.class2_parms[0] & 0x80000000)
3490 rport->supported_classes |= FC_COS_CLASS2;
3491 if (tgt->service_parms.class3_parms[0] & 0x80000000)
3492 rport->supported_classes |= FC_COS_CLASS3;
3493 } else
3494 tgt_dbg(tgt, "rport add failed\n");
3495 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3496}
3497
3498/**
3499 * ibmvfc_do_work - Do task level work
3500 * @vhost: ibmvfc host struct
3501 *
3502 **/
3503static void ibmvfc_do_work(struct ibmvfc_host *vhost)
3504{
3505 struct ibmvfc_target *tgt;
3506 unsigned long flags;
3507 struct fc_rport *rport;
3508
3509 ibmvfc_log_ae(vhost, vhost->events_to_log);
3510 spin_lock_irqsave(vhost->host->host_lock, flags);
3511 vhost->events_to_log = 0;
3512 switch (vhost->action) {
3513 case IBMVFC_HOST_ACTION_NONE:
3514 case IBMVFC_HOST_ACTION_INIT_WAIT:
3515 break;
3516 case IBMVFC_HOST_ACTION_INIT:
3517 BUG_ON(vhost->state != IBMVFC_INITIALIZING);
3518 vhost->job_step(vhost);
3519 break;
3520 case IBMVFC_HOST_ACTION_QUERY:
3521 list_for_each_entry(tgt, &vhost->targets, queue)
3522 ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
3523 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
3524 break;
3525 case IBMVFC_HOST_ACTION_QUERY_TGTS:
3526 list_for_each_entry(tgt, &vhost->targets, queue) {
3527 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3528 tgt->job_step(tgt);
3529 break;
3530 }
3531 }
3532
3533 if (!ibmvfc_dev_init_to_do(vhost))
3534 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
3535 break;
3536 case IBMVFC_HOST_ACTION_TGT_DEL:
3537 list_for_each_entry(tgt, &vhost->targets, queue) {
3538 if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3539 tgt_dbg(tgt, "Deleting rport\n");
3540 rport = tgt->rport;
3541 tgt->rport = NULL;
3542 list_del(&tgt->queue);
3543 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3544 if (rport)
3545 fc_remote_port_delete(rport);
3546 kref_put(&tgt->kref, ibmvfc_release_tgt);
3547 return;
3548 }
3549 }
3550
3551 if (vhost->state == IBMVFC_INITIALIZING) {
3552 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
3553 vhost->job_step = ibmvfc_discover_targets;
3554 } else {
3555 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3556 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3557 scsi_unblock_requests(vhost->host);
3558 wake_up(&vhost->init_wait_q);
3559 return;
3560 }
3561 break;
3562 case IBMVFC_HOST_ACTION_ALLOC_TGTS:
3563 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
3564 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3565 ibmvfc_alloc_targets(vhost);
3566 spin_lock_irqsave(vhost->host->host_lock, flags);
3567 break;
3568 case IBMVFC_HOST_ACTION_TGT_INIT:
3569 list_for_each_entry(tgt, &vhost->targets, queue) {
3570 if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
3571 tgt->job_step(tgt);
3572 break;
3573 }
3574 }
3575
3576 if (!ibmvfc_dev_init_to_do(vhost)) {
3577 ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
3578 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_ADD);
3579 vhost->init_retries = 0;
3580 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3581 scsi_unblock_requests(vhost->host);
3582 return;
3583 }
3584 break;
3585 case IBMVFC_HOST_ACTION_TGT_ADD:
3586 list_for_each_entry(tgt, &vhost->targets, queue) {
3587 if (tgt->action == IBMVFC_TGT_ACTION_ADD_RPORT) {
3588 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3589 ibmvfc_tgt_add_rport(tgt);
3590 return;
3591 } else if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
3592 tgt_dbg(tgt, "Deleting rport\n");
3593 rport = tgt->rport;
3594 tgt->rport = NULL;
3595 list_del(&tgt->queue);
3596 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3597 if (rport)
3598 fc_remote_port_delete(rport);
3599 kref_put(&tgt->kref, ibmvfc_release_tgt);
3600 return;
3601 }
3602 }
3603
915be024 3604 if (vhost->reinit && !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
072b91f9 3605 vhost->reinit = 0;
915be024 3606 scsi_block_requests(vhost->host);
072b91f9
BK
3607 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
3608 } else {
3609 ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
3610 wake_up(&vhost->init_wait_q);
3611 }
3612 break;
3613 default:
3614 break;
3615 };
3616
3617 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3618}
3619
3620/**
3621 * ibmvfc_work - Do task level work
3622 * @data: ibmvfc host struct
3623 *
3624 * Returns:
3625 * zero
3626 **/
3627static int ibmvfc_work(void *data)
3628{
3629 struct ibmvfc_host *vhost = data;
3630 int rc;
3631
3632 set_user_nice(current, -20);
3633
3634 while (1) {
3635 rc = wait_event_interruptible(vhost->work_wait_q,
3636 ibmvfc_work_to_do(vhost));
3637
3638 BUG_ON(rc);
3639
3640 if (kthread_should_stop())
3641 break;
3642
3643 ibmvfc_do_work(vhost);
3644 }
3645
3646 ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
3647 return 0;
3648}
3649
3650/**
3651 * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
3652 * @vhost: ibmvfc host struct
3653 *
3654 * Allocates a page for messages, maps it for dma, and registers
3655 * the crq with the hypervisor.
3656 *
3657 * Return value:
3658 * zero on success / other on failure
3659 **/
3660static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
3661{
3662 int rc, retrc = -ENOMEM;
3663 struct device *dev = vhost->dev;
3664 struct vio_dev *vdev = to_vio_dev(dev);
3665 struct ibmvfc_crq_queue *crq = &vhost->crq;
3666
3667 ENTER;
3668 crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
3669
3670 if (!crq->msgs)
3671 return -ENOMEM;
3672
3673 crq->size = PAGE_SIZE / sizeof(*crq->msgs);
3674 crq->msg_token = dma_map_single(dev, crq->msgs,
3675 PAGE_SIZE, DMA_BIDIRECTIONAL);
3676
8d8bb39b 3677 if (dma_mapping_error(dev, crq->msg_token))
072b91f9
BK
3678 goto map_failed;
3679
3680 retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
3681 crq->msg_token, PAGE_SIZE);
3682
3683 if (rc == H_RESOURCE)
3684 /* maybe kexecing and resource is busy. try a reset */
3685 retrc = rc = ibmvfc_reset_crq(vhost);
3686
3687 if (rc == H_CLOSED)
3688 dev_warn(dev, "Partner adapter not ready\n");
3689 else if (rc) {
3690 dev_warn(dev, "Error %d opening adapter\n", rc);
3691 goto reg_crq_failed;
3692 }
3693
3694 retrc = 0;
3695
3696 if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
3697 dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
3698 goto req_irq_failed;
3699 }
3700
3701 if ((rc = vio_enable_interrupts(vdev))) {
3702 dev_err(dev, "Error %d enabling interrupts\n", rc);
3703 goto req_irq_failed;
3704 }
3705
3706 crq->cur = 0;
3707 LEAVE;
3708 return retrc;
3709
3710req_irq_failed:
3711 do {
3712 rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
3713 } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
3714reg_crq_failed:
3715 dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
3716map_failed:
3717 free_page((unsigned long)crq->msgs);
3718 return retrc;
3719}
3720
3721/**
3722 * ibmvfc_free_mem - Free memory for vhost
3723 * @vhost: ibmvfc host struct
3724 *
3725 * Return value:
3726 * none
3727 **/
3728static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
3729{
3730 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3731
3732 ENTER;
3733 mempool_destroy(vhost->tgt_pool);
3734 kfree(vhost->trace);
3735 dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
3736 vhost->disc_buf_dma);
3737 dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
3738 vhost->login_buf, vhost->login_buf_dma);
3739 dma_pool_destroy(vhost->sg_pool);
3740 dma_unmap_single(vhost->dev, async_q->msg_token,
3741 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3742 free_page((unsigned long)async_q->msgs);
3743 LEAVE;
3744}
3745
3746/**
3747 * ibmvfc_alloc_mem - Allocate memory for vhost
3748 * @vhost: ibmvfc host struct
3749 *
3750 * Return value:
3751 * 0 on success / non-zero on failure
3752 **/
3753static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
3754{
3755 struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
3756 struct device *dev = vhost->dev;
3757
3758 ENTER;
3759 async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
3760 if (!async_q->msgs) {
3761 dev_err(dev, "Couldn't allocate async queue.\n");
3762 goto nomem;
3763 }
3764
3765 async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
3766 async_q->msg_token = dma_map_single(dev, async_q->msgs,
3767 async_q->size * sizeof(*async_q->msgs),
3768 DMA_BIDIRECTIONAL);
3769
8d8bb39b 3770 if (dma_mapping_error(dev, async_q->msg_token)) {
072b91f9
BK
3771 dev_err(dev, "Failed to map async queue\n");
3772 goto free_async_crq;
3773 }
3774
3775 vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
3776 SG_ALL * sizeof(struct srp_direct_buf),
3777 sizeof(struct srp_direct_buf), 0);
3778
3779 if (!vhost->sg_pool) {
3780 dev_err(dev, "Failed to allocate sg pool\n");
3781 goto unmap_async_crq;
3782 }
3783
3784 vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
3785 &vhost->login_buf_dma, GFP_KERNEL);
3786
3787 if (!vhost->login_buf) {
3788 dev_err(dev, "Couldn't allocate NPIV login buffer\n");
3789 goto free_sg_pool;
3790 }
3791
3792 vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
3793 vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
3794 &vhost->disc_buf_dma, GFP_KERNEL);
3795
3796 if (!vhost->disc_buf) {
3797 dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
3798 goto free_login_buffer;
3799 }
3800
3801 vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
3802 sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
3803
3804 if (!vhost->trace)
3805 goto free_disc_buffer;
3806
3807 vhost->tgt_pool = mempool_create_kzalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
3808 sizeof(struct ibmvfc_target));
3809
3810 if (!vhost->tgt_pool) {
3811 dev_err(dev, "Couldn't allocate target memory pool\n");
3812 goto free_trace;
3813 }
3814
3815 LEAVE;
3816 return 0;
3817
3818free_trace:
3819 kfree(vhost->trace);
3820free_disc_buffer:
3821 dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
3822 vhost->disc_buf_dma);
3823free_login_buffer:
3824 dma_free_coherent(dev, sizeof(*vhost->login_buf),
3825 vhost->login_buf, vhost->login_buf_dma);
3826free_sg_pool:
3827 dma_pool_destroy(vhost->sg_pool);
3828unmap_async_crq:
3829 dma_unmap_single(dev, async_q->msg_token,
3830 async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
3831free_async_crq:
3832 free_page((unsigned long)async_q->msgs);
3833nomem:
3834 LEAVE;
3835 return -ENOMEM;
3836}
3837
3838/**
3839 * ibmvfc_probe - Adapter hot plug add entry point
3840 * @vdev: vio device struct
3841 * @id: vio device id struct
3842 *
3843 * Return value:
3844 * 0 on success / non-zero on failure
3845 **/
3846static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
3847{
3848 struct ibmvfc_host *vhost;
3849 struct Scsi_Host *shost;
3850 struct device *dev = &vdev->dev;
3851 int rc = -ENOMEM;
3852
3853 ENTER;
3854 shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
3855 if (!shost) {
3856 dev_err(dev, "Couldn't allocate host data\n");
3857 goto out;
3858 }
3859
3860 shost->transportt = ibmvfc_transport_template;
3861 shost->can_queue = max_requests;
3862 shost->max_lun = max_lun;
3863 shost->max_id = max_targets;
3864 shost->max_sectors = IBMVFC_MAX_SECTORS;
3865 shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
3866 shost->unique_id = shost->host_no;
3867
3868 vhost = shost_priv(shost);
3869 INIT_LIST_HEAD(&vhost->sent);
3870 INIT_LIST_HEAD(&vhost->free);
3871 INIT_LIST_HEAD(&vhost->targets);
3872 sprintf(vhost->name, IBMVFC_NAME);
3873 vhost->host = shost;
3874 vhost->dev = dev;
3875 vhost->partition_number = -1;
3876 vhost->log_level = log_level;
3877 strcpy(vhost->partition_name, "UNKNOWN");
3878 init_waitqueue_head(&vhost->work_wait_q);
3879 init_waitqueue_head(&vhost->init_wait_q);
3880
3881 if ((rc = ibmvfc_alloc_mem(vhost)))
3882 goto free_scsi_host;
3883
3884 vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
3885 shost->host_no);
3886
3887 if (IS_ERR(vhost->work_thread)) {
3888 dev_err(dev, "Couldn't create kernel thread: %ld\n",
3889 PTR_ERR(vhost->work_thread));
3890 goto free_host_mem;
3891 }
3892
3893 if ((rc = ibmvfc_init_crq(vhost))) {
3894 dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
3895 goto kill_kthread;
3896 }
3897
3898 if ((rc = ibmvfc_init_event_pool(vhost))) {
3899 dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
3900 goto release_crq;
3901 }
3902
3903 if ((rc = scsi_add_host(shost, dev)))
3904 goto release_event_pool;
3905
3906 if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
3907 &ibmvfc_trace_attr))) {
3908 dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
3909 goto remove_shost;
3910 }
3911
3912 dev_set_drvdata(dev, vhost);
3913 spin_lock(&ibmvfc_driver_lock);
3914 list_add_tail(&vhost->queue, &ibmvfc_head);
3915 spin_unlock(&ibmvfc_driver_lock);
3916
3917 ibmvfc_send_crq_init(vhost);
3918 scsi_scan_host(shost);
3919 return 0;
3920
3921remove_shost:
3922 scsi_remove_host(shost);
3923release_event_pool:
3924 ibmvfc_free_event_pool(vhost);
3925release_crq:
3926 ibmvfc_release_crq_queue(vhost);
3927kill_kthread:
3928 kthread_stop(vhost->work_thread);
3929free_host_mem:
3930 ibmvfc_free_mem(vhost);
3931free_scsi_host:
3932 scsi_host_put(shost);
3933out:
3934 LEAVE;
3935 return rc;
3936}
3937
3938/**
3939 * ibmvfc_remove - Adapter hot plug remove entry point
3940 * @vdev: vio device struct
3941 *
3942 * Return value:
3943 * 0
3944 **/
3945static int ibmvfc_remove(struct vio_dev *vdev)
3946{
3947 struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
3948 unsigned long flags;
3949
3950 ENTER;
3951 ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
2d0da2a4
BK
3952 ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
3953 ibmvfc_wait_while_resetting(vhost);
3954 ibmvfc_release_crq_queue(vhost);
072b91f9
BK
3955 kthread_stop(vhost->work_thread);
3956 fc_remove_host(vhost->host);
3957 scsi_remove_host(vhost->host);
072b91f9
BK
3958
3959 spin_lock_irqsave(vhost->host->host_lock, flags);
3960 ibmvfc_purge_requests(vhost, DID_ERROR);
3961 ibmvfc_free_event_pool(vhost);
3962 spin_unlock_irqrestore(vhost->host->host_lock, flags);
3963
3964 ibmvfc_free_mem(vhost);
3965 spin_lock(&ibmvfc_driver_lock);
3966 list_del(&vhost->queue);
3967 spin_unlock(&ibmvfc_driver_lock);
3968 scsi_host_put(vhost->host);
3969 LEAVE;
3970 return 0;
3971}
3972
39c1ffec
BK
3973/**
3974 * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
3975 * @vdev: vio device struct
3976 *
3977 * Return value:
3978 * Number of bytes the driver will need to DMA map at the same time in
3979 * order to perform well.
3980 */
3981static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
3982{
3983 unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
3984 return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
3985}
3986
072b91f9
BK
3987static struct vio_device_id ibmvfc_device_table[] __devinitdata = {
3988 {"fcp", "IBM,vfc-client"},
3989 { "", "" }
3990};
3991MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
3992
3993static struct vio_driver ibmvfc_driver = {
3994 .id_table = ibmvfc_device_table,
3995 .probe = ibmvfc_probe,
3996 .remove = ibmvfc_remove,
39c1ffec 3997 .get_desired_dma = ibmvfc_get_desired_dma,
072b91f9
BK
3998 .driver = {
3999 .name = IBMVFC_NAME,
4000 .owner = THIS_MODULE,
4001 }
4002};
4003
4004static struct fc_function_template ibmvfc_transport_functions = {
4005 .show_host_fabric_name = 1,
4006 .show_host_node_name = 1,
4007 .show_host_port_name = 1,
4008 .show_host_supported_classes = 1,
4009 .show_host_port_type = 1,
4010 .show_host_port_id = 1,
4011
4012 .get_host_port_state = ibmvfc_get_host_port_state,
4013 .show_host_port_state = 1,
4014
4015 .get_host_speed = ibmvfc_get_host_speed,
4016 .show_host_speed = 1,
4017
4018 .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
4019 .terminate_rport_io = ibmvfc_terminate_rport_io,
4020
4021 .show_rport_maxframe_size = 1,
4022 .show_rport_supported_classes = 1,
4023
4024 .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
4025 .show_rport_dev_loss_tmo = 1,
4026
4027 .get_starget_node_name = ibmvfc_get_starget_node_name,
4028 .show_starget_node_name = 1,
4029
4030 .get_starget_port_name = ibmvfc_get_starget_port_name,
4031 .show_starget_port_name = 1,
4032
4033 .get_starget_port_id = ibmvfc_get_starget_port_id,
4034 .show_starget_port_id = 1,
4035};
4036
4037/**
4038 * ibmvfc_module_init - Initialize the ibmvfc module
4039 *
4040 * Return value:
4041 * 0 on success / other on failure
4042 **/
4043static int __init ibmvfc_module_init(void)
4044{
4045 int rc;
4046
4047 if (!firmware_has_feature(FW_FEATURE_VIO))
4048 return -ENODEV;
4049
4050 printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
4051 IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
4052
4053 ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
4054 if (!ibmvfc_transport_template)
4055 return -ENOMEM;
4056
4057 rc = vio_register_driver(&ibmvfc_driver);
4058 if (rc)
4059 fc_release_transport(ibmvfc_transport_template);
4060 return rc;
4061}
4062
4063/**
4064 * ibmvfc_module_exit - Teardown the ibmvfc module
4065 *
4066 * Return value:
4067 * nothing
4068 **/
4069static void __exit ibmvfc_module_exit(void)
4070{
4071 vio_unregister_driver(&ibmvfc_driver);
4072 fc_release_transport(ibmvfc_transport_template);
4073}
4074
4075module_init(ibmvfc_module_init);
4076module_exit(ibmvfc_module_exit);