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