]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ocs_fc/ocs_cam.c
Import mandoc 1.14.4
[FreeBSD/FreeBSD.git] / sys / dev / ocs_fc / ocs_cam.c
1 /*-
2  * Copyright (c) 2017 Broadcom. All rights reserved.
3  * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  *    this list of conditions and the following disclaimer in the documentation
13  *    and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33
34 /**
35  * @defgroup scsi_api_target SCSI Target API
36  * @defgroup scsi_api_initiator SCSI Initiator API
37  * @defgroup cam_api Common Access Method (CAM) API
38  * @defgroup cam_io CAM IO
39  */
40
41 /**
42  * @file
43  * Provides CAM functionality.
44  */
45
46 #include "ocs.h"
47 #include "ocs_scsi.h"
48 #include "ocs_device.h"
49
50 /* Default IO timeout value for initiators is 30 seconds */
51 #define OCS_CAM_IO_TIMEOUT      30
52
53 typedef struct {
54         ocs_scsi_sgl_t *sgl;
55         uint32_t sgl_max;
56         uint32_t sgl_count;
57         int32_t rc;
58 } ocs_dmamap_load_arg_t;
59
60 static void ocs_action(struct cam_sim *, union ccb *);
61 static void ocs_poll(struct cam_sim *);
62
63 static ocs_tgt_resource_t *ocs_tgt_resource_get(ocs_fcport *,
64                                         struct ccb_hdr *, uint32_t *);
65 static int32_t ocs_tgt_resource_abort(struct ocs_softc *, ocs_tgt_resource_t *);
66 static uint32_t ocs_abort_initiator_io(struct ocs_softc *ocs, union ccb *accb);
67 static void ocs_abort_inot(struct ocs_softc *ocs, union ccb *ccb);
68 static void ocs_abort_atio(struct ocs_softc *ocs, union ccb *ccb);
69 static int32_t ocs_target_tmf_cb(ocs_io_t *, ocs_scsi_io_status_e, uint32_t, void *);
70 static int32_t ocs_io_abort_cb(ocs_io_t *, ocs_scsi_io_status_e, uint32_t, void *);
71 static int32_t ocs_task_set_full_or_busy(ocs_io_t *io);
72 static int32_t ocs_initiator_tmf_cb(ocs_io_t *, ocs_scsi_io_status_e,
73                 ocs_scsi_cmd_resp_t *, uint32_t, void *);
74 static uint32_t
75 ocs_fcp_change_role(struct ocs_softc *ocs, ocs_fcport *fcp, uint32_t new_role);
76
77 static void ocs_ldt(void *arg);
78 static void ocs_ldt_task(void *arg, int pending);
79 static void ocs_delete_target(ocs_t *ocs, ocs_fcport *fcp, int tgt);
80 uint32_t ocs_add_new_tgt(ocs_node_t *node, ocs_fcport *fcp);
81 uint32_t ocs_update_tgt(ocs_node_t *node, ocs_fcport *fcp, uint32_t tgt_id);
82
83 int32_t ocs_tgt_find(ocs_fcport *fcp, ocs_node_t *node);
84
85 static inline ocs_io_t *ocs_scsi_find_io(struct ocs_softc *ocs, uint32_t tag)
86 {
87
88         return ocs_io_get_instance(ocs, tag);
89 }
90
91 static inline void ocs_target_io_free(ocs_io_t *io)
92 {
93         io->tgt_io.state = OCS_CAM_IO_FREE;
94         io->tgt_io.flags = 0;
95         io->tgt_io.app = NULL;
96         ocs_scsi_io_complete(io);
97         if(io->ocs->io_in_use != 0)
98                 atomic_subtract_acq_32(&io->ocs->io_in_use, 1);
99 }
100
101 static int32_t
102 ocs_attach_port(ocs_t *ocs, int chan)
103 {
104
105         struct cam_sim  *sim = NULL;
106         struct cam_path *path = NULL;
107         uint32_t        max_io = ocs_scsi_get_property(ocs, OCS_SCSI_MAX_IOS);
108         ocs_fcport *fcp = FCPORT(ocs, chan);
109
110         if (NULL == (sim = cam_sim_alloc(ocs_action, ocs_poll, 
111                                 device_get_name(ocs->dev), ocs, 
112                                 device_get_unit(ocs->dev), &ocs->sim_lock,
113                                 max_io, max_io, ocs->devq))) {
114                 device_printf(ocs->dev, "Can't allocate SIM\n");
115                 return 1;
116         }
117
118         mtx_lock(&ocs->sim_lock);
119         if (CAM_SUCCESS != xpt_bus_register(sim, ocs->dev, chan)) {
120                 device_printf(ocs->dev, "Can't register bus %d\n", 0);
121                 mtx_unlock(&ocs->sim_lock);
122                 cam_sim_free(sim, FALSE);
123                 return 1;
124         }
125         mtx_unlock(&ocs->sim_lock);
126
127         if (CAM_REQ_CMP != xpt_create_path(&path, NULL, cam_sim_path(sim),
128                                 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)) {
129                 device_printf(ocs->dev, "Can't create path\n");
130                 xpt_bus_deregister(cam_sim_path(sim));
131                 mtx_unlock(&ocs->sim_lock);
132                 cam_sim_free(sim, FALSE);
133                 return 1;
134         }
135         
136         fcp->ocs = ocs;
137         fcp->sim  = sim;
138         fcp->path = path;
139
140         callout_init_mtx(&fcp->ldt, &ocs->sim_lock, 0);
141         TASK_INIT(&fcp->ltask, 1, ocs_ldt_task, fcp);
142
143         return 0;
144 }
145
146 static int32_t
147 ocs_detach_port(ocs_t *ocs, int32_t chan)
148 {
149         ocs_fcport *fcp = NULL;
150         struct cam_sim  *sim = NULL;
151         struct cam_path *path = NULL;
152         fcp = FCPORT(ocs, chan);
153
154         sim = fcp->sim;
155         path = fcp->path;
156
157         callout_drain(&fcp->ldt);
158         ocs_ldt_task(fcp, 0);   
159
160         if (fcp->sim) {
161                 mtx_lock(&ocs->sim_lock);
162                         ocs_tgt_resource_abort(ocs, &fcp->targ_rsrc_wildcard);
163                         if (path) {
164                                 xpt_async(AC_LOST_DEVICE, path, NULL);
165                                 xpt_free_path(path);
166                                 fcp->path = NULL;
167                         }
168                         xpt_bus_deregister(cam_sim_path(sim));
169
170                         cam_sim_free(sim, FALSE);
171                         fcp->sim = NULL;
172                 mtx_unlock(&ocs->sim_lock);
173         }
174         
175         return 0;
176 }
177
178 int32_t
179 ocs_cam_attach(ocs_t *ocs)
180 {
181         struct cam_devq *devq = NULL;
182         int     i = 0;
183         uint32_t        max_io = ocs_scsi_get_property(ocs, OCS_SCSI_MAX_IOS);
184
185         if (NULL == (devq = cam_simq_alloc(max_io))) {
186                 device_printf(ocs->dev, "Can't allocate SIMQ\n");
187                 return -1;
188         }
189
190         ocs->devq = devq;
191
192         if (mtx_initialized(&ocs->sim_lock) == 0) {
193                 mtx_init(&ocs->sim_lock, "ocs_sim_lock", NULL, MTX_DEF);
194         }
195
196         for (i = 0; i < (ocs->num_vports + 1); i++) {
197                 if (ocs_attach_port(ocs, i)) {
198                         ocs_log_err(ocs, "Attach port failed for chan: %d\n", i);
199                         goto detach_port;
200                 }
201         }
202         
203         ocs->io_high_watermark = max_io;
204         ocs->io_in_use = 0;
205         return 0;
206
207 detach_port:
208         while (--i >= 0) {
209                 ocs_detach_port(ocs, i);
210         }
211
212         cam_simq_free(ocs->devq);
213
214         if (mtx_initialized(&ocs->sim_lock))
215                 mtx_destroy(&ocs->sim_lock);
216
217         return 1;       
218 }
219
220 int32_t
221 ocs_cam_detach(ocs_t *ocs)
222 {
223         int i = 0;
224
225         for (i = (ocs->num_vports); i >= 0; i--) {
226                 ocs_detach_port(ocs, i);
227         }
228
229         cam_simq_free(ocs->devq);
230
231         if (mtx_initialized(&ocs->sim_lock))
232                 mtx_destroy(&ocs->sim_lock);
233
234         return 0;
235 }
236
237 /***************************************************************************
238  * Functions required by SCSI base driver API
239  */
240
241 /**
242  * @ingroup scsi_api_target
243  * @brief Attach driver to the BSD SCSI layer (a.k.a CAM)
244  *
245  * Allocates + initializes CAM related resources and attaches to the CAM
246  *
247  * @param ocs the driver instance's software context
248  *
249  * @return 0 on success, non-zero otherwise
250  */
251 int32_t
252 ocs_scsi_tgt_new_device(ocs_t *ocs)
253 {
254         ocs->enable_task_set_full = ocs_scsi_get_property(ocs, 
255                                         OCS_SCSI_ENABLE_TASK_SET_FULL);
256         ocs_log_debug(ocs, "task set full processing is %s\n",
257                 ocs->enable_task_set_full ? "enabled" : "disabled");
258
259         return 0;
260 }
261
262 /**
263  * @ingroup scsi_api_target
264  * @brief Tears down target members of ocs structure.
265  *
266  * Called by OS code when device is removed.
267  *
268  * @param ocs pointer to ocs
269  *
270  * @return returns 0 for success, a negative error code value for failure.
271  */
272 int32_t
273 ocs_scsi_tgt_del_device(ocs_t *ocs)
274 {
275
276         return 0;
277 }
278
279 /**
280  * @ingroup scsi_api_target
281  * @brief accept new domain notification
282  *
283  * Called by base drive when new domain is discovered.  A target-server
284  * will use this call to prepare for new remote node notifications
285  * arising from ocs_scsi_new_initiator().
286  *
287  * The domain context has an element <b>ocs_scsi_tgt_domain_t tgt_domain</b> 
288  * which is declared by the target-server code and is used for target-server 
289  * private data.
290  *
291  * This function will only be called if the base-driver has been enabled for 
292  * target capability.
293  *
294  * Note that this call is made to target-server backends, 
295  * the ocs_scsi_ini_new_domain() function is called to initiator-client backends.
296  *
297  * @param domain pointer to domain
298  *
299  * @return returns 0 for success, a negative error code value for failure.
300  */
301 int32_t
302 ocs_scsi_tgt_new_domain(ocs_domain_t *domain)
303 {
304         return 0;
305 }
306
307 /**
308  * @ingroup scsi_api_target
309  * @brief accept domain lost notification
310  *
311  * Called by base-driver when a domain goes away.  A target-server will
312  * use this call to clean up all domain scoped resources.
313  *
314  * Note that this call is made to target-server backends,
315  * the ocs_scsi_ini_del_domain() function is called to initiator-client backends.
316  *
317  * @param domain pointer to domain
318  *
319  * @return returns 0 for success, a negative error code value for failure.
320  */
321 void
322 ocs_scsi_tgt_del_domain(ocs_domain_t *domain)
323 {
324 }
325
326
327 /**
328  * @ingroup scsi_api_target
329  * @brief accept new sli port (sport) notification
330  *
331  * Called by base drive when new sport is discovered.  A target-server
332  * will use this call to prepare for new remote node notifications
333  * arising from ocs_scsi_new_initiator().
334  *
335  * The domain context has an element <b>ocs_scsi_tgt_sport_t tgt_sport</b> 
336  * which is declared by the target-server code and is used for
337  * target-server private data.
338  *
339  * This function will only be called if the base-driver has been enabled for 
340  * target capability.
341  *
342  * Note that this call is made to target-server backends,
343  * the ocs_scsi_tgt_new_domain() is called to initiator-client backends.
344  *
345  * @param sport pointer to SLI port
346  *
347  * @return returns 0 for success, a negative error code value for failure.
348  */
349 int32_t
350 ocs_scsi_tgt_new_sport(ocs_sport_t *sport)
351 {
352         ocs_t *ocs = sport->ocs;
353
354         if(!sport->is_vport) {
355                 sport->tgt_data = FCPORT(ocs, 0);
356         }
357
358         return 0;
359 }
360
361 /**
362  * @ingroup scsi_api_target
363  * @brief accept SLI port gone notification
364  *
365  * Called by base-driver when a sport goes away.  A target-server will
366  * use this call to clean up all sport scoped resources.
367  *
368  * Note that this call is made to target-server backends,
369  * the ocs_scsi_ini_del_sport() is called to initiator-client backends.
370  *
371  * @param sport pointer to SLI port
372  *
373  * @return returns 0 for success, a negative error code value for failure.
374  */
375 void
376 ocs_scsi_tgt_del_sport(ocs_sport_t *sport)
377 {
378         return;
379 }
380
381 /**
382  * @ingroup scsi_api_target
383  * @brief receive notification of a new SCSI initiator node
384  *
385  * Sent by base driver to notify a target-server of the presense of a new
386  * remote initiator.   The target-server may use this call to prepare for
387  * inbound IO from this node.
388  *
389  * The ocs_node_t structure has and elment of type ocs_scsi_tgt_node_t named
390  * tgt_node that is declared and used by a target-server for private
391  * information.
392  *
393  * This function is only called if the target capability is enabled in driver.
394  *
395  * @param node pointer to new remote initiator node
396  *
397  * @return returns 0 for success, a negative error code value for failure.
398  *
399  * @note
400  */
401 int32_t
402 ocs_scsi_new_initiator(ocs_node_t *node)
403 {
404         ocs_t   *ocs = node->ocs;
405         struct ac_contract ac;
406         struct ac_device_changed *adc;
407
408         ocs_fcport      *fcp = NULL;
409
410         fcp = node->sport->tgt_data;
411         if (fcp == NULL) {
412                 ocs_log_err(ocs, "FCP is NULL \n");
413                 return 1;
414         }       
415
416         /*
417          * Update the IO watermark by decrementing it by the
418          * number of IOs reserved for each initiator.
419          */
420         atomic_subtract_acq_32(&ocs->io_high_watermark, OCS_RSVD_INI_IO);
421
422         ac.contract_number = AC_CONTRACT_DEV_CHG;
423         adc = (struct ac_device_changed *) ac.contract_data;
424         adc->wwpn = ocs_node_get_wwpn(node);
425         adc->port = node->rnode.fc_id;
426         adc->target = node->instance_index;
427         adc->arrived = 1;
428         xpt_async(AC_CONTRACT, fcp->path, &ac);
429
430         return 0;
431 }
432
433 /**
434  * @ingroup scsi_api_target
435  * @brief validate new initiator
436  *
437  * Sent by base driver to validate a remote initiatiator.   The target-server
438  * returns TRUE if this initiator should be accepted.
439  *
440  * This function is only called if the target capability is enabled in driver.
441  *
442  * @param node pointer to remote initiator node to validate
443  *
444  * @return TRUE if initiator should be accepted, FALSE if it should be rejected
445  *
446  * @note
447  */
448
449 int32_t
450 ocs_scsi_validate_initiator(ocs_node_t *node)
451 {
452         return 1;
453 }
454
455 /**
456  * @ingroup scsi_api_target
457  * @brief Delete a SCSI initiator node
458  *
459  * Sent by base driver to notify a target-server that a remote initiator
460  * is now gone. The base driver will have terminated all outstanding IOs 
461  * and the target-server will receive appropriate completions.
462  *
463  * This function is only called if the base driver is enabled for
464  * target capability.
465  *
466  * @param node pointer node being deleted
467  * @param reason Reason why initiator is gone.
468  *
469  * @return OCS_SCSI_CALL_COMPLETE to indicate that all work was completed
470  *
471  * @note
472  */
473 int32_t
474 ocs_scsi_del_initiator(ocs_node_t *node, ocs_scsi_del_initiator_reason_e reason)
475 {
476         ocs_t   *ocs = node->ocs;
477
478         struct ac_contract ac;
479         struct ac_device_changed *adc;
480         ocs_fcport      *fcp = NULL;
481
482         fcp = node->sport->tgt_data;
483         if (fcp == NULL) {
484                 ocs_log_err(ocs, "FCP is NULL \n");
485                 return 1;
486         }
487
488         ac.contract_number = AC_CONTRACT_DEV_CHG;
489         adc = (struct ac_device_changed *) ac.contract_data;
490         adc->wwpn = ocs_node_get_wwpn(node);
491         adc->port = node->rnode.fc_id;
492         adc->target = node->instance_index;
493         adc->arrived = 0;
494         xpt_async(AC_CONTRACT, fcp->path, &ac);
495
496
497         if (reason == OCS_SCSI_INITIATOR_MISSING) {
498                 return OCS_SCSI_CALL_COMPLETE;
499         }
500
501         /*
502          * Update the IO watermark by incrementing it by the
503          * number of IOs reserved for each initiator.
504          */
505         atomic_add_acq_32(&ocs->io_high_watermark, OCS_RSVD_INI_IO);
506
507         return OCS_SCSI_CALL_COMPLETE;
508 }
509
510 /**
511  * @ingroup scsi_api_target
512  * @brief receive FCP SCSI Command
513  *
514  * Called by the base driver when a new SCSI command has been received.   The
515  * target-server will process the command, and issue data and/or response phase
516  * requests to the base driver.
517  *
518  * The IO context (ocs_io_t) structure has and element of type 
519  * ocs_scsi_tgt_io_t named tgt_io that is declared and used by 
520  * a target-server for private information.
521  *
522  * @param io pointer to IO context
523  * @param lun LUN for this IO
524  * @param cdb pointer to SCSI CDB
525  * @param cdb_len length of CDB in bytes
526  * @param flags command flags
527  *
528  * @return returns 0 for success, a negative error code value for failure.
529  */
530 int32_t ocs_scsi_recv_cmd(ocs_io_t *io, uint64_t lun, uint8_t *cdb,
531                                 uint32_t cdb_len, uint32_t flags)
532 {
533         ocs_t *ocs = io->ocs;
534         struct ccb_accept_tio *atio = NULL;
535         ocs_node_t      *node = io->node;
536         ocs_tgt_resource_t *trsrc = NULL;
537         int32_t         rc = -1;
538         ocs_fcport      *fcp = NULL;
539
540         fcp = node->sport->tgt_data;
541         if (fcp == NULL) {
542                 ocs_log_err(ocs, "FCP is NULL \n");
543                 return 1;
544         }
545
546         atomic_add_acq_32(&ocs->io_in_use, 1);
547
548         /* set target io timeout */
549         io->timeout = ocs->target_io_timer_sec;
550
551         if (ocs->enable_task_set_full && 
552                 (ocs->io_in_use >= ocs->io_high_watermark)) {
553                 return ocs_task_set_full_or_busy(io);
554         } else {
555                 atomic_store_rel_32(&io->node->tgt_node.busy_sent, FALSE);
556         }
557
558         if ((lun < OCS_MAX_LUN) && fcp->targ_rsrc[lun].enabled) {
559                 trsrc = &fcp->targ_rsrc[lun];
560         } else if (fcp->targ_rsrc_wildcard.enabled) {
561                 trsrc = &fcp->targ_rsrc_wildcard;
562         }
563
564         if (trsrc) {
565                 atio = (struct ccb_accept_tio *)STAILQ_FIRST(&trsrc->atio);
566         }
567
568         if (atio) {
569
570                 STAILQ_REMOVE_HEAD(&trsrc->atio, sim_links.stqe);
571
572                 atio->ccb_h.status = CAM_CDB_RECVD;
573                 atio->ccb_h.target_lun = lun;
574                 atio->sense_len = 0;
575
576                 atio->init_id = node->instance_index;
577                 atio->tag_id = io->tag;
578                 atio->ccb_h.ccb_io_ptr = io;
579
580                 if (flags & OCS_SCSI_CMD_SIMPLE)
581                         atio->tag_action = MSG_SIMPLE_Q_TAG;
582                 else if (flags &  FCP_TASK_ATTR_HEAD_OF_QUEUE)
583                         atio->tag_action = MSG_HEAD_OF_Q_TAG;
584                 else if (flags & FCP_TASK_ATTR_ORDERED)
585                         atio->tag_action = MSG_ORDERED_Q_TAG;
586                 else
587                         atio->tag_action = 0;
588
589                 atio->cdb_len = cdb_len;
590                 ocs_memcpy(atio->cdb_io.cdb_bytes, cdb, cdb_len);
591
592                 io->tgt_io.flags = 0;
593                 io->tgt_io.state = OCS_CAM_IO_COMMAND;
594                 io->tgt_io.lun = lun;
595
596                 xpt_done((union ccb *)atio);
597
598                 rc = 0;
599         } else {
600                 device_printf(
601                         ocs->dev, "%s: no ATIO for LUN %lx (en=%s) OX_ID %#x\n",
602                         __func__, (unsigned long)lun,
603                         trsrc ? (trsrc->enabled ? "T" : "F") : "X",
604                         be16toh(io->init_task_tag));
605
606                 io->tgt_io.state = OCS_CAM_IO_MAX;
607                 ocs_target_io_free(io);
608         }
609
610         return rc;
611 }
612
613 /**
614  * @ingroup scsi_api_target
615  * @brief receive FCP SCSI Command with first burst data.
616  *
617  * Receive a new FCP SCSI command from the base driver with first burst data.
618  *
619  * @param io pointer to IO context
620  * @param lun LUN for this IO
621  * @param cdb pointer to SCSI CDB
622  * @param cdb_len length of CDB in bytes
623  * @param flags command flags
624  * @param first_burst_buffers first burst buffers
625  * @param first_burst_buffer_count The number of bytes received in the first burst
626  *
627  * @return returns 0 for success, a negative error code value for failure.
628  */
629 int32_t ocs_scsi_recv_cmd_first_burst(ocs_io_t *io, uint64_t lun, uint8_t *cdb,
630                                         uint32_t cdb_len, uint32_t flags, 
631                                         ocs_dma_t first_burst_buffers[], 
632                                         uint32_t first_burst_buffer_count)
633 {
634         return -1;
635 }
636
637 /**
638  * @ingroup scsi_api_target
639  * @brief receive a TMF command IO
640  *
641  * Called by the base driver when a SCSI TMF command has been received.   The
642  * target-server will process the command, aborting commands as needed, and post
643  * a response using ocs_scsi_send_resp()
644  *
645  * The IO context (ocs_io_t) structure has and element of type ocs_scsi_tgt_io_t named
646  * tgt_io that is declared and used by a target-server for private information.
647  *
648  * If the target-server walks the nodes active_ios linked list, and starts IO
649  * abort processing, the code <b>must</b> be sure not to abort the IO passed into the
650  * ocs_scsi_recv_tmf() command.
651  *
652  * @param tmfio pointer to IO context
653  * @param lun logical unit value
654  * @param cmd command request
655  * @param abortio pointer to IO object to abort for TASK_ABORT (NULL for all other TMF)
656  * @param flags flags
657  *
658  * @return returns 0 for success, a negative error code value for failure.
659  */
660 int32_t ocs_scsi_recv_tmf(ocs_io_t *tmfio, uint64_t lun, ocs_scsi_tmf_cmd_e cmd,
661                                 ocs_io_t *abortio, uint32_t flags)
662 {
663         ocs_t *ocs = tmfio->ocs;
664         ocs_node_t *node = tmfio->node;
665         ocs_tgt_resource_t *trsrc = NULL;
666         struct ccb_immediate_notify *inot = NULL;
667         int32_t         rc = -1;
668         ocs_fcport      *fcp = NULL;
669
670         fcp = node->sport->tgt_data;
671         if (fcp == NULL) {
672                 ocs_log_err(ocs, "FCP is NULL \n");
673                 return 1;
674         }
675
676         if ((lun < OCS_MAX_LUN) && fcp->targ_rsrc[lun].enabled) {
677                 trsrc = &fcp->targ_rsrc[lun];
678         } else if (fcp->targ_rsrc_wildcard.enabled) {
679                 trsrc = &fcp->targ_rsrc_wildcard;
680         }
681
682         device_printf(tmfio->ocs->dev, "%s: io=%p cmd=%#x LU=%lx en=%s\n",
683                         __func__, tmfio, cmd, (unsigned long)lun,
684                         trsrc ? (trsrc->enabled ? "T" : "F") : "X");
685         if (trsrc) {
686                 inot = (struct ccb_immediate_notify *)STAILQ_FIRST(&trsrc->inot);
687         }
688
689         if (!inot) {
690                 device_printf(
691                         ocs->dev, "%s: no INOT for LUN %llx (en=%s) OX_ID %#x\n",
692                         __func__, (unsigned long long)lun, trsrc ? (trsrc->enabled ? "T" : "F") : "X",
693                         be16toh(tmfio->init_task_tag));
694
695                 if (abortio) {
696                         ocs_scsi_io_complete(abortio);
697                 }
698                 ocs_scsi_io_complete(tmfio);
699                 goto ocs_scsi_recv_tmf_out;
700         }
701
702
703         tmfio->tgt_io.app = abortio;
704
705         STAILQ_REMOVE_HEAD(&trsrc->inot, sim_links.stqe);
706
707         inot->tag_id = tmfio->tag;
708         inot->seq_id = tmfio->tag;
709
710         if ((lun < OCS_MAX_LUN) && fcp->targ_rsrc[lun].enabled) {
711                 inot->initiator_id = node->instance_index;
712         } else {
713                 inot->initiator_id = CAM_TARGET_WILDCARD;
714         } 
715
716         inot->ccb_h.status = CAM_MESSAGE_RECV;
717         inot->ccb_h.target_lun = lun;
718
719         switch (cmd) {
720         case OCS_SCSI_TMF_ABORT_TASK:
721                 inot->arg = MSG_ABORT_TASK;
722                 inot->seq_id = abortio->tag;
723                 device_printf(ocs->dev, "%s: ABTS IO.%#x st=%#x\n", 
724                         __func__, abortio->tag, abortio->tgt_io.state);
725                 abortio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_RECV;
726                 abortio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_NOTIFY;
727                 break;
728         case OCS_SCSI_TMF_QUERY_TASK_SET:
729                 device_printf(ocs->dev, 
730                         "%s: OCS_SCSI_TMF_QUERY_TASK_SET not supported\n",
731                                 __func__);
732                 STAILQ_INSERT_TAIL(&trsrc->inot, &inot->ccb_h, sim_links.stqe);
733                 ocs_scsi_io_complete(tmfio);
734                 goto ocs_scsi_recv_tmf_out;
735                 break;
736         case OCS_SCSI_TMF_ABORT_TASK_SET:
737                 inot->arg = MSG_ABORT_TASK_SET;
738                 break;
739         case OCS_SCSI_TMF_CLEAR_TASK_SET:
740                 inot->arg = MSG_CLEAR_TASK_SET;
741                 break;
742         case OCS_SCSI_TMF_QUERY_ASYNCHRONOUS_EVENT:
743                 inot->arg = MSG_QUERY_ASYNC_EVENT;
744                 break;
745         case OCS_SCSI_TMF_LOGICAL_UNIT_RESET:
746                 inot->arg = MSG_LOGICAL_UNIT_RESET;
747                 break;
748         case OCS_SCSI_TMF_CLEAR_ACA:
749                 inot->arg = MSG_CLEAR_ACA;
750                 break;
751         case OCS_SCSI_TMF_TARGET_RESET:
752                 inot->arg = MSG_TARGET_RESET;
753                 break;
754         default:
755                 device_printf(ocs->dev, "%s: unsupported TMF %#x\n",
756                                                          __func__, cmd);
757                 STAILQ_INSERT_TAIL(&trsrc->inot, &inot->ccb_h, sim_links.stqe);
758                 goto ocs_scsi_recv_tmf_out;
759         }
760
761         rc = 0;
762
763         xpt_print(inot->ccb_h.path, "%s: func=%#x stat=%#x id=%#x lun=%#x"
764                         " flags=%#x tag=%#x seq=%#x ini=%#x arg=%#x\n", 
765                         __func__, inot->ccb_h.func_code, inot->ccb_h.status,
766                         inot->ccb_h.target_id, 
767                         (unsigned int)inot->ccb_h.target_lun, inot->ccb_h.flags,
768                         inot->tag_id, inot->seq_id, inot->initiator_id,
769                         inot->arg);
770         xpt_done((union ccb *)inot);
771
772         if (abortio) {
773                 abortio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_DEV;
774                 rc = ocs_scsi_tgt_abort_io(abortio, ocs_io_abort_cb, tmfio);
775         }
776         
777 ocs_scsi_recv_tmf_out:
778         return rc;
779 }
780
781 /**
782  * @ingroup scsi_api_initiator
783  * @brief Initializes any initiator fields on the ocs structure.
784  *
785  * Called by OS initialization code when a new device is discovered.
786  *
787  * @param ocs pointer to ocs
788  *
789  * @return returns 0 for success, a negative error code value for failure.
790  */
791 int32_t
792 ocs_scsi_ini_new_device(ocs_t *ocs)
793 {
794
795         return 0;
796 }
797
798 /**
799  * @ingroup scsi_api_initiator
800  * @brief Tears down initiator members of ocs structure.
801  *
802  * Called by OS code when device is removed.
803  *
804  * @param ocs pointer to ocs
805  *
806  * @return returns 0 for success, a negative error code value for failure.
807  */
808
809 int32_t
810 ocs_scsi_ini_del_device(ocs_t *ocs)
811 {
812
813         return 0;
814 }
815
816
817 /**
818  * @ingroup scsi_api_initiator
819  * @brief accept new domain notification
820  *
821  * Called by base drive when new domain is discovered.  An initiator-client
822  * will accept this call to prepare for new remote node notifications
823  * arising from ocs_scsi_new_target().
824  *
825  * The domain context has the element <b>ocs_scsi_ini_domain_t ini_domain</b>
826  * which is declared by the initiator-client code and is used for 
827  * initiator-client private data.
828  *
829  * This function will only be called if the base-driver has been enabled for 
830  * initiator capability.
831  *
832  * Note that this call is made to initiator-client backends, 
833  * the ocs_scsi_tgt_new_domain() function is called to target-server backends.
834  *
835  * @param domain pointer to domain
836  *
837  * @return returns 0 for success, a negative error code value for failure.
838  */
839 int32_t
840 ocs_scsi_ini_new_domain(ocs_domain_t *domain)
841 {
842         return 0;
843 }
844
845 /**
846  * @ingroup scsi_api_initiator
847  * @brief accept domain lost notification
848  *
849  * Called by base-driver when a domain goes away.  An initiator-client will
850  * use this call to clean up all domain scoped resources.
851  *
852  * This function will only be called if the base-driver has been enabled for
853  * initiator capability.
854  *
855  * Note that this call is made to initiator-client backends,
856  * the ocs_scsi_tgt_del_domain() function is called to target-server backends.
857  *
858  * @param domain pointer to domain
859  *
860  * @return returns 0 for success, a negative error code value for failure.
861  */
862 void
863 ocs_scsi_ini_del_domain(ocs_domain_t *domain)
864 {
865 }
866
867 /**
868  * @ingroup scsi_api_initiator
869  * @brief accept new sli port notification
870  *
871  * Called by base drive when new sli port (sport) is discovered.
872  * A target-server will use this call to prepare for new remote node
873  * notifications arising from ocs_scsi_new_initiator().
874  *
875  * This function will only be called if the base-driver has been enabled for
876  * target capability.
877  *
878  * Note that this call is made to target-server backends,
879  * the ocs_scsi_ini_new_sport() function is called to initiator-client backends.
880  *
881  * @param sport pointer to sport
882  *
883  * @return returns 0 for success, a negative error code value for failure.
884  */
885 int32_t
886 ocs_scsi_ini_new_sport(ocs_sport_t *sport)
887 {
888         ocs_t *ocs = sport->ocs;
889
890         if(!sport->is_vport) {
891                 sport->tgt_data = FCPORT(ocs, 0);
892         }
893
894         return 0;
895 }
896
897 /**
898  * @ingroup scsi_api_initiator
899  * @brief accept sli port gone notification
900  *
901  * Called by base-driver when a sport goes away.  A target-server will
902  * use this call to clean up all sport scoped resources.
903  *
904  * Note that this call is made to target-server backends,
905  * the ocs_scsi_ini_del_sport() function is called to initiator-client backends.
906  *
907  * @param sport pointer to SLI port
908  *
909  * @return returns 0 for success, a negative error code value for failure.
910  */
911 void
912 ocs_scsi_ini_del_sport(ocs_sport_t *sport)
913 {
914 }
915
916 void 
917 ocs_scsi_sport_deleted(ocs_sport_t *sport)
918 {
919         ocs_t *ocs = sport->ocs;
920         ocs_fcport *fcp = NULL;
921
922         ocs_xport_stats_t value;
923
924         if (!sport->is_vport) {
925                 return;
926         }
927
928         fcp = sport->tgt_data;
929
930         ocs_xport_status(ocs->xport, OCS_XPORT_PORT_STATUS, &value);
931
932         if (value.value == 0) {
933                 ocs_log_debug(ocs, "PORT offline,.. skipping\n");
934                 return;
935         }       
936
937         if ((fcp->role != KNOB_ROLE_NONE)) {
938                 if(fcp->vport->sport != NULL) {
939                         ocs_log_debug(ocs,"sport is not NULL, skipping\n");
940                         return;
941                 }
942
943                 ocs_sport_vport_alloc(ocs->domain, fcp->vport);
944                 return;
945         }
946
947 }
948
949 int32_t
950 ocs_tgt_find(ocs_fcport *fcp, ocs_node_t *node)
951 {
952         ocs_fc_target_t *tgt = NULL;
953         uint32_t i;
954         
955         for (i = 0; i < OCS_MAX_TARGETS; i++) {
956                 tgt = &fcp->tgt[i];
957
958                 if (tgt->state == OCS_TGT_STATE_NONE)
959                         continue;
960                 
961                 if (ocs_node_get_wwpn(node) == tgt->wwpn) {
962                         return i;
963                 }
964         }
965         
966         return -1;
967 }
968
969 /**
970  * @ingroup scsi_api_initiator
971  * @brief receive notification of a new SCSI target node
972  *
973  * Sent by base driver to notify an initiator-client of the presense of a new
974  * remote target.   The initiator-server may use this call to prepare for
975  * inbound IO from this node.
976  *
977  * This function is only called if the base driver is enabled for
978  * initiator capability.
979  *
980  * @param node pointer to new remote initiator node
981  *
982  * @return none
983  *
984  * @note
985  */
986
987 uint32_t
988 ocs_update_tgt(ocs_node_t *node, ocs_fcport *fcp, uint32_t tgt_id)
989 {
990         ocs_fc_target_t *tgt = NULL;
991         
992         tgt = &fcp->tgt[tgt_id];
993
994         tgt->node_id = node->instance_index;
995         tgt->state = OCS_TGT_STATE_VALID;
996         
997         tgt->port_id = node->rnode.fc_id;
998         tgt->wwpn = ocs_node_get_wwpn(node);
999         tgt->wwnn = ocs_node_get_wwnn(node);
1000         return 0;
1001 }
1002
1003 uint32_t
1004 ocs_add_new_tgt(ocs_node_t *node, ocs_fcport *fcp)
1005 {
1006         uint32_t i;
1007
1008         struct ocs_softc *ocs = node->ocs;
1009         union ccb *ccb = NULL;
1010         for (i = 0; i < OCS_MAX_TARGETS; i++) {
1011                 if (fcp->tgt[i].state == OCS_TGT_STATE_NONE)
1012                         break;
1013         }
1014
1015         if (NULL == (ccb = xpt_alloc_ccb_nowait())) {
1016                 device_printf(ocs->dev, "%s: ccb allocation failed\n", __func__);
1017                 return -1;
1018         }
1019
1020         if (CAM_REQ_CMP != xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1021                                 cam_sim_path(fcp->sim),
1022                                 i, CAM_LUN_WILDCARD)) {
1023                 device_printf(
1024                         ocs->dev, "%s: target path creation failed\n", __func__);
1025                 xpt_free_ccb(ccb);
1026                 return -1;
1027         }
1028
1029         ocs_update_tgt(node, fcp, i);
1030         xpt_rescan(ccb);
1031         return 0;
1032 }
1033
1034 int32_t
1035 ocs_scsi_new_target(ocs_node_t *node)
1036 {
1037         ocs_fcport      *fcp = NULL;
1038         int32_t i;
1039
1040         fcp = node->sport->tgt_data;
1041         if (fcp == NULL) {
1042                 printf("%s:FCP is NULL \n", __func__);
1043                 return 0;
1044         }
1045
1046         i = ocs_tgt_find(fcp, node);
1047         
1048         if (i < 0) {
1049                 ocs_add_new_tgt(node, fcp);
1050                 return 0;
1051         }
1052
1053         ocs_update_tgt(node, fcp, i);
1054         return 0;
1055 }
1056
1057 static void
1058 ocs_delete_target(ocs_t *ocs, ocs_fcport *fcp, int tgt)
1059 {
1060         struct cam_path *cpath = NULL;
1061
1062         if (!fcp->sim) { 
1063                 device_printf(ocs->dev, "%s: calling with NULL sim\n", __func__); 
1064                 return;
1065         }
1066         
1067         if (CAM_REQ_CMP == xpt_create_path(&cpath, NULL, cam_sim_path(fcp->sim),
1068                                 tgt, CAM_LUN_WILDCARD)) {
1069                 xpt_async(AC_LOST_DEVICE, cpath, NULL);
1070                 
1071                 xpt_free_path(cpath);
1072         }
1073 }
1074
1075 /*
1076  * Device Lost Timer Function- when we have decided that a device was lost,
1077  * we wait a specific period of time prior to telling the OS about lost device.
1078  *
1079  * This timer function gets activated when the device was lost. 
1080  * This function fires once a second and then scans the port database
1081  * for devices that are marked dead but still have a virtual target assigned.
1082  * We decrement a counter for that port database entry, and when it hits zero,
1083  * we tell the OS the device was lost. Timer will be stopped when the device
1084  * comes back active or removed from the OS.
1085  */
1086 static void
1087 ocs_ldt(void *arg)
1088 {
1089         ocs_fcport *fcp = arg;
1090         taskqueue_enqueue(taskqueue_thread, &fcp->ltask);
1091 }
1092
1093 static void
1094 ocs_ldt_task(void *arg, int pending)
1095 {
1096         ocs_fcport *fcp = arg;
1097         ocs_t   *ocs = fcp->ocs;
1098         int i, more_to_do = 0;
1099         ocs_fc_target_t *tgt = NULL;
1100
1101         for (i = 0; i < OCS_MAX_TARGETS; i++) {
1102                 tgt = &fcp->tgt[i];
1103
1104                 if (tgt->state != OCS_TGT_STATE_LOST) {
1105                         continue;
1106                 }
1107
1108                 if ((tgt->gone_timer != 0) && (ocs->attached)){
1109                         tgt->gone_timer -= 1;
1110                         more_to_do++;
1111                         continue;
1112                 }
1113
1114                 if (tgt->is_target) {
1115                         tgt->is_target = 0;
1116                         ocs_delete_target(ocs, fcp, i);
1117                 }
1118
1119                 tgt->state = OCS_TGT_STATE_NONE;
1120         }
1121
1122         if (more_to_do) {
1123                 callout_reset(&fcp->ldt, hz, ocs_ldt, fcp);
1124         } else {
1125                 callout_deactivate(&fcp->ldt);
1126         }
1127
1128 }
1129
1130 /**
1131  * @ingroup scsi_api_initiator
1132  * @brief Delete a SCSI target node
1133  *
1134  * Sent by base driver to notify a initiator-client that a remote target 
1135  * is now gone. The base driver will have terminated all  outstanding IOs 
1136  * and the initiator-client will receive appropriate completions.
1137  *
1138  * The ocs_node_t structure has and elment of type ocs_scsi_ini_node_t named
1139  * ini_node that is declared and used by a target-server for private
1140  * information.
1141  *
1142  * This function is only called if the base driver is enabled for
1143  * initiator capability.
1144  *
1145  * @param node pointer node being deleted
1146  * @param reason reason for deleting the target
1147  *
1148  * @return Returns OCS_SCSI_CALL_ASYNC if target delete is queued for async 
1149  * completion and OCS_SCSI_CALL_COMPLETE if call completed or error.
1150  *
1151  * @note
1152  */
1153 int32_t
1154 ocs_scsi_del_target(ocs_node_t *node, ocs_scsi_del_target_reason_e reason)
1155 {
1156         struct ocs_softc *ocs = node->ocs;
1157         ocs_fcport      *fcp = NULL;
1158         ocs_fc_target_t *tgt = NULL;
1159         uint32_t        tgt_id;
1160
1161         fcp = node->sport->tgt_data;
1162         if (fcp == NULL) {
1163                 ocs_log_err(ocs,"FCP is NULL \n");
1164                 return 0;
1165         }
1166
1167         tgt_id = ocs_tgt_find(fcp, node);
1168
1169         tgt = &fcp->tgt[tgt_id];
1170
1171         // IF in shutdown delete target.
1172         if(!ocs->attached) {
1173                 ocs_delete_target(ocs, fcp, tgt_id);
1174         } else {
1175         
1176                 tgt->state = OCS_TGT_STATE_LOST;
1177                 tgt->gone_timer = 30;
1178                 if (!callout_active(&fcp->ldt)) {
1179                         callout_reset(&fcp->ldt, hz, ocs_ldt, fcp);
1180                 }
1181         }
1182         
1183         return 0;
1184 }
1185
1186 /**
1187  * @brief Initialize SCSI IO
1188  *
1189  * Initialize SCSI IO, this function is called once per IO during IO pool
1190  * allocation so that the target server may initialize any of its own private
1191  * data.
1192  *
1193  * @param io pointer to SCSI IO object
1194  *
1195  * @return returns 0 for success, a negative error code value for failure.
1196  */
1197 int32_t
1198 ocs_scsi_tgt_io_init(ocs_io_t *io)
1199 {
1200         return 0;
1201 }
1202
1203 /**
1204  * @brief Uninitialize SCSI IO
1205  *
1206  * Uninitialize target server private data in a SCSI io object
1207  *
1208  * @param io pointer to SCSI IO object
1209  *
1210  * @return returns 0 for success, a negative error code value for failure.
1211  */
1212 int32_t
1213 ocs_scsi_tgt_io_exit(ocs_io_t *io)
1214 {
1215         return 0;
1216 }
1217
1218 /**
1219  * @brief Initialize SCSI IO
1220  *
1221  * Initialize SCSI IO, this function is called once per IO during IO pool
1222  * allocation so that the initiator client may initialize any of its own private
1223  * data.
1224  *
1225  * @param io pointer to SCSI IO object
1226  *
1227  * @return returns 0 for success, a negative error code value for failure.
1228  */
1229 int32_t
1230 ocs_scsi_ini_io_init(ocs_io_t *io)
1231 {
1232         return 0;
1233 }
1234
1235 /**
1236  * @brief Uninitialize SCSI IO
1237  *
1238  * Uninitialize initiator client private data in a SCSI io object
1239  *
1240  * @param io pointer to SCSI IO object
1241  *
1242  * @return returns 0 for success, a negative error code value for failure.
1243  */
1244 int32_t
1245 ocs_scsi_ini_io_exit(ocs_io_t *io)
1246 {
1247         return 0;
1248 }
1249 /*
1250  * End of functions required by SCSI base driver API
1251  ***************************************************************************/
1252
1253 static __inline void
1254 ocs_set_ccb_status(union ccb *ccb, cam_status status)
1255 {
1256         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1257         ccb->ccb_h.status |= status;
1258 }
1259
1260 static int32_t
1261 ocs_task_set_full_or_busy_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status,
1262                                                 uint32_t flags, void *arg)
1263 {
1264
1265         ocs_target_io_free(io);
1266
1267         return 0;
1268 }
1269
1270 /**
1271  * @brief send SCSI task set full or busy status
1272  *
1273  * A SCSI task set full or busy response is sent depending on whether
1274  * another IO is already active on the LUN.
1275  *
1276  * @param io pointer to IO context
1277  *
1278  * @return returns 0 for success, a negative error code value for failure.
1279  */
1280
1281 static int32_t
1282 ocs_task_set_full_or_busy(ocs_io_t *io)
1283 {
1284         ocs_scsi_cmd_resp_t rsp = { 0 };
1285         ocs_t *ocs = io->ocs;
1286
1287         /*
1288          * If there is another command for the LUN, then send task set full,
1289          * if this is the first one, then send the busy status.
1290          *
1291          * if 'busy sent' is FALSE, set it to TRUE and send BUSY
1292          * otherwise send FULL
1293          */
1294         if (atomic_cmpset_acq_32(&io->node->tgt_node.busy_sent, FALSE, TRUE)) {
1295                 rsp.scsi_status = SCSI_STATUS_BUSY; /* Busy */
1296                 printf("%s: busy [%s] tag=%x iiu=%d ihw=%d\n", __func__,
1297                                 io->node->display_name, io->tag,
1298                                 io->ocs->io_in_use, io->ocs->io_high_watermark);
1299         } else {
1300                 rsp.scsi_status = SCSI_STATUS_TASK_SET_FULL; /* Task set full */
1301                 printf("%s: full tag=%x iiu=%d\n", __func__, io->tag,
1302                         io->ocs->io_in_use);
1303         }
1304
1305         /* Log a message here indicating a busy or task set full state */
1306         if (OCS_LOG_ENABLE_Q_FULL_BUSY_MSG(ocs)) {
1307                 /* Log Task Set Full */
1308                 if (rsp.scsi_status == SCSI_STATUS_TASK_SET_FULL) {
1309                         /* Task Set Full Message */
1310                         ocs_log_info(ocs, "OCS CAM TASK SET FULL. Tasks >= %d\n",
1311                                         ocs->io_high_watermark);
1312                 }
1313                 else if (rsp.scsi_status == SCSI_STATUS_BUSY) {
1314                         /* Log Busy Message */
1315                         ocs_log_info(ocs, "OCS CAM SCSI BUSY\n");
1316                 }
1317         }
1318
1319         /* Send the response */
1320         return 
1321         ocs_scsi_send_resp(io, 0, &rsp, ocs_task_set_full_or_busy_cb, NULL);
1322 }
1323
1324 /**
1325  * @ingroup cam_io
1326  * @brief Process target IO completions
1327  *
1328  * @param io 
1329  * @param scsi_status did the IO complete successfully
1330  * @param flags 
1331  * @param arg application specific pointer provided in the call to ocs_target_io()
1332  *
1333  * @todo
1334  */
1335 static int32_t ocs_scsi_target_io_cb(ocs_io_t *io, 
1336                                 ocs_scsi_io_status_e scsi_status,
1337                                 uint32_t flags, void *arg)
1338 {
1339         union ccb *ccb = arg;
1340         struct ccb_scsiio *csio = &ccb->csio;
1341         struct ocs_softc *ocs = csio->ccb_h.ccb_ocs_ptr;
1342         uint32_t cam_dir = ccb->ccb_h.flags & CAM_DIR_MASK;
1343         uint32_t io_is_done = 
1344                 (ccb->ccb_h.flags & CAM_SEND_STATUS) == CAM_SEND_STATUS;
1345
1346         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1347
1348         if (CAM_DIR_NONE != cam_dir) {
1349                 bus_dmasync_op_t op;
1350
1351                 if (CAM_DIR_IN == cam_dir) {
1352                         op = BUS_DMASYNC_POSTREAD;
1353                 } else {
1354                         op = BUS_DMASYNC_POSTWRITE;
1355                 }
1356                 /* Synchronize the DMA memory with the CPU and free the mapping */
1357                 bus_dmamap_sync(ocs->buf_dmat, io->tgt_io.dmap, op);
1358                 if (io->tgt_io.flags & OCS_CAM_IO_F_DMAPPED) {
1359                         bus_dmamap_unload(ocs->buf_dmat, io->tgt_io.dmap);
1360                 }
1361         }
1362
1363         if (io->tgt_io.sendresp) {
1364                 io->tgt_io.sendresp = 0;
1365                 ocs_scsi_cmd_resp_t  resp = { 0 };
1366                 io->tgt_io.state = OCS_CAM_IO_RESP;
1367                 resp.scsi_status = scsi_status;
1368                 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1369                         resp.sense_data = (uint8_t *)&csio->sense_data;
1370                         resp.sense_data_length = csio->sense_len;
1371                 }
1372                 resp.residual = io->exp_xfer_len - io->transferred;
1373
1374                 return ocs_scsi_send_resp(io, 0, &resp, ocs_scsi_target_io_cb, ccb);
1375         }
1376
1377         switch (scsi_status) {
1378         case OCS_SCSI_STATUS_GOOD:
1379                 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
1380                 break;
1381         case OCS_SCSI_STATUS_ABORTED:
1382                 ocs_set_ccb_status(ccb, CAM_REQ_ABORTED);
1383                 break;
1384         default:
1385                 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1386         }
1387
1388         if (io_is_done) {
1389                 if ((io->tgt_io.flags & OCS_CAM_IO_F_ABORT_NOTIFY) == 0) {
1390                         ocs_target_io_free(io);
1391                 }
1392         } else {
1393                 io->tgt_io.state = OCS_CAM_IO_DATA_DONE;
1394                 /*device_printf(ocs->dev, "%s: CTIO state=%d tag=%#x\n",
1395                                 __func__, io->tgt_io.state, io->tag);*/
1396         }
1397
1398         xpt_done(ccb);
1399
1400         return 0;
1401 }
1402
1403 /**
1404  * @note        1. Since the CCB is assigned to the ocs_io_t on an XPT_CONT_TARGET_IO
1405  *                 action, if an initiator aborts a command prior to the SIM receiving
1406  *                 a CTIO, the IO's CCB will be NULL.
1407  */
1408 static int32_t
1409 ocs_io_abort_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags, void *arg)
1410 {
1411         struct ocs_softc *ocs = NULL;
1412         ocs_io_t        *tmfio = arg;
1413         ocs_scsi_tmf_resp_e tmf_resp = OCS_SCSI_TMF_FUNCTION_COMPLETE;
1414         int32_t rc = 0;
1415
1416         ocs = io->ocs;
1417
1418         io->tgt_io.flags &= ~OCS_CAM_IO_F_ABORT_DEV;
1419
1420         /* A good status indicates the IO was aborted and will be completed in
1421          * the IO's completion handler. Handle the other cases here. */
1422         switch (scsi_status) {
1423         case OCS_SCSI_STATUS_GOOD:
1424                 break;
1425         case OCS_SCSI_STATUS_NO_IO:
1426                 break;
1427         default:
1428                 device_printf(ocs->dev, "%s: unhandled status %d\n",
1429                                 __func__, scsi_status);
1430                 tmf_resp = OCS_SCSI_TMF_FUNCTION_REJECTED;
1431                 rc = -1;
1432         }
1433
1434         ocs_scsi_send_tmf_resp(tmfio, tmf_resp, NULL, ocs_target_tmf_cb, NULL);
1435
1436         return rc;
1437 }
1438
1439 /**
1440  * @ingroup cam_io
1441  * @brief Process initiator IO completions
1442  *
1443  * @param io 
1444  * @param scsi_status did the IO complete successfully
1445  * @param rsp pointer to response buffer
1446  * @param flags 
1447  * @param arg application specific pointer provided in the call to ocs_target_io()
1448  *
1449  * @todo
1450  */
1451 static int32_t ocs_scsi_initiator_io_cb(ocs_io_t *io,
1452                                         ocs_scsi_io_status_e scsi_status,
1453                                         ocs_scsi_cmd_resp_t *rsp,
1454                                         uint32_t flags, void *arg)
1455 {
1456         union ccb *ccb = arg;
1457         struct ccb_scsiio *csio = &ccb->csio;
1458         struct ocs_softc *ocs = csio->ccb_h.ccb_ocs_ptr;
1459         uint32_t cam_dir = ccb->ccb_h.flags & CAM_DIR_MASK;
1460         cam_status ccb_status= CAM_REQ_CMP_ERR;
1461
1462         if (CAM_DIR_NONE != cam_dir) {
1463                 bus_dmasync_op_t op;
1464
1465                 if (CAM_DIR_IN == cam_dir) {
1466                         op = BUS_DMASYNC_POSTREAD;
1467                 } else {
1468                         op = BUS_DMASYNC_POSTWRITE;
1469                 }
1470                 /* Synchronize the DMA memory with the CPU and free the mapping */
1471                 bus_dmamap_sync(ocs->buf_dmat, io->tgt_io.dmap, op);
1472                 if (io->tgt_io.flags & OCS_CAM_IO_F_DMAPPED) {
1473                         bus_dmamap_unload(ocs->buf_dmat, io->tgt_io.dmap);
1474                 }
1475         }
1476
1477         if (scsi_status == OCS_SCSI_STATUS_CHECK_RESPONSE) {
1478                 csio->scsi_status = rsp->scsi_status;
1479                 if (SCSI_STATUS_OK != rsp->scsi_status) {
1480                         ccb_status = CAM_SCSI_STATUS_ERROR;
1481                 }
1482
1483                 csio->resid = rsp->residual;
1484                 if (rsp->residual > 0) {
1485                         uint32_t length = rsp->response_wire_length;
1486                         /* underflow */
1487                         if (csio->dxfer_len == (length + csio->resid)) {
1488                                 ccb_status = CAM_REQ_CMP;
1489                         }
1490                 } else if (rsp->residual < 0) {
1491                         ccb_status = CAM_DATA_RUN_ERR;
1492                 }
1493
1494                 if ((rsp->sense_data_length) &&
1495                         !(ccb->ccb_h.flags & (CAM_SENSE_PHYS | CAM_SENSE_PTR))) {
1496                         uint32_t        sense_len = 0;
1497
1498                         ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1499                         if (rsp->sense_data_length < csio->sense_len) {
1500                                 csio->sense_resid = 
1501                                         csio->sense_len - rsp->sense_data_length;
1502                                 sense_len = rsp->sense_data_length;
1503                         } else {
1504                                 csio->sense_resid = 0;
1505                                 sense_len = csio->sense_len;
1506                         }
1507                         ocs_memcpy(&csio->sense_data, rsp->sense_data, sense_len);
1508                 }
1509         } else if (scsi_status != OCS_SCSI_STATUS_GOOD) {
1510                 ccb_status = CAM_REQ_CMP_ERR;
1511                 ocs_set_ccb_status(ccb, ccb_status);
1512                 csio->ccb_h.status |= CAM_DEV_QFRZN;
1513                 xpt_freeze_devq(csio->ccb_h.path, 1);
1514
1515         } else {
1516                 ccb_status = CAM_REQ_CMP;
1517         }
1518
1519         ocs_set_ccb_status(ccb, ccb_status);
1520
1521         ocs_scsi_io_free(io);
1522
1523         csio->ccb_h.ccb_io_ptr = NULL;
1524         csio->ccb_h.ccb_ocs_ptr = NULL;
1525         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1526
1527         xpt_done(ccb);
1528
1529         return 0;
1530 }
1531
1532 /**
1533  * @brief Load scatter-gather list entries into an IO
1534  *
1535  * This routine relies on the driver instance's software context pointer and
1536  * the IO object pointer having been already assigned to hooks in the CCB.
1537  * Although the routine does not return success/fail, callers can look at the
1538  * n_sge member to determine if the mapping failed (0 on failure).
1539  *
1540  * @param arg pointer to the CAM ccb for this IO
1541  * @param seg DMA address/length pairs
1542  * @param nseg number of DMA address/length pairs
1543  * @param error any errors while mapping the IO
1544  */
1545 static void
1546 ocs_scsi_dmamap_load(void *arg, bus_dma_segment_t *seg, int nseg, int error)
1547 {
1548         ocs_dmamap_load_arg_t *sglarg = (ocs_dmamap_load_arg_t*) arg;
1549
1550         if (error) {
1551                 printf("%s: seg=%p nseg=%d error=%d\n",
1552                                 __func__, seg, nseg, error);
1553                 sglarg->rc = -1;
1554         } else {
1555                 uint32_t i = 0;
1556                 uint32_t c = 0;
1557
1558                 if ((sglarg->sgl_count + nseg) > sglarg->sgl_max) {
1559                         printf("%s: sgl_count=%d nseg=%d max=%d\n", __func__,
1560                                 sglarg->sgl_count, nseg, sglarg->sgl_max);
1561                         sglarg->rc = -2;
1562                         return;
1563                 }
1564
1565                 for (i = 0, c = sglarg->sgl_count; i < nseg; i++, c++) {
1566                         sglarg->sgl[c].addr = seg[i].ds_addr;
1567                         sglarg->sgl[c].len  = seg[i].ds_len;
1568                 }
1569
1570                 sglarg->sgl_count = c;
1571
1572                 sglarg->rc = 0;
1573         }
1574 }
1575
1576 /**
1577  * @brief Build a scatter-gather list from a CAM CCB
1578  *
1579  * @param ocs the driver instance's software context
1580  * @param ccb pointer to the CCB
1581  * @param io pointer to the previously allocated IO object
1582  * @param sgl pointer to SGL
1583  * @param sgl_max number of entries in sgl
1584  *
1585  * @return 0 on success, non-zero otherwise
1586  */
1587 static int32_t
1588 ocs_build_scsi_sgl(struct ocs_softc *ocs, union ccb *ccb, ocs_io_t *io,
1589                 ocs_scsi_sgl_t *sgl, uint32_t sgl_max)
1590 {
1591         ocs_dmamap_load_arg_t dmaarg;
1592         int32_t err = 0;
1593
1594         if (!ocs || !ccb || !io || !sgl) {
1595                 printf("%s: bad param o=%p c=%p i=%p s=%p\n", __func__,
1596                                 ocs, ccb, io, sgl);
1597                 return -1;
1598         }
1599
1600         io->tgt_io.flags &= ~OCS_CAM_IO_F_DMAPPED;
1601
1602         dmaarg.sgl = sgl;
1603         dmaarg.sgl_count = 0;
1604         dmaarg.sgl_max = sgl_max;
1605         dmaarg.rc = 0;
1606
1607         err = bus_dmamap_load_ccb(ocs->buf_dmat, io->tgt_io.dmap, ccb,
1608                         ocs_scsi_dmamap_load, &dmaarg, 0);
1609
1610         if (err || dmaarg.rc) {
1611                 device_printf(
1612                         ocs->dev, "%s: bus_dmamap_load_ccb error (%d %d)\n",
1613                                 __func__, err, dmaarg.rc);
1614                 return -1;
1615         }
1616
1617         io->tgt_io.flags |= OCS_CAM_IO_F_DMAPPED;
1618         return dmaarg.sgl_count;
1619 }
1620
1621 /**
1622  * @ingroup cam_io
1623  * @brief Send a target IO
1624  *
1625  * @param ocs the driver instance's software context
1626  * @param ccb pointer to the CCB
1627  *
1628  * @return 0 on success, non-zero otherwise
1629  */
1630 static int32_t
1631 ocs_target_io(struct ocs_softc *ocs, union ccb *ccb)
1632 {
1633         struct ccb_scsiio *csio = &ccb->csio;
1634         ocs_io_t *io = NULL;
1635         uint32_t cam_dir = ccb->ccb_h.flags & CAM_DIR_MASK;
1636         bool sendstatus = ccb->ccb_h.flags & CAM_SEND_STATUS;
1637         uint32_t xferlen = csio->dxfer_len;
1638         int32_t rc = 0;
1639
1640         io = ocs_scsi_find_io(ocs, csio->tag_id);
1641         if (io == NULL) {
1642                 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1643                 panic("bad tag value");
1644                 return 1;
1645         }
1646
1647         /* Received an ABORT TASK for this IO */
1648         if (io->tgt_io.flags & OCS_CAM_IO_F_ABORT_RECV) {
1649                 /*device_printf(ocs->dev,
1650                         "%s: XPT_CONT_TARGET_IO state=%d tag=%#x xid=%#x flags=%#x\n",
1651                         __func__, io->tgt_io.state, io->tag, io->init_task_tag,
1652                         io->tgt_io.flags);*/
1653                 io->tgt_io.flags |= OCS_CAM_IO_F_ABORT_CAM;
1654
1655                 if (ccb->ccb_h.flags & CAM_SEND_STATUS) {
1656                         ocs_set_ccb_status(ccb, CAM_REQ_CMP);
1657                         ocs_target_io_free(io);
1658                         return 1;
1659                 } 
1660
1661                 ocs_set_ccb_status(ccb, CAM_REQ_ABORTED);
1662
1663                 return 1;
1664         }
1665
1666         io->tgt_io.app = ccb;
1667
1668         ocs_set_ccb_status(ccb, CAM_REQ_INPROG);
1669         ccb->ccb_h.status |= CAM_SIM_QUEUED;
1670
1671         csio->ccb_h.ccb_ocs_ptr = ocs;
1672         csio->ccb_h.ccb_io_ptr  = io;
1673
1674         if ((sendstatus && (xferlen == 0))) {
1675                 ocs_scsi_cmd_resp_t     resp = { 0 };
1676
1677                 ocs_assert(ccb->ccb_h.flags & CAM_SEND_STATUS, -1);
1678
1679                 io->tgt_io.state = OCS_CAM_IO_RESP;
1680
1681                 resp.scsi_status = csio->scsi_status;
1682
1683                 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
1684                         resp.sense_data = (uint8_t *)&csio->sense_data;
1685                         resp.sense_data_length = csio->sense_len;
1686                 }
1687
1688                 resp.residual = io->exp_xfer_len - io->transferred;
1689                 rc = ocs_scsi_send_resp(io, 0, &resp, ocs_scsi_target_io_cb, ccb);
1690
1691         } else if (xferlen != 0) {
1692                 ocs_scsi_sgl_t sgl[OCS_FC_MAX_SGL];
1693                 int32_t sgl_count = 0;
1694
1695                 io->tgt_io.state = OCS_CAM_IO_DATA;
1696                 
1697                 if (sendstatus)
1698                         io->tgt_io.sendresp = 1;
1699
1700                 sgl_count = ocs_build_scsi_sgl(ocs, ccb, io, sgl, ARRAY_SIZE(sgl));
1701                 if (sgl_count > 0) {
1702                         if (cam_dir == CAM_DIR_IN) {
1703                                 rc = ocs_scsi_send_rd_data(io, 0, NULL, sgl,
1704                                                 sgl_count, csio->dxfer_len,
1705                                                 ocs_scsi_target_io_cb, ccb);
1706                         } else if (cam_dir == CAM_DIR_OUT) {
1707                                 rc = ocs_scsi_recv_wr_data(io, 0, NULL, sgl,
1708                                                 sgl_count, csio->dxfer_len,
1709                                                 ocs_scsi_target_io_cb, ccb);
1710                         } else {
1711                                 device_printf(ocs->dev, "%s:"
1712                                                 " unknown CAM direction %#x\n",
1713                                                 __func__, cam_dir);
1714                                 ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
1715                                 rc = 1;
1716                         }
1717                 } else {
1718                         device_printf(ocs->dev, "%s: building SGL failed\n",
1719                                                 __func__);
1720                         ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1721                         rc = 1;
1722                 }
1723         } else {
1724                 device_printf(ocs->dev, "%s: Wrong value xfer and sendstatus"
1725                                         " are 0 \n", __func__);
1726                 ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
1727                 rc = 1;
1728
1729         }
1730
1731         if (rc) {
1732                 ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
1733                 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1734                 io->tgt_io.state = OCS_CAM_IO_DATA_DONE;
1735                 device_printf(ocs->dev, "%s: CTIO state=%d tag=%#x\n",
1736                                 __func__, io->tgt_io.state, io->tag);
1737         if ((sendstatus && (xferlen == 0))) {
1738                         ocs_target_io_free(io);
1739                 }
1740         }
1741
1742         return rc;
1743 }
1744
1745 static int32_t
1746 ocs_target_tmf_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status, uint32_t flags,
1747                 void *arg)
1748 {
1749
1750         /*device_printf(io->ocs->dev, "%s: tag=%x io=%p s=%#x\n",
1751                          __func__, io->tag, io, scsi_status);*/
1752         ocs_scsi_io_complete(io);
1753
1754         return 0;
1755 }
1756
1757 /**
1758  * @ingroup cam_io
1759  * @brief Send an initiator IO
1760  *
1761  * @param ocs the driver instance's software context
1762  * @param ccb pointer to the CCB
1763  *
1764  * @return 0 on success, non-zero otherwise
1765  */
1766 static int32_t
1767 ocs_initiator_io(struct ocs_softc *ocs, union ccb *ccb)
1768 {
1769         int32_t rc;
1770         struct ccb_scsiio *csio = &ccb->csio;
1771         struct ccb_hdr *ccb_h = &csio->ccb_h;
1772         ocs_node_t *node = NULL;
1773         ocs_io_t *io = NULL;
1774         ocs_scsi_sgl_t sgl[OCS_FC_MAX_SGL];
1775         int32_t sgl_count;
1776
1777         ocs_fcport      *fcp = NULL;
1778         fcp = FCPORT(ocs, cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path)));
1779         if (fcp == NULL) {
1780                 device_printf(ocs->dev, "%s: fcp is NULL\n", __func__);
1781                 return -1;
1782         }
1783
1784         if (fcp->tgt[ccb_h->target_id].state == OCS_TGT_STATE_LOST) {
1785                 device_printf(ocs->dev, "%s: device LOST %d\n", __func__,
1786                                                         ccb_h->target_id);
1787                 return CAM_REQUEUE_REQ;
1788         }
1789
1790         if (fcp->tgt[ccb_h->target_id].state == OCS_TGT_STATE_NONE) {
1791                 device_printf(ocs->dev, "%s: device not ready %d\n", __func__,
1792                                                         ccb_h->target_id);
1793                 return CAM_SEL_TIMEOUT;
1794         }
1795
1796         node = ocs_node_get_instance(ocs, fcp->tgt[ccb_h->target_id].node_id);
1797         if (node == NULL) {
1798                 device_printf(ocs->dev, "%s: no device %d\n", __func__,
1799                                                         ccb_h->target_id);
1800                 return CAM_SEL_TIMEOUT;
1801         }
1802
1803         if (!node->targ) {
1804                 device_printf(ocs->dev, "%s: not target device %d\n", __func__,
1805                                                         ccb_h->target_id);
1806                 return CAM_SEL_TIMEOUT;
1807         }
1808
1809         io = ocs_scsi_io_alloc(node, OCS_SCSI_IO_ROLE_ORIGINATOR);
1810         if (io == NULL) {
1811                 device_printf(ocs->dev, "%s: unable to alloc IO\n", __func__);
1812                 return -1;
1813         }
1814
1815         /* eventhough this is INI, use target structure as ocs_build_scsi_sgl
1816          * only references the tgt_io part of an ocs_io_t */
1817         io->tgt_io.app = ccb;
1818
1819         csio->ccb_h.ccb_ocs_ptr = ocs;
1820         csio->ccb_h.ccb_io_ptr  = io;
1821
1822         sgl_count = ocs_build_scsi_sgl(ocs, ccb, io, sgl, ARRAY_SIZE(sgl));
1823         if (sgl_count < 0) {
1824                 ocs_scsi_io_free(io);
1825                 device_printf(ocs->dev, "%s: building SGL failed\n", __func__);
1826                 return -1;
1827         }
1828
1829         if (ccb->ccb_h.timeout == CAM_TIME_INFINITY) {
1830                 io->timeout = 0;
1831         } else if (ccb->ccb_h.timeout == CAM_TIME_DEFAULT) {
1832                 io->timeout = OCS_CAM_IO_TIMEOUT;
1833         } else {
1834                 io->timeout = ccb->ccb_h.timeout;
1835         }
1836
1837         switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1838         case CAM_DIR_NONE:
1839                 rc = ocs_scsi_send_nodata_io(node, io, ccb_h->target_lun,
1840                                 ccb->ccb_h.flags & CAM_CDB_POINTER ? 
1841                                 csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
1842                                 csio->cdb_len,
1843                                 ocs_scsi_initiator_io_cb, ccb);
1844                 break;
1845         case CAM_DIR_IN:
1846                 rc = ocs_scsi_send_rd_io(node, io, ccb_h->target_lun,
1847                                 ccb->ccb_h.flags & CAM_CDB_POINTER ? 
1848                                 csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
1849                                 csio->cdb_len,
1850                                 NULL,
1851                                 sgl, sgl_count, csio->dxfer_len,
1852                                 ocs_scsi_initiator_io_cb, ccb);
1853                 break;
1854         case CAM_DIR_OUT:
1855                 rc = ocs_scsi_send_wr_io(node, io, ccb_h->target_lun,
1856                                 ccb->ccb_h.flags & CAM_CDB_POINTER ? 
1857                                 csio->cdb_io.cdb_ptr: csio->cdb_io.cdb_bytes,
1858                                 csio->cdb_len,
1859                                 NULL,
1860                                 sgl, sgl_count, csio->dxfer_len,
1861                                 ocs_scsi_initiator_io_cb, ccb);
1862                 break;
1863         default:
1864                 panic("%s invalid data direction %08x\n", __func__, 
1865                                                         ccb->ccb_h.flags);
1866                 break;
1867         }
1868
1869         return rc;
1870 }
1871
1872 static uint32_t
1873 ocs_fcp_change_role(struct ocs_softc *ocs, ocs_fcport *fcp, uint32_t new_role)
1874 {
1875
1876         uint32_t rc = 0, was = 0, i = 0;
1877         ocs_vport_spec_t *vport = fcp->vport;
1878
1879         for (was = 0, i = 0; i < (ocs->num_vports + 1); i++) {
1880                 if (FCPORT(ocs, i)->role != KNOB_ROLE_NONE)
1881                 was++;
1882         }
1883
1884         // Physical port        
1885         if ((was == 0) || (vport == NULL)) { 
1886                 fcp->role = new_role;
1887                 if (vport == NULL) {
1888                         ocs->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1889                         ocs->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1890                 } else {
1891                         vport->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1892                         vport->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1893                 }
1894
1895                 rc = ocs_xport_control(ocs->xport, OCS_XPORT_PORT_OFFLINE);
1896                 if (rc) {
1897                         ocs_log_debug(ocs, "port offline failed : %d\n", rc);
1898                 }
1899
1900                 rc = ocs_xport_control(ocs->xport, OCS_XPORT_PORT_ONLINE);
1901                 if (rc) {
1902                         ocs_log_debug(ocs, "port online failed : %d\n", rc);
1903                 }
1904                 
1905                 return 0;
1906         }
1907         
1908         if ((fcp->role != KNOB_ROLE_NONE)){
1909                 fcp->role = new_role;
1910                 vport->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1911                 vport->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1912                 /* New Sport will be created in sport deleted cb */
1913                 return ocs_sport_vport_del(ocs, ocs->domain, vport->wwpn, vport->wwnn);
1914         }
1915
1916         fcp->role = new_role;
1917         
1918         vport->enable_ini = (new_role & KNOB_ROLE_INITIATOR)? 1:0;
1919         vport->enable_tgt = (new_role & KNOB_ROLE_TARGET)? 1:0;
1920
1921         if (fcp->role != KNOB_ROLE_NONE) {
1922                 return ocs_sport_vport_alloc(ocs->domain, vport);
1923         }
1924
1925         return (0);
1926 }
1927
1928 /**
1929  * @ingroup cam_api
1930  * @brief Process CAM actions
1931  *
1932  * The driver supplies this routine to the CAM during intialization and
1933  * is the main entry point for processing CAM Control Blocks (CCB)
1934  *
1935  * @param sim pointer to the SCSI Interface Module
1936  * @param ccb CAM control block
1937  *
1938  * @todo
1939  *  - populate path inquiry data via info retrieved from SLI port
1940  */
1941 static void
1942 ocs_action(struct cam_sim *sim, union ccb *ccb)
1943 {
1944         struct ocs_softc *ocs = (struct ocs_softc *)cam_sim_softc(sim);
1945         struct ccb_hdr  *ccb_h = &ccb->ccb_h;
1946
1947         int32_t rc, bus;
1948         bus = cam_sim_bus(sim);
1949
1950         switch (ccb_h->func_code) {
1951         case XPT_SCSI_IO:
1952
1953                 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
1954                         if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
1955                                 ccb->ccb_h.status = CAM_REQ_INVALID;
1956                                 xpt_done(ccb);
1957                                 break;
1958                         }
1959                 }
1960
1961                 rc = ocs_initiator_io(ocs, ccb);
1962                 if (0 == rc) {
1963                         ocs_set_ccb_status(ccb, CAM_REQ_INPROG | CAM_SIM_QUEUED);
1964                         break;
1965                 } else {
1966                         if (rc == CAM_REQUEUE_REQ) {
1967                                 cam_freeze_devq(ccb->ccb_h.path);
1968                                 cam_release_devq(ccb->ccb_h.path, RELSIM_RELEASE_AFTER_TIMEOUT, 0, 100, 0);
1969                                 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1970                                 xpt_done(ccb);
1971                                 break;
1972                         }
1973
1974                         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1975                         if (rc > 0) {
1976                                 ocs_set_ccb_status(ccb, rc);
1977                         } else {
1978                                 ocs_set_ccb_status(ccb, CAM_SEL_TIMEOUT);
1979                         }
1980                 }
1981                 xpt_done(ccb);
1982                 break;
1983         case XPT_PATH_INQ:
1984         {
1985                 struct ccb_pathinq *cpi = &ccb->cpi;
1986                 struct ccb_pathinq_settings_fc *fc = &cpi->xport_specific.fc;
1987
1988                 uint64_t wwn = 0;
1989                 ocs_xport_stats_t value;
1990
1991                 cpi->version_num = 1;
1992
1993                 cpi->protocol = PROTO_SCSI;
1994                 cpi->protocol_version = SCSI_REV_SPC;
1995
1996                 if (ocs->ocs_xport == OCS_XPORT_FC) {
1997                         cpi->transport = XPORT_FC;
1998                 } else {
1999                         cpi->transport = XPORT_UNKNOWN;
2000                 }
2001
2002                 cpi->transport_version = 0;
2003
2004                 /* Set the transport parameters of the SIM */
2005                 ocs_xport_status(ocs->xport, OCS_XPORT_LINK_SPEED, &value);
2006                 fc->bitrate = value.value * 1000;       /* speed in Mbps */
2007
2008                 wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs, OCS_SCSI_WWPN));
2009                 fc->wwpn = be64toh(wwn);
2010
2011                 wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs, OCS_SCSI_WWNN));
2012                 fc->wwnn = be64toh(wwn);
2013
2014                 if (ocs->domain && ocs->domain->attached) {
2015                         fc->port = ocs->domain->sport->fc_id;
2016                 }
2017
2018                 if (ocs->config_tgt) {
2019                         cpi->target_sprt =
2020                                 PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
2021                 }
2022
2023                 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
2024                 cpi->hba_misc |= PIM_EXTLUNS | PIM_NOSCAN;
2025
2026                 cpi->hba_inquiry = PI_TAG_ABLE; 
2027                 cpi->max_target = OCS_MAX_TARGETS;
2028                 cpi->initiator_id = ocs->max_remote_nodes + 1;
2029
2030                 if (!ocs->enable_ini) {
2031                         cpi->hba_misc |= PIM_NOINITIATOR;
2032                 }
2033
2034                 cpi->max_lun = OCS_MAX_LUN;
2035                 cpi->bus_id = cam_sim_bus(sim);
2036
2037                 /* Need to supply a base transfer speed prior to linking up
2038                  * Worst case, this would be FC 1Gbps */
2039                 cpi->base_transfer_speed = 1 * 1000 * 1000;
2040
2041                 /* Calculate the max IO supported
2042                  * Worst case would be an OS page per SGL entry */
2043                 cpi->maxio = PAGE_SIZE * 
2044                         (ocs_scsi_get_property(ocs, OCS_SCSI_MAX_SGL) - 1);
2045
2046                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2047                 strncpy(cpi->hba_vid, "Emulex", HBA_IDLEN);
2048                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2049                 cpi->unit_number = cam_sim_unit(sim);
2050
2051                 cpi->ccb_h.status = CAM_REQ_CMP;
2052                 xpt_done(ccb);
2053                 break;
2054         }
2055         case XPT_GET_TRAN_SETTINGS:
2056         {
2057                 struct ccb_trans_settings *cts = &ccb->cts;
2058                 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
2059                 struct ccb_trans_settings_fc *fc = &cts->xport_specific.fc;
2060                 ocs_xport_stats_t value;
2061                 ocs_fcport *fcp = FCPORT(ocs, bus);
2062                 ocs_node_t      *fnode = NULL;
2063
2064                 if (ocs->ocs_xport != OCS_XPORT_FC) {
2065                         ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
2066                         xpt_done(ccb);
2067                         break;
2068                 }
2069
2070                 fnode = ocs_node_get_instance(ocs, fcp->tgt[cts->ccb_h.target_id].node_id);
2071                 if (fnode == NULL) {
2072                         ocs_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2073                         xpt_done(ccb);
2074                         break;
2075                 }
2076
2077                 cts->protocol = PROTO_SCSI;
2078                 cts->protocol_version = SCSI_REV_SPC2;
2079                 cts->transport = XPORT_FC;
2080                 cts->transport_version = 2;
2081
2082                 scsi->valid = CTS_SCSI_VALID_TQ;
2083                 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2084
2085                 /* speed in Mbps */
2086                 ocs_xport_status(ocs->xport, OCS_XPORT_LINK_SPEED, &value);
2087                 fc->bitrate = value.value * 100;
2088
2089                 fc->wwpn = ocs_node_get_wwpn(fnode);
2090
2091                 fc->wwnn = ocs_node_get_wwnn(fnode);
2092
2093                 fc->port = fnode->rnode.fc_id;
2094
2095                 fc->valid = CTS_FC_VALID_SPEED |
2096                         CTS_FC_VALID_WWPN |
2097                         CTS_FC_VALID_WWNN |
2098                         CTS_FC_VALID_PORT;
2099
2100                 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2101                 xpt_done(ccb);
2102                 break;
2103         }
2104         case XPT_SET_TRAN_SETTINGS:
2105                 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2106                 xpt_done(ccb);
2107                 break;
2108
2109         case XPT_CALC_GEOMETRY:
2110                 cam_calc_geometry(&ccb->ccg, TRUE);
2111                 xpt_done(ccb);
2112                 break;
2113
2114         case XPT_GET_SIM_KNOB:
2115         {
2116                 struct ccb_sim_knob *knob = &ccb->knob;
2117                 uint64_t wwn = 0;
2118                 ocs_fcport *fcp = FCPORT(ocs, bus);
2119
2120                 if (ocs->ocs_xport != OCS_XPORT_FC) {
2121                         ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
2122                         xpt_done(ccb);
2123                         break;
2124                 }
2125                 
2126                 if (bus == 0) {
2127                         wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs,
2128                                                 OCS_SCSI_WWNN));
2129                         knob->xport_specific.fc.wwnn = be64toh(wwn);
2130
2131                         wwn = *((uint64_t *)ocs_scsi_get_property_ptr(ocs,
2132                                                 OCS_SCSI_WWPN));
2133                         knob->xport_specific.fc.wwpn = be64toh(wwn);
2134                 } else {
2135                         knob->xport_specific.fc.wwnn = fcp->vport->wwnn;
2136                         knob->xport_specific.fc.wwpn = fcp->vport->wwpn;
2137                 }
2138
2139                 knob->xport_specific.fc.role = fcp->role;
2140                 knob->xport_specific.fc.valid = KNOB_VALID_ADDRESS |
2141                                                 KNOB_VALID_ROLE;
2142
2143                 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2144                 xpt_done(ccb);
2145                 break;
2146         }
2147         case XPT_SET_SIM_KNOB:
2148         {
2149                 struct ccb_sim_knob *knob = &ccb->knob;
2150                 bool role_changed = FALSE;
2151                 ocs_fcport *fcp = FCPORT(ocs, bus);
2152
2153                 if (ocs->ocs_xport != OCS_XPORT_FC) {
2154                         ocs_set_ccb_status(ccb, CAM_REQ_INVALID);
2155                         xpt_done(ccb);
2156                         break;
2157                 }
2158                         
2159                 if (knob->xport_specific.fc.valid & KNOB_VALID_ADDRESS) {
2160                         device_printf(ocs->dev, 
2161                                 "%s: XPT_SET_SIM_KNOB wwnn=%llx wwpn=%llx\n",
2162                                         __func__,
2163                                         (unsigned long long)knob->xport_specific.fc.wwnn,
2164                                         (unsigned long long)knob->xport_specific.fc.wwpn);
2165                 }
2166
2167                 if (knob->xport_specific.fc.valid & KNOB_VALID_ROLE) {
2168                         switch (knob->xport_specific.fc.role) {
2169                         case KNOB_ROLE_NONE:
2170                                 if (fcp->role != KNOB_ROLE_NONE) {
2171                                         role_changed = TRUE;
2172                                 }
2173                                 break;
2174                         case KNOB_ROLE_TARGET:
2175                                 if (fcp->role != KNOB_ROLE_TARGET) {
2176                                         role_changed = TRUE;
2177                                 }
2178                                 break;
2179                         case KNOB_ROLE_INITIATOR:
2180                                 if (fcp->role != KNOB_ROLE_INITIATOR) {
2181                                         role_changed = TRUE;
2182                                 }
2183                                 break;
2184                         case KNOB_ROLE_BOTH:
2185                                 if (fcp->role != KNOB_ROLE_BOTH) {
2186                                         role_changed = TRUE;
2187                                 }
2188                                 break;
2189                         default:
2190                                 device_printf(ocs->dev,
2191                                         "%s: XPT_SET_SIM_KNOB unsupported role: %d\n",
2192                                         __func__, knob->xport_specific.fc.role);
2193                         }
2194
2195                         if (role_changed) {
2196                                 device_printf(ocs->dev,
2197                                                 "BUS:%d XPT_SET_SIM_KNOB old_role: %d new_role: %d\n",
2198                                                 bus, fcp->role, knob->xport_specific.fc.role);
2199
2200                                 ocs_fcp_change_role(ocs, fcp, knob->xport_specific.fc.role);
2201                         }
2202                 }
2203
2204                 
2205
2206                 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2207                 xpt_done(ccb);
2208                 break;
2209         }
2210         case XPT_ABORT:
2211         {
2212                 union ccb *accb = ccb->cab.abort_ccb;
2213
2214                 switch (accb->ccb_h.func_code) {
2215                 case XPT_ACCEPT_TARGET_IO:
2216                         ocs_abort_atio(ocs, ccb);
2217                         break;
2218                 case XPT_IMMEDIATE_NOTIFY:
2219                         ocs_abort_inot(ocs, ccb);
2220                         break;
2221                 case XPT_SCSI_IO:
2222                         rc = ocs_abort_initiator_io(ocs, accb);
2223                         if (rc) {
2224                                 ccb->ccb_h.status = CAM_UA_ABORT;
2225                         } else {
2226                                 ccb->ccb_h.status = CAM_REQ_CMP;
2227                         }
2228
2229                         break;
2230                 default:
2231                         printf("abort of unknown func %#x\n",
2232                                         accb->ccb_h.func_code);
2233                         ccb->ccb_h.status = CAM_REQ_INVALID;
2234                         break;
2235                 }
2236                 break;
2237         }
2238         case XPT_RESET_BUS:
2239                 if (ocs_xport_control(ocs->xport, OCS_XPORT_PORT_OFFLINE) == 0) {
2240                         ocs_xport_control(ocs->xport, OCS_XPORT_PORT_ONLINE);
2241
2242                         ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2243                 } else {
2244                         ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2245                 }
2246                 xpt_done(ccb);
2247                 break;
2248         case XPT_RESET_DEV:
2249         {
2250                 ocs_node_t      *node = NULL;
2251                 ocs_io_t        *io = NULL;
2252                 int32_t         rc = 0;
2253                 ocs_fcport *fcp = FCPORT(ocs, bus);
2254
2255                 node = ocs_node_get_instance(ocs, fcp->tgt[ccb_h->target_id].node_id);
2256                 if (node == NULL) {
2257                         device_printf(ocs->dev, "%s: no device %d\n",
2258                                                 __func__, ccb_h->target_id);
2259                         ocs_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2260                         xpt_done(ccb);
2261                         break;
2262                 }
2263
2264                 io = ocs_scsi_io_alloc(node, OCS_SCSI_IO_ROLE_ORIGINATOR);
2265                 if (io == NULL) {
2266                         device_printf(ocs->dev, "%s: unable to alloc IO\n",
2267                                                                  __func__);
2268                         ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2269                         xpt_done(ccb);
2270                         break;
2271                 }
2272
2273                 rc = ocs_scsi_send_tmf(node, io, NULL, ccb_h->target_lun,
2274                                 OCS_SCSI_TMF_LOGICAL_UNIT_RESET,
2275                                 NULL, 0, 0,     /* sgl, sgl_count, length */
2276                                 ocs_initiator_tmf_cb, NULL/*arg*/);
2277
2278                 if (rc) {
2279                         ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2280                 } else {
2281                         ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2282                 }
2283                 
2284                 if (node->fcp2device) {
2285                         ocs_reset_crn(node, ccb_h->target_lun);
2286                 }
2287
2288                 xpt_done(ccb);
2289                 break;
2290         }
2291         case XPT_EN_LUN:        /* target support */
2292         {
2293                 ocs_tgt_resource_t *trsrc = NULL;
2294                 uint32_t        status = 0;
2295                 ocs_fcport *fcp = FCPORT(ocs, bus);
2296
2297                 device_printf(ocs->dev, "XPT_EN_LUN %sable %d:%d\n",
2298                                 ccb->cel.enable ? "en" : "dis",
2299                                 ccb->ccb_h.target_id,
2300                                 (unsigned int)ccb->ccb_h.target_lun);
2301
2302                 trsrc = ocs_tgt_resource_get(fcp, &ccb->ccb_h, &status);
2303                 if (trsrc) {
2304                         trsrc->enabled = ccb->cel.enable;
2305
2306                         /* Abort all ATIO/INOT on LUN disable */
2307                         if (trsrc->enabled == FALSE) {
2308                                 ocs_tgt_resource_abort(ocs, trsrc);
2309                         } else {
2310                                 STAILQ_INIT(&trsrc->atio);
2311                                 STAILQ_INIT(&trsrc->inot);
2312                         }
2313                         status = CAM_REQ_CMP;
2314                 }
2315
2316                 ocs_set_ccb_status(ccb, status);
2317                 xpt_done(ccb);
2318                 break;
2319         }
2320         /*
2321          * The flow of target IOs in CAM is:
2322          *  - CAM supplies a number of CCBs to the driver used for received
2323          *    commands.
2324          *  - when the driver receives a command, it copies the relevant
2325          *    information to the CCB and returns it to the CAM using xpt_done()
2326          *  - after the target server processes the request, it creates
2327          *    a new CCB containing information on how to continue the IO and 
2328          *    passes that to the driver
2329          *  - the driver processes the "continue IO" (a.k.a CTIO) CCB
2330          *  - once the IO completes, the driver returns the CTIO to the CAM 
2331          *    using xpt_done()
2332          */
2333         case XPT_ACCEPT_TARGET_IO:      /* used to inform upper layer of 
2334                                                 received CDB (a.k.a. ATIO) */
2335         case XPT_IMMEDIATE_NOTIFY:      /* used to inform upper layer of other
2336                                                          event (a.k.a. INOT) */
2337         {
2338                 ocs_tgt_resource_t *trsrc = NULL;
2339                 uint32_t        status = 0;
2340                 ocs_fcport *fcp = FCPORT(ocs, bus);
2341
2342                 /*printf("XPT_%s %p\n", ccb_h->func_code == XPT_ACCEPT_TARGET_IO ?
2343                                 "ACCEPT_TARGET_IO" : "IMMEDIATE_NOTIFY", ccb);*/
2344                 trsrc = ocs_tgt_resource_get(fcp, &ccb->ccb_h, &status);
2345                 if (trsrc == NULL) {
2346                         ocs_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2347                         xpt_done(ccb);
2348                         break;
2349                 }
2350
2351                 if (XPT_ACCEPT_TARGET_IO == ccb->ccb_h.func_code) {
2352                         struct ccb_accept_tio *atio = NULL;
2353
2354                         atio = (struct ccb_accept_tio *)ccb;
2355                         atio->init_id = 0x0badbeef;
2356                         atio->tag_id  = 0xdeadc0de;
2357
2358                         STAILQ_INSERT_TAIL(&trsrc->atio, &ccb->ccb_h, 
2359                                         sim_links.stqe);
2360                 } else {
2361                         STAILQ_INSERT_TAIL(&trsrc->inot, &ccb->ccb_h, 
2362                                         sim_links.stqe);
2363                 }
2364                 ccb->ccb_h.ccb_io_ptr  = NULL;
2365                 ccb->ccb_h.ccb_ocs_ptr = ocs;
2366                 ocs_set_ccb_status(ccb, CAM_REQ_INPROG);
2367                 /*
2368                  * These actions give resources to the target driver.
2369                  * If we didn't return here, this function would call
2370                  * xpt_done(), signaling to the upper layers that an
2371                  * IO or other event had arrived.
2372                  */
2373                 break;
2374         }
2375         case XPT_NOTIFY_ACKNOWLEDGE:
2376         {
2377                 ocs_io_t *io = NULL;
2378                 ocs_io_t *abortio = NULL;
2379
2380                 /* Get the IO reference for this tag */
2381                 io = ocs_scsi_find_io(ocs, ccb->cna2.tag_id);
2382                 if (io == NULL) {
2383                         device_printf(ocs->dev,
2384                                 "%s: XPT_NOTIFY_ACKNOWLEDGE no IO with tag %#x\n",
2385                                         __func__, ccb->cna2.tag_id);
2386                         ocs_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
2387                         xpt_done(ccb);
2388                         break;
2389                 }
2390
2391                 abortio = io->tgt_io.app;
2392                 if (abortio) {
2393                         abortio->tgt_io.flags &= ~OCS_CAM_IO_F_ABORT_NOTIFY;
2394                         device_printf(ocs->dev,
2395                                 "%s: XPT_NOTIFY_ACK state=%d tag=%#x xid=%#x"
2396                                 " flags=%#x\n", __func__, abortio->tgt_io.state,
2397                                 abortio->tag, abortio->init_task_tag,
2398                                         abortio->tgt_io.flags);
2399                         /* TMF response was sent in abort callback */
2400                 } else {
2401                         ocs_scsi_send_tmf_resp(io, 
2402                                         OCS_SCSI_TMF_FUNCTION_COMPLETE,
2403                                         NULL, ocs_target_tmf_cb, NULL);
2404                 }
2405
2406                 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2407                 xpt_done(ccb);
2408                 break;
2409         }
2410         case XPT_CONT_TARGET_IO:        /* continue target IO, sending data/response (a.k.a. CTIO) */
2411                 if (ocs_target_io(ocs, ccb)) {
2412                         device_printf(ocs->dev, 
2413                                 "XPT_CONT_TARGET_IO failed flags=%x tag=%#x\n",
2414                                 ccb->ccb_h.flags, ccb->csio.tag_id);
2415                         xpt_done(ccb);
2416                 }
2417                 break;
2418         default:
2419                 device_printf(ocs->dev, "unhandled func_code = %#x\n",
2420                                 ccb_h->func_code);
2421                 ccb_h->status = CAM_REQ_INVALID;
2422                 xpt_done(ccb);
2423                 break;
2424         }
2425 }
2426
2427 /**
2428  * @ingroup cam_api
2429  * @brief Process events
2430  *
2431  * @param sim pointer to the SCSI Interface Module
2432  *
2433  */
2434 static void
2435 ocs_poll(struct cam_sim *sim)
2436 {
2437         printf("%s\n", __func__);
2438 }
2439
2440 static int32_t
2441 ocs_initiator_tmf_cb(ocs_io_t *io, ocs_scsi_io_status_e scsi_status,
2442                 ocs_scsi_cmd_resp_t *rsp, uint32_t flags, void *arg)
2443 {
2444         int32_t rc = 0;
2445
2446         switch (scsi_status) {
2447         case OCS_SCSI_STATUS_GOOD:
2448         case OCS_SCSI_STATUS_NO_IO:
2449                 break;
2450         case OCS_SCSI_STATUS_CHECK_RESPONSE:
2451                 if (rsp->response_data_length == 0) {
2452                         ocs_log_test(io->ocs, "check response without data?!?\n");
2453                         rc = -1;
2454                         break;
2455                 }
2456
2457                 if (rsp->response_data[3] != 0) {
2458                         ocs_log_test(io->ocs, "TMF status %08x\n",
2459                                 be32toh(*((uint32_t *)rsp->response_data)));
2460                         rc = -1;
2461                         break;
2462                 }
2463                 break;
2464         default:
2465                 ocs_log_test(io->ocs, "status=%#x\n", scsi_status);
2466                 rc = -1;
2467         }
2468
2469         ocs_scsi_io_free(io);
2470
2471         return rc;
2472 }
2473
2474 /**
2475  * @brief lookup target resource structure
2476  *
2477  * Arbitrarily support
2478  *  - wildcard target ID + LU
2479  *  - 0 target ID + non-wildcard LU
2480  *
2481  * @param ocs the driver instance's software context
2482  * @param ccb_h pointer to the CCB header
2483  * @param status returned status value
2484  *
2485  * @return pointer to the target resource, NULL if none available (e.g. if LU
2486  *         is not enabled)
2487  */
2488 static ocs_tgt_resource_t *ocs_tgt_resource_get(ocs_fcport *fcp, 
2489                                 struct ccb_hdr *ccb_h, uint32_t *status)
2490 {
2491         target_id_t     tid = ccb_h->target_id;
2492         lun_id_t        lun = ccb_h->target_lun;
2493
2494         if (CAM_TARGET_WILDCARD == tid) {
2495                 if (CAM_LUN_WILDCARD != lun) {
2496                         *status = CAM_LUN_INVALID;
2497                         return NULL;
2498                 }
2499                 return &fcp->targ_rsrc_wildcard;
2500         } else {
2501                 if (lun < OCS_MAX_LUN) {
2502                         return &fcp->targ_rsrc[lun];
2503                 } else {
2504                         *status = CAM_LUN_INVALID;
2505                         return NULL;
2506                 }
2507         } 
2508
2509 }
2510
2511 static int32_t
2512 ocs_tgt_resource_abort(struct ocs_softc *ocs, ocs_tgt_resource_t *trsrc)
2513 {
2514         union ccb *ccb = NULL;
2515         uint32_t        count;
2516
2517         count = 0;
2518         do {
2519                 ccb = (union ccb *)STAILQ_FIRST(&trsrc->atio);
2520                 if (ccb) {
2521                         STAILQ_REMOVE_HEAD(&trsrc->atio, sim_links.stqe);
2522                         ccb->ccb_h.status = CAM_REQ_ABORTED;
2523                         xpt_done(ccb);
2524                         count++;
2525                 }
2526         } while (ccb);
2527
2528         count = 0;
2529         do {
2530                 ccb = (union ccb *)STAILQ_FIRST(&trsrc->inot);
2531                 if (ccb) {
2532                         STAILQ_REMOVE_HEAD(&trsrc->inot, sim_links.stqe);
2533                         ccb->ccb_h.status = CAM_REQ_ABORTED;
2534                         xpt_done(ccb);
2535                         count++;
2536                 }
2537         } while (ccb);
2538
2539         return 0;
2540 }
2541
2542 static void
2543 ocs_abort_atio(struct ocs_softc *ocs, union ccb *ccb)
2544 {
2545
2546         ocs_io_t        *aio = NULL;
2547         ocs_tgt_resource_t *trsrc = NULL;
2548         uint32_t        status = CAM_REQ_INVALID;
2549         struct ccb_hdr *cur = NULL;
2550         union ccb *accb = ccb->cab.abort_ccb;
2551
2552         int bus = cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path));
2553         ocs_fcport *fcp = FCPORT(ocs, bus); 
2554
2555         trsrc = ocs_tgt_resource_get(fcp, &accb->ccb_h, &status);
2556         if (trsrc != NULL) {
2557                 STAILQ_FOREACH(cur, &trsrc->atio, sim_links.stqe) {
2558                         if (cur != &accb->ccb_h) 
2559                                 continue;
2560
2561                         STAILQ_REMOVE(&trsrc->atio, cur, ccb_hdr,
2562                                                         sim_links.stqe);
2563                         accb->ccb_h.status = CAM_REQ_ABORTED;
2564                         xpt_done(accb);
2565                         ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2566                         return;
2567                 }
2568         }
2569
2570         /* if the ATIO has a valid IO pointer, CAM is telling
2571          * the driver that the ATIO (which represents the entire
2572          * exchange) has been aborted. */
2573
2574         aio = accb->ccb_h.ccb_io_ptr;
2575         if (aio == NULL) {
2576                 ccb->ccb_h.status = CAM_UA_ABORT;
2577                 return;
2578         }
2579
2580         device_printf(ocs->dev,
2581                         "%s: XPT_ABORT ATIO state=%d tag=%#x"
2582                         " xid=%#x flags=%#x\n", __func__, 
2583                         aio->tgt_io.state, aio->tag, 
2584                         aio->init_task_tag, aio->tgt_io.flags);
2585         /* Expectations are:
2586          *  - abort task was received
2587          *  - already aborted IO in the DEVICE
2588          *  - already received NOTIFY ACKNOWLEDGE */
2589
2590         if ((aio->tgt_io.flags & OCS_CAM_IO_F_ABORT_RECV) == 0) {
2591                 device_printf(ocs->dev, "%s: abort not received or io completed \n", __func__);
2592                 ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2593                 return;
2594         }
2595
2596         aio->tgt_io.flags |= OCS_CAM_IO_F_ABORT_CAM;
2597         ocs_target_io_free(aio);
2598         ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2599         
2600         return;
2601 }
2602
2603 static void
2604 ocs_abort_inot(struct ocs_softc *ocs, union ccb *ccb)
2605 {
2606         ocs_tgt_resource_t *trsrc = NULL;
2607         uint32_t        status = CAM_REQ_INVALID;
2608         struct ccb_hdr *cur = NULL;
2609         union ccb *accb = ccb->cab.abort_ccb;
2610
2611         int bus = cam_sim_bus(xpt_path_sim((ccb)->ccb_h.path));
2612         ocs_fcport *fcp = FCPORT(ocs, bus); 
2613
2614         trsrc = ocs_tgt_resource_get(fcp, &accb->ccb_h, &status);
2615         if (trsrc) {
2616                 STAILQ_FOREACH(cur, &trsrc->inot, sim_links.stqe) {
2617                         if (cur != &accb->ccb_h) 
2618                                 continue;
2619
2620                         STAILQ_REMOVE(&trsrc->inot, cur, ccb_hdr,
2621                                                         sim_links.stqe);
2622                         accb->ccb_h.status = CAM_REQ_ABORTED;
2623                         xpt_done(accb);
2624                         ocs_set_ccb_status(ccb, CAM_REQ_CMP);
2625                         return;
2626                 }
2627         }
2628
2629         ocs_set_ccb_status(ccb, CAM_UA_ABORT);
2630         return;
2631 }
2632
2633 static uint32_t
2634 ocs_abort_initiator_io(struct ocs_softc *ocs, union ccb *accb)
2635 {
2636
2637         ocs_node_t      *node = NULL;
2638         ocs_io_t        *io = NULL;
2639         int32_t         rc = 0;
2640         struct ccb_scsiio *csio = &accb->csio;
2641
2642         ocs_fcport *fcp = FCPORT(ocs, cam_sim_bus(xpt_path_sim((accb)->ccb_h.path)));
2643         node = ocs_node_get_instance(ocs, fcp->tgt[accb->ccb_h.target_id].node_id);
2644         if (node == NULL) {
2645                 device_printf(ocs->dev, "%s: no device %d\n", 
2646                                 __func__, accb->ccb_h.target_id);
2647                 ocs_set_ccb_status(accb, CAM_DEV_NOT_THERE);
2648                 xpt_done(accb);
2649                 return (-1);
2650         }
2651
2652         io = ocs_scsi_io_alloc(node, OCS_SCSI_IO_ROLE_ORIGINATOR);
2653         if (io == NULL) {
2654                 device_printf(ocs->dev,
2655                                 "%s: unable to alloc IO\n", __func__);
2656                 ocs_set_ccb_status(accb, CAM_REQ_CMP_ERR);
2657                 xpt_done(accb);
2658                 return (-1);
2659         }
2660
2661         rc = ocs_scsi_send_tmf(node, io, 
2662                         (ocs_io_t *)csio->ccb_h.ccb_io_ptr,
2663                         accb->ccb_h.target_lun,
2664                         OCS_SCSI_TMF_ABORT_TASK,
2665                         NULL, 0, 0,
2666                         ocs_initiator_tmf_cb, NULL/*arg*/);
2667
2668         return rc;
2669 }
2670
2671 void
2672 ocs_scsi_ini_ddump(ocs_textbuf_t *textbuf, ocs_scsi_ddump_type_e type, void *obj)
2673 {
2674         switch(type) {
2675         case OCS_SCSI_DDUMP_DEVICE: {
2676                 //ocs_t *ocs = obj;
2677                 break;
2678         }
2679         case OCS_SCSI_DDUMP_DOMAIN: {
2680                 //ocs_domain_t *domain = obj;
2681                 break;
2682         }
2683         case OCS_SCSI_DDUMP_SPORT: {
2684                 //ocs_sport_t *sport = obj;
2685                 break;
2686         }
2687         case OCS_SCSI_DDUMP_NODE: {
2688                 //ocs_node_t *node = obj;
2689                 break;
2690         }
2691         case OCS_SCSI_DDUMP_IO: {
2692                 //ocs_io_t *io = obj;
2693                 break;
2694         }
2695         default: {
2696                 break;
2697         }
2698         }
2699 }
2700
2701 void
2702 ocs_scsi_tgt_ddump(ocs_textbuf_t *textbuf, ocs_scsi_ddump_type_e type, void *obj)
2703 {
2704         switch(type) {
2705         case OCS_SCSI_DDUMP_DEVICE: {
2706                 //ocs_t *ocs = obj;
2707                 break;
2708         }
2709         case OCS_SCSI_DDUMP_DOMAIN: {
2710                 //ocs_domain_t *domain = obj;
2711                 break;
2712         }
2713         case OCS_SCSI_DDUMP_SPORT: {
2714                 //ocs_sport_t *sport = obj;
2715                 break;
2716         }
2717         case OCS_SCSI_DDUMP_NODE: {
2718                 //ocs_node_t *node = obj;
2719                 break;
2720         }
2721         case OCS_SCSI_DDUMP_IO: {
2722                 ocs_io_t *io = obj;
2723                 char *state_str = NULL;
2724
2725                 switch (io->tgt_io.state) {
2726                 case OCS_CAM_IO_FREE:
2727                         state_str = "FREE";
2728                         break;
2729                 case OCS_CAM_IO_COMMAND:
2730                         state_str = "COMMAND";
2731                         break;
2732                 case OCS_CAM_IO_DATA:
2733                         state_str = "DATA";
2734                         break;
2735                 case OCS_CAM_IO_DATA_DONE:
2736                         state_str = "DATA_DONE";
2737                         break;
2738                 case OCS_CAM_IO_RESP:
2739                         state_str = "RESP";
2740                         break;
2741                 default:
2742                         state_str = "xxx BAD xxx";
2743                 }
2744                 ocs_ddump_value(textbuf, "cam_st", "%s", state_str);
2745                 if (io->tgt_io.app) {
2746                         ocs_ddump_value(textbuf, "cam_flags", "%#x",
2747                                 ((union ccb *)(io->tgt_io.app))->ccb_h.flags);
2748                         ocs_ddump_value(textbuf, "cam_status", "%#x",
2749                                 ((union ccb *)(io->tgt_io.app))->ccb_h.status);
2750                 }
2751
2752                 break;
2753         }
2754         default: {
2755                 break;
2756         }
2757         }
2758 }
2759
2760 int32_t ocs_scsi_get_block_vaddr(ocs_io_t *io, uint64_t blocknumber,
2761                                 ocs_scsi_vaddr_len_t addrlen[],
2762                                 uint32_t max_addrlen, void **dif_vaddr)
2763 {
2764         return -1;
2765 }
2766
2767 uint32_t
2768 ocs_get_crn(ocs_node_t *node, uint8_t *crn, uint64_t lun)
2769 {
2770         uint32_t idx;
2771         struct ocs_lun_crn *lcrn = NULL;
2772         idx = lun % OCS_MAX_LUN;
2773
2774         lcrn = node->ini_node.lun_crn[idx];
2775
2776         if (lcrn == NULL) {
2777                 lcrn = ocs_malloc(node->ocs, sizeof(struct ocs_lun_crn),
2778                                         M_ZERO|M_NOWAIT);
2779                 if (lcrn == NULL) {
2780                         return (1);
2781                 }
2782                 
2783                 lcrn->lun = lun;
2784                 node->ini_node.lun_crn[idx] = lcrn;
2785         }
2786
2787         if (lcrn->lun != lun) {
2788                 return (1);
2789         }       
2790         
2791         if (lcrn->crnseed == 0)
2792                 lcrn->crnseed = 1;
2793
2794         *crn = lcrn->crnseed++;
2795         return (0);
2796 }
2797
2798 void
2799 ocs_del_crn(ocs_node_t *node)
2800 {
2801         uint32_t i;
2802         struct ocs_lun_crn *lcrn = NULL;
2803         
2804         for(i = 0; i < OCS_MAX_LUN; i++) {
2805                 lcrn = node->ini_node.lun_crn[i];
2806                 if (lcrn) {
2807                         ocs_free(node->ocs, lcrn, sizeof(*lcrn));
2808                 }
2809         }
2810
2811         return;
2812 }
2813
2814 void
2815 ocs_reset_crn(ocs_node_t *node, uint64_t lun)
2816 {
2817         uint32_t idx;
2818         struct ocs_lun_crn *lcrn = NULL;
2819         idx = lun % OCS_MAX_LUN;
2820
2821         lcrn = node->ini_node.lun_crn[idx];
2822         if (lcrn)
2823                 lcrn->crnseed = 0;
2824
2825         return;
2826 }