]> Git Repo - linux.git/commitdiff
Merge branch 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target...
authorLinus Torvalds <[email protected]>
Thu, 21 Jan 2016 01:20:53 +0000 (17:20 -0800)
committerLinus Torvalds <[email protected]>
Thu, 21 Jan 2016 01:20:53 +0000 (17:20 -0800)
Pull SCSI target updates from Nicholas Bellinger:
 "The highlights this round include:

   - Introduce configfs support for unlocked configfs_depend_item()
     (krzysztof + andrezej)
   - Conversion of usb-gadget target driver to new function registration
     interface (andrzej + sebastian)
   - Enable qla2xxx FC target mode support for Extended Logins (himansu +
     giridhar)
   - Enable qla2xxx FC target mode support for Exchange Offload (himansu +
     giridhar)
   - Add qla2xxx FC target mode irq affinity notification + selective
     command queuing.  (quinn + himanshu)
   - Fix iscsi-target deadlock in se_node_acl configfs deletion (sagi +
     nab)
   - Convert se_node_acl configfs deletion + se_node_acl->queue_depth to
     proper se_session->sess_kref + target_get_session() usage.  (hch +
     sagi + nab)
   - Fix long-standing race between se_node_acl->acl_kref get and
     get_initiator_node_acl() lookup.  (hch + nab)
   - Fix target/user block-size handling, and make sure netlink reaches
     all network namespaces (sheng + andy)

  Note there is an outstanding bug-fix series for remote I_T nexus port
  TMR LUN_RESET has been posted and still being tested, and will likely
  become post -rc1 material at this point"

* 'for-next' of git://git.kernel.org/pub/scm/linux/kernel/git/nab/target-pending: (56 commits)
  scsi: qla2xxxx: avoid type mismatch in comparison
  target/user: Make sure netlink would reach all network namespaces
  target: Obtain se_node_acl->acl_kref during get_initiator_node_acl
  target: Convert ACL change queue_depth se_session reference usage
  iscsi-target: Fix potential dead-lock during node acl delete
  ib_srpt: Convert acl lookup to modern get_initiator_node_acl usage
  tcm_fc: Convert acl lookup to modern get_initiator_node_acl usage
  tcm_fc: Wait for command completion before freeing a session
  target: Fix a memory leak in target_dev_lba_map_store()
  target: Support aborting tasks with a 64-bit tag
  usb/gadget: Remove set-but-not-used variables
  target: Remove an unused variable
  target: Fix indentation in target_core_configfs.c
  target/user: Allow user to set block size before enabling device
  iser-target: Fix non negative ERR_PTR isert_device_get usage
  target/fcoe: Add tag support to tcm_fc
  qla2xxx: Check for online flag instead of active reset when transmitting responses
  qla2xxx: Set all queues to 4k
  qla2xxx: Disable ZIO at start time.
  qla2xxx: Move atioq to a different lock to reduce lock contention
  ...

14 files changed:
1  2 
drivers/infiniband/ulp/isert/ib_isert.c
drivers/scsi/qla2xxx/qla_os.c
drivers/scsi/qla2xxx/tcm_qla2xxx.c
drivers/target/iscsi/iscsi_target.c
drivers/target/iscsi/iscsi_target_parameters.c
drivers/target/target_core_sbc.c
drivers/target/target_core_tmr.c
drivers/target/target_core_transport.c
drivers/target/target_core_user.c
drivers/usb/gadget/Kconfig
drivers/usb/gadget/legacy/tcm_usb_gadget.c
fs/configfs/dir.c
include/linux/configfs.h
include/target/target_core_base.h

index 8a51c3b5d657e5a49ae50c4e5f104a7298d732f2,ae84de12e70e73dc9d780278a19e59fb5a0cb9f1..468c5e13256368c8b23b5ab25c903ead808f490f
@@@ -157,9 -157,16 +157,9 @@@ isert_create_qp(struct isert_conn *iser
        attr.recv_cq = comp->cq;
        attr.cap.max_send_wr = ISERT_QP_MAX_REQ_DTOS;
        attr.cap.max_recv_wr = ISERT_QP_MAX_RECV_DTOS + 1;
 -      /*
 -       * FIXME: Use devattr.max_sge - 2 for max_send_sge as
 -       * work-around for RDMA_READs with ConnectX-2.
 -       *
 -       * Also, still make sure to have at least two SGEs for
 -       * outgoing control PDU responses.
 -       */
 -      attr.cap.max_send_sge = max(2, device->dev_attr.max_sge - 2);
 -      isert_conn->max_sge = attr.cap.max_send_sge;
 -
 +      attr.cap.max_send_sge = device->dev_attr.max_sge;
 +      isert_conn->max_sge = min(device->dev_attr.max_sge,
 +                                device->dev_attr.max_sge_rd);
        attr.cap.max_recv_sge = 1;
        attr.sq_sig_type = IB_SIGNAL_REQ_WR;
        attr.qp_type = IB_QPT_RC;
@@@ -350,7 -357,7 +350,7 @@@ isert_create_device_ib_res(struct isert
        dev_attr = &device->dev_attr;
        ret = isert_query_device(device->ib_device, dev_attr);
        if (ret)
-               return ret;
+               goto out;
  
        /* asign function handlers */
        if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS &&
  
        ret = isert_alloc_comps(device, dev_attr);
        if (ret)
-               return ret;
+               goto out;
  
        device->pd = ib_alloc_pd(device->ib_device);
        if (IS_ERR(device->pd)) {
  
  out_cq:
        isert_free_comps(device);
+ out:
+       if (ret > 0)
+               ret = -EINVAL;
        return ret;
  }
  
index 6be32fdab365bd5258d1ea57b342a359fe968348,3758a00c30d99c90d553a36542bfd98daf87b1df..f1788db4319560c8210eda5a0fc4f576d5ec321f
@@@ -221,6 -221,18 +221,18 @@@ MODULE_PARM_DESC(ql2xmdenable
                "0 - MiniDump disabled. "
                "1 (Default) - MiniDump enabled.");
  
+ int ql2xexlogins = 0;
+ module_param(ql2xexlogins, uint, S_IRUGO|S_IWUSR);
+ MODULE_PARM_DESC(ql2xexlogins,
+                "Number of extended Logins. "
+                "0 (Default)- Disabled.");
+ int ql2xexchoffld = 0;
+ module_param(ql2xexchoffld, uint, S_IRUGO|S_IWUSR);
+ MODULE_PARM_DESC(ql2xexchoffld,
+                "Number of exchanges to offload. "
+                "0 (Default)- Disabled.");
  /*
   * SCSI host template entry points
   */
@@@ -2324,6 -2336,9 +2336,9 @@@ qla2x00_probe_one(struct pci_dev *pdev
        ha->tgt.enable_class_2 = ql2xenableclass2;
        INIT_LIST_HEAD(&ha->tgt.q_full_list);
        spin_lock_init(&ha->tgt.q_full_lock);
+       spin_lock_init(&ha->tgt.sess_lock);
+       spin_lock_init(&ha->tgt.atio_lock);
  
        /* Clear our data area */
        ha->bars = bars;
                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
                ha->mbx_count = MAILBOX_REGISTER_COUNT;
                req_length = REQUEST_ENTRY_CNT_83XX;
-               rsp_length = RESPONSE_ENTRY_CNT_2300;
+               rsp_length = RESPONSE_ENTRY_CNT_83XX;
                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
                ha->portnum = PCI_FUNC(ha->pdev->devfn);
                ha->max_fibre_devices = MAX_FIBRE_DEVICES_2400;
                ha->mbx_count = MAILBOX_REGISTER_COUNT;
-               req_length = REQUEST_ENTRY_CNT_24XX;
-               rsp_length = RESPONSE_ENTRY_CNT_2300;
+               req_length = REQUEST_ENTRY_CNT_83XX;
+               rsp_length = RESPONSE_ENTRY_CNT_83XX;
                ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
                ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
                ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
@@@ -3128,6 -3143,14 +3143,14 @@@ qla2x00_remove_one(struct pci_dev *pdev
  
        base_vha->flags.online = 0;
  
+       /* free DMA memory */
+       if (ha->exlogin_buf)
+               qla2x00_free_exlogin_buffer(ha);
+       /* free DMA memory */
+       if (ha->exchoffld_buf)
+               qla2x00_free_exchoffld_buffer(ha);
        qla2x00_destroy_deferred_work(ha);
  
        qlt_remove_target(ha, base_vha);
        return -ENOMEM;
  }
  
+ int
+ qla2x00_set_exlogins_buffer(scsi_qla_host_t *vha)
+ {
+       int rval;
+       uint16_t        size, max_cnt, temp;
+       struct qla_hw_data *ha = vha->hw;
+       /* Return if we don't need to alloacate any extended logins */
+       if (!ql2xexlogins)
+               return QLA_SUCCESS;
+       ql_log(ql_log_info, vha, 0xd021, "EXLOGIN count: %d.\n", ql2xexlogins);
+       max_cnt = 0;
+       rval = qla_get_exlogin_status(vha, &size, &max_cnt);
+       if (rval != QLA_SUCCESS) {
+               ql_log_pci(ql_log_fatal, ha->pdev, 0xd029,
+                   "Failed to get exlogin status.\n");
+               return rval;
+       }
+       temp = (ql2xexlogins > max_cnt) ? max_cnt : ql2xexlogins;
+       ha->exlogin_size = (size * temp);
+       ql_log(ql_log_info, vha, 0xd024,
+               "EXLOGIN: max_logins=%d, portdb=0x%x, total=%d.\n",
+               max_cnt, size, temp);
+       ql_log(ql_log_info, vha, 0xd025, "EXLOGIN: requested size=0x%x\n",
+               ha->exlogin_size);
+       /* Get consistent memory for extended logins */
+       ha->exlogin_buf = dma_alloc_coherent(&ha->pdev->dev,
+           ha->exlogin_size, &ha->exlogin_buf_dma, GFP_KERNEL);
+       if (!ha->exlogin_buf) {
+               ql_log_pci(ql_log_fatal, ha->pdev, 0xd02a,
+                   "Failed to allocate memory for exlogin_buf_dma.\n");
+               return -ENOMEM;
+       }
+       /* Now configure the dma buffer */
+       rval = qla_set_exlogin_mem_cfg(vha, ha->exlogin_buf_dma);
+       if (rval) {
+               ql_log(ql_log_fatal, vha, 0x00cf,
+                   "Setup extended login buffer  ****FAILED****.\n");
+               qla2x00_free_exlogin_buffer(ha);
+       }
+       return rval;
+ }
+ /*
+ * qla2x00_free_exlogin_buffer
+ *
+ * Input:
+ *     ha = adapter block pointer
+ */
+ void
+ qla2x00_free_exlogin_buffer(struct qla_hw_data *ha)
+ {
+       if (ha->exlogin_buf) {
+               dma_free_coherent(&ha->pdev->dev, ha->exlogin_size,
+                   ha->exlogin_buf, ha->exlogin_buf_dma);
+               ha->exlogin_buf = NULL;
+               ha->exlogin_size = 0;
+       }
+ }
+ int
+ qla2x00_set_exchoffld_buffer(scsi_qla_host_t *vha)
+ {
+       int rval;
+       uint16_t        size, max_cnt, temp;
+       struct qla_hw_data *ha = vha->hw;
+       /* Return if we don't need to alloacate any extended logins */
+       if (!ql2xexchoffld)
+               return QLA_SUCCESS;
+       ql_log(ql_log_info, vha, 0xd014,
+           "Exchange offload count: %d.\n", ql2xexlogins);
+       max_cnt = 0;
+       rval = qla_get_exchoffld_status(vha, &size, &max_cnt);
+       if (rval != QLA_SUCCESS) {
+               ql_log_pci(ql_log_fatal, ha->pdev, 0xd012,
+                   "Failed to get exlogin status.\n");
+               return rval;
+       }
+       temp = (ql2xexchoffld > max_cnt) ? max_cnt : ql2xexchoffld;
+       ha->exchoffld_size = (size * temp);
+       ql_log(ql_log_info, vha, 0xd016,
+               "Exchange offload: max_count=%d, buffers=0x%x, total=%d.\n",
+               max_cnt, size, temp);
+       ql_log(ql_log_info, vha, 0xd017,
+           "Exchange Buffers requested size = 0x%x\n", ha->exchoffld_size);
+       /* Get consistent memory for extended logins */
+       ha->exchoffld_buf = dma_alloc_coherent(&ha->pdev->dev,
+           ha->exchoffld_size, &ha->exchoffld_buf_dma, GFP_KERNEL);
+       if (!ha->exchoffld_buf) {
+               ql_log_pci(ql_log_fatal, ha->pdev, 0xd013,
+                   "Failed to allocate memory for exchoffld_buf_dma.\n");
+               return -ENOMEM;
+       }
+       /* Now configure the dma buffer */
+       rval = qla_set_exchoffld_mem_cfg(vha, ha->exchoffld_buf_dma);
+       if (rval) {
+               ql_log(ql_log_fatal, vha, 0xd02e,
+                   "Setup exchange offload buffer ****FAILED****.\n");
+               qla2x00_free_exchoffld_buffer(ha);
+       }
+       return rval;
+ }
+ /*
+ * qla2x00_free_exchoffld_buffer
+ *
+ * Input:
+ *     ha = adapter block pointer
+ */
+ void
+ qla2x00_free_exchoffld_buffer(struct qla_hw_data *ha)
+ {
+       if (ha->exchoffld_buf) {
+               dma_free_coherent(&ha->pdev->dev, ha->exchoffld_size,
+                   ha->exchoffld_buf, ha->exchoffld_buf_dma);
+               ha->exchoffld_buf = NULL;
+               ha->exchoffld_size = 0;
+       }
+ }
  /*
  * qla2x00_free_fw_dump
  *     Frees fw dump stuff.
@@@ -3766,6 -3923,8 +3923,8 @@@ struct scsi_qla_host *qla2x00_create_ho
        INIT_LIST_HEAD(&vha->list);
        INIT_LIST_HEAD(&vha->qla_cmd_list);
        INIT_LIST_HEAD(&vha->qla_sess_op_cmd_list);
+       INIT_LIST_HEAD(&vha->logo_list);
+       INIT_LIST_HEAD(&vha->plogi_ack_list);
  
        spin_lock_init(&vha->work_lock);
        spin_lock_init(&vha->cmd_list_lock);
@@@ -5843,3 -6002,6 +6002,3 @@@ MODULE_FIRMWARE(FW_FILE_ISP2300)
  MODULE_FIRMWARE(FW_FILE_ISP2322);
  MODULE_FIRMWARE(FW_FILE_ISP24XX);
  MODULE_FIRMWARE(FW_FILE_ISP25XX);
 -MODULE_FIRMWARE(FW_FILE_ISP2031);
 -MODULE_FIRMWARE(FW_FILE_ISP8031);
 -MODULE_FIRMWARE(FW_FILE_ISP27XX);
index 81af294f15a7d4deb557b162dc51ca676df9618a,b44f397821ef2b153fd824f2a70fa1a34031f099..faf0a126627f65d6cbe275c1ea4b34062a579a7a
@@@ -284,6 -284,7 +284,7 @@@ static void tcm_qla2xxx_complete_free(s
  
        WARN_ON(cmd->cmd_flags &  BIT_16);
  
+       cmd->vha->tgt_counters.qla_core_ret_sta_ctio++;
        cmd->cmd_flags |= BIT_16;
        transport_generic_free_cmd(&cmd->se_cmd, 0);
  }
   */
  static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
  {
+       cmd->vha->tgt_counters.core_qla_free_cmd++;
        cmd->cmd_in_wq = 1;
        INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
-       queue_work(tcm_qla2xxx_free_wq, &cmd->work);
+       queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
  }
  
  /*
@@@ -342,9 -344,9 +344,9 @@@ static int tcm_qla2xxx_shutdown_session
        BUG_ON(!sess);
        vha = sess->vha;
  
-       spin_lock_irqsave(&vha->hw->hardware_lock, flags);
+       spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
        target_sess_cmd_list_set_waiting(se_sess);
-       spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
+       spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
  
        return 1;
  }
@@@ -358,9 -360,9 +360,9 @@@ static void tcm_qla2xxx_close_session(s
        BUG_ON(!sess);
        vha = sess->vha;
  
-       spin_lock_irqsave(&vha->hw->hardware_lock, flags);
+       spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
        qlt_unreg_sess(sess);
-       spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
+       spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
  }
  
  static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
@@@ -454,6 -456,7 +456,7 @@@ static int tcm_qla2xxx_handle_cmd(scsi_
                return -EINVAL;
        }
  
+       cmd->vha->tgt_counters.qla_core_sbt_cmd++;
        return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
                                cmd->unpacked_lun, data_length, fcp_task_attr,
                                data_dir, flags);
@@@ -469,6 -472,7 +472,7 @@@ static void tcm_qla2xxx_handle_data_wor
         */
        cmd->cmd_in_wq = 0;
        cmd->cmd_flags |= BIT_11;
+       cmd->vha->tgt_counters.qla_core_ret_ctio++;
        if (!cmd->write_data_transferred) {
                /*
                 * Check if se_cmd has already been aborted via LUN_RESET, and
@@@ -500,7 -504,7 +504,7 @@@ static void tcm_qla2xxx_handle_data(str
        cmd->cmd_flags |= BIT_10;
        cmd->cmd_in_wq = 1;
        INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
-       queue_work(tcm_qla2xxx_free_wq, &cmd->work);
+       queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
  }
  
  static void tcm_qla2xxx_handle_dif_work(struct work_struct *work)
@@@ -643,7 -647,7 +647,7 @@@ static void tcm_qla2xxx_aborted_task(st
  static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
                        struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
  /*
-  * Expected to be called with struct qla_hw_data->hardware_lock held
+  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
   */
  static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
  {
@@@ -697,13 -701,13 +701,13 @@@ static void tcm_qla2xxx_put_sess(struc
        if (!sess)
                return;
  
-       assert_spin_locked(&sess->vha->hw->hardware_lock);
+       assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
        kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
  }
  
  static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
  {
-       assert_spin_locked(&sess->vha->hw->hardware_lock);
+       assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
        target_sess_cmd_list_set_waiting(sess->se_sess);
  }
  
@@@ -902,7 -906,7 +906,7 @@@ static ssize_t tcm_qla2xxx_tpg_fabric_p
        return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
  }
  
 -CONFIGFS_ATTR_WO(tcm_qla2xxx_tpg_, enable);
 +CONFIGFS_ATTR(tcm_qla2xxx_tpg_, enable);
  CONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions);
  CONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type);
  
@@@ -1077,7 -1081,7 +1081,7 @@@ static struct se_portal_group *tcm_qla2
  }
  
  /*
-  * Expected to be called with struct qla_hw_data->hardware_lock held
+  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
   */
  static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
        scsi_qla_host_t *vha,
  }
  
  /*
-  * Expected to be called with struct qla_hw_data->hardware_lock held
+  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
   */
  static void tcm_qla2xxx_set_sess_by_s_id(
        struct tcm_qla2xxx_lport *lport,
  }
  
  /*
-  * Expected to be called with struct qla_hw_data->hardware_lock held
+  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
   */
  static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
        scsi_qla_host_t *vha,
  }
  
  /*
-  * Expected to be called with struct qla_hw_data->hardware_lock held
+  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
   */
  static void tcm_qla2xxx_set_sess_by_loop_id(
        struct tcm_qla2xxx_lport *lport,
  }
  
  /*
-  * Should always be called with qla_hw_data->hardware_lock held.
+  * Should always be called with qla_hw_data->tgt.sess_lock held.
   */
  static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
                struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
@@@ -1353,7 -1357,7 +1357,7 @@@ static int tcm_qla2xxx_check_initiator_
        struct qla_tgt_sess *sess = qla_tgt_sess;
        unsigned char port_name[36];
        unsigned long flags;
-       int num_tags = (ha->fw_xcb_count) ? ha->fw_xcb_count :
+       int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
                       TCM_QLA2XXX_DEFAULT_TAGS;
  
        lport = vha->vha_tgt.target_lport_ptr;
         * And now setup the new se_nacl and session pointers into our HW lport
         * mappings for fabric S_ID and LOOP_ID.
         */
-       spin_lock_irqsave(&ha->hardware_lock, flags);
+       spin_lock_irqsave(&ha->tgt.sess_lock, flags);
        tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
                        qla_tgt_sess, s_id);
        tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
                        qla_tgt_sess, loop_id);
-       spin_unlock_irqrestore(&ha->hardware_lock, flags);
+       spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
        /*
         * Finally register the new FC Nexus with TCM
         */
index 72204fbf2bb13be5cdb7211ca6001b58f0c4fbc6,762b2d6ea1cc44c3d1a22b98a3792b5ee0cbe88b..576a7a43470ce0b0c0932e563bdb2fa639f7b110
@@@ -1333,7 -1333,7 +1333,7 @@@ iscsit_check_dataout_hdr(struct iscsi_c
                        /*
                         * Check if a delayed TASK_ABORTED status needs to
                         * be sent now if the ISCSI_FLAG_CMD_FINAL has been
-                        * received with the unsolicitied data out.
+                        * received with the unsolicited data out.
                         */
                        if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
                                iscsit_stop_dataout_timer(cmd);
@@@ -3435,7 -3435,7 +3435,7 @@@ iscsit_build_sendtargets_response(struc
  
                        if ((tpg->tpg_attrib.generate_node_acls == 0) &&
                            (tpg->tpg_attrib.demo_mode_discovery == 0) &&
-                           (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
+                           (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
                                cmd->conn->sess->sess_ops->InitiatorName))) {
                                continue;
                        }
@@@ -4074,17 -4074,6 +4074,17 @@@ reject
        return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
  }
  
 +static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
 +{
 +      bool ret;
 +
 +      spin_lock_bh(&conn->state_lock);
 +      ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
 +      spin_unlock_bh(&conn->state_lock);
 +
 +      return ret;
 +}
 +
  int iscsi_target_rx_thread(void *arg)
  {
        int ret, rc;
         * incoming iscsi/tcp socket I/O, and/or failing the connection.
         */
        rc = wait_for_completion_interruptible(&conn->rx_login_comp);
 -      if (rc < 0)
 +      if (rc < 0 || iscsi_target_check_conn_state(conn))
                return 0;
  
        if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
@@@ -4459,9 -4448,6 +4459,6 @@@ int iscsit_close_connection
  
                return 0;
        }
-       spin_unlock_bh(&sess->conn_lock);
-       return 0;
  }
  
  int iscsit_close_session(struct iscsi_session *sess)
index 2cbea2af7cd032512572a07275f713aef366475b,1aa6c2b5abbe844a5f0c9e3ac4b4c00e06b17b1b..3a1f9a7e6bb6ee9531f94bce11113ef7d4564e34
@@@ -208,7 -208,7 +208,7 @@@ int iscsi_create_default_params(struct 
        if (!pl) {
                pr_err("Unable to allocate memory for"
                                " struct iscsi_param_list.\n");
 -              return -;
 +              return -ENOMEM;
        }
        INIT_LIST_HEAD(&pl->param_list);
        INIT_LIST_HEAD(&pl->extra_response_list);
@@@ -578,7 -578,7 +578,7 @@@ int iscsi_copy_param_list
        param_list = kzalloc(sizeof(struct iscsi_param_list), GFP_KERNEL);
        if (!param_list) {
                pr_err("Unable to allocate memory for struct iscsi_param_list.\n");
 -              return -1;
 +              return -ENOMEM;
        }
        INIT_LIST_HEAD(&param_list->param_list);
        INIT_LIST_HEAD(&param_list->extra_response_list);
  
  err_out:
        iscsi_release_param_list(param_list);
 -      return -1;
 +      return -ENOMEM;
  }
  
  static void iscsi_release_extra_responses(struct iscsi_param_list *param_list)
@@@ -729,7 -729,7 +729,7 @@@ static int iscsi_add_notunderstood_resp
        if (!extra_response) {
                pr_err("Unable to allocate memory for"
                        " struct iscsi_extra_response.\n");
 -              return -1;
 +              return -ENOMEM;
        }
        INIT_LIST_HEAD(&extra_response->er_list);
  
@@@ -1370,7 -1370,7 +1370,7 @@@ int iscsi_decode_text_input
        tmpbuf = kzalloc(length + 1, GFP_KERNEL);
        if (!tmpbuf) {
                pr_err("Unable to allocate %u + 1 bytes for tmpbuf.\n", length);
 -              return -1;
 +              return -ENOMEM;
        }
  
        memcpy(tmpbuf, textbuf, length);
@@@ -1668,7 -1668,7 +1668,7 @@@ void iscsi_set_session_parameters
                                param->value);
                } else if (!strcmp(param->name, INITIALR2T)) {
                        ops->InitialR2T = !strcmp(param->value, YES);
-                        pr_debug("InitialR2T:                   %s\n",
+                       pr_debug("InitialR2T:                   %s\n",
                                param->value);
                } else if (!strcmp(param->name, IMMEDIATEDATA)) {
                        ops->ImmediateData = !strcmp(param->value, YES);
index 98698d87574262226bcf893da2cb3d3849e6df32,b83ac3766fe77851ff60a9aa65582cb51b7ffb48..a9057aa07176ff240748372215333dd531296ca2
@@@ -141,9 -141,17 +141,17 @@@ sbc_emulate_readcapacity_16(struct se_c
         * Set Thin Provisioning Enable bit following sbc3r22 in section
         * READ CAPACITY (16) byte 14 if emulate_tpu or emulate_tpws is enabled.
         */
-       if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws)
+       if (dev->dev_attrib.emulate_tpu || dev->dev_attrib.emulate_tpws) {
                buf[14] |= 0x80;
  
+               /*
+                * LBPRZ signifies that zeroes will be read back from an LBA after
+                * an UNMAP or WRITE SAME w/ unmap bit (sbc3r36 5.16.2)
+                */
+               if (dev->dev_attrib.unmap_zeroes_data)
+                       buf[14] |= 0x40;
+       }
        rbuf = transport_kmap_data_sg(cmd);
        if (rbuf) {
                memcpy(rbuf, buf, min_t(u32, sizeof(buf), cmd->data_length));
@@@ -371,8 -379,7 +379,8 @@@ sbc_setup_write_same(struct se_cmd *cmd
        return 0;
  }
  
 -static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success)
 +static sense_reason_t xdreadwrite_callback(struct se_cmd *cmd, bool success,
 +                                         int *post_ret)
  {
        unsigned char *buf, *addr;
        struct scatterlist *sg;
@@@ -438,8 -445,7 +446,8 @@@ sbc_execute_rw(struct se_cmd *cmd
                               cmd->data_direction);
  }
  
 -static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success)
 +static sense_reason_t compare_and_write_post(struct se_cmd *cmd, bool success,
 +                                           int *post_ret)
  {
        struct se_device *dev = cmd->se_dev;
  
         * sent to the backend driver.
         */
        spin_lock_irq(&cmd->t_state_lock);
 -      if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status)
 +      if ((cmd->transport_state & CMD_T_SENT) && !cmd->scsi_status) {
                cmd->se_cmd_flags |= SCF_COMPARE_AND_WRITE_POST;
 +              *post_ret = 1;
 +      }
        spin_unlock_irq(&cmd->t_state_lock);
  
        /*
        return TCM_NO_SENSE;
  }
  
 -static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success)
 +static sense_reason_t compare_and_write_callback(struct se_cmd *cmd, bool success,
 +                                               int *post_ret)
  {
        struct se_device *dev = cmd->se_dev;
        struct scatterlist *write_sg = NULL, *sg;
  
                if (block_size < PAGE_SIZE) {
                        sg_set_page(&write_sg[i], m.page, block_size,
 -                                  block_size);
 +                                  m.piter.sg->offset + block_size);
                } else {
                        sg_miter_next(&m);
                        sg_set_page(&write_sg[i], m.page, block_size,
 -                                  0);
 +                                  m.piter.sg->offset);
                }
                len -= block_size;
                i++;
index 28fb3016370faf1048bd361c1b98d2c3855483c8,713704211aab8e1b2c0a318fbf119710b2e3e0c4..fcdcb117c60da36e546370ecfd7e973f17b5d4ec
@@@ -130,9 -130,6 +130,9 @@@ void core_tmr_abort_task
                if (tmr->ref_task_tag != ref_tag)
                        continue;
  
 +              if (!kref_get_unless_zero(&se_cmd->cmd_kref))
 +                      continue;
 +
                printk("ABORT_TASK: Found referenced %s task_tag: %llu\n",
                        se_cmd->se_tfo->get_fabric_name(), ref_tag);
  
                               " skipping\n", ref_tag);
                        spin_unlock(&se_cmd->t_state_lock);
                        spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
 +
 +                      target_put_sess_cmd(se_cmd);
 +
                        goto out;
                }
                se_cmd->transport_state |= CMD_T_ABORTED;
                spin_unlock(&se_cmd->t_state_lock);
  
                list_del_init(&se_cmd->se_cmd_list);
 -              kref_get(&se_cmd->cmd_kref);
                spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  
                cancel_work_sync(&se_cmd->work);
@@@ -201,7 -196,7 +201,7 @@@ static void core_tmr_drain_tmr_list
                /*
                 * If this function was called with a valid pr_res_key
                 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
-                * skip non regisration key matching TMRs.
+                * skip non registration key matching TMRs.
                 */
                if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
                        continue;
index 4fdcee2006d1698f4b4e8689c7976ea161a34685,a7c1bb54cf72e36e81fbbb80a06fa0417f55710b..9f3608e10f25a0ea2c1cf71b419596d558e7f31b
@@@ -341,7 -341,6 +341,6 @@@ void __transport_register_session
                                        &buf[0], PR_REG_ISID_LEN);
                        se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
                }
-               kref_get(&se_nacl->acl_kref);
  
                spin_lock_irq(&se_nacl->nacl_sess_lock);
                /*
@@@ -384,9 -383,9 +383,9 @@@ static void target_release_session(stru
        se_tpg->se_tpg_tfo->close_session(se_sess);
  }
  
void target_get_session(struct se_session *se_sess)
int target_get_session(struct se_session *se_sess)
  {
-       kref_get(&se_sess->sess_kref);
+       return kref_get_unless_zero(&se_sess->sess_kref);
  }
  EXPORT_SYMBOL(target_get_session);
  
@@@ -432,6 -431,7 +431,7 @@@ void target_put_nacl(struct se_node_ac
  {
        kref_put(&nacl->acl_kref, target_complete_nacl);
  }
+ EXPORT_SYMBOL(target_put_nacl);
  
  void transport_deregister_session_configfs(struct se_session *se_sess)
  {
@@@ -464,6 -464,15 +464,15 @@@ EXPORT_SYMBOL(transport_deregister_sess
  
  void transport_free_session(struct se_session *se_sess)
  {
+       struct se_node_acl *se_nacl = se_sess->se_node_acl;
+       /*
+        * Drop the se_node_acl->nacl_kref obtained from within
+        * core_tpg_get_initiator_node_acl().
+        */
+       if (se_nacl) {
+               se_sess->se_node_acl = NULL;
+               target_put_nacl(se_nacl);
+       }
        if (se_sess->sess_cmd_map) {
                percpu_ida_destroy(&se_sess->sess_tag_pool);
                kvfree(se_sess->sess_cmd_map);
@@@ -478,7 -487,7 +487,7 @@@ void transport_deregister_session(struc
        const struct target_core_fabric_ops *se_tfo;
        struct se_node_acl *se_nacl;
        unsigned long flags;
-       bool comp_nacl = true, drop_nacl = false;
+       bool drop_nacl = false;
  
        if (!se_tpg) {
                transport_free_session(se_sess);
        if (se_nacl && se_nacl->dynamic_node_acl) {
                if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
                        list_del(&se_nacl->acl_list);
-                       se_tpg->num_node_acls--;
                        drop_nacl = true;
                }
        }
        if (drop_nacl) {
                core_tpg_wait_for_nacl_pr_ref(se_nacl);
                core_free_device_list_for_node(se_nacl, se_tpg);
+               se_sess->se_node_acl = NULL;
                kfree(se_nacl);
-               comp_nacl = false;
        }
        pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
                se_tpg->se_tpg_tfo->get_fabric_name());
        /*
         * If last kref is dropping now for an explicit NodeACL, awake sleeping
         * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
-        * removal context.
+        * removal context from within transport_free_session() code.
         */
-       if (se_nacl && comp_nacl)
-               target_put_nacl(se_nacl);
  
        transport_free_session(se_sess);
  }
@@@ -715,7 -721,10 +721,10 @@@ void target_complete_cmd(struct se_cmd 
        cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
        spin_unlock_irqrestore(&cmd->t_state_lock, flags);
  
-       queue_work(target_completion_wq, &cmd->work);
+       if (cmd->cpuid == -1)
+               queue_work(target_completion_wq, &cmd->work);
+       else
+               queue_work_on(cmd->cpuid, target_completion_wq, &cmd->work);
  }
  EXPORT_SYMBOL(target_complete_cmd);
  
@@@ -1309,7 -1318,7 +1318,7 @@@ EXPORT_SYMBOL(target_setup_cmd_from_cdb
  
  /*
   * Used by fabric module frontends to queue tasks directly.
-  * Many only be used from process context only
+  * May only be used from process context.
   */
  int transport_handle_cdb_direct(
        struct se_cmd *cmd)
@@@ -1582,7 -1591,7 +1591,7 @@@ static void target_complete_tmr_failure
  int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
                unsigned char *sense, u64 unpacked_lun,
                void *fabric_tmr_ptr, unsigned char tm_type,
-               gfp_t gfp, unsigned int tag, int flags)
+               gfp_t gfp, u64 tag, int flags)
  {
        struct se_portal_group *se_tpg;
        int ret;
@@@ -1658,7 -1667,7 +1667,7 @@@ bool target_stop_cmd(struct se_cmd *cmd
  void transport_generic_request_failure(struct se_cmd *cmd,
                sense_reason_t sense_reason)
  {
 -      int ret = 0;
 +      int ret = 0, post_ret = 0;
  
        pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08llx"
                " CDB: 0x%02x\n", cmd, cmd->tag, cmd->t_task_cdb[0]);
         */
        if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
             cmd->transport_complete_callback)
 -              cmd->transport_complete_callback(cmd, false);
 +              cmd->transport_complete_callback(cmd, false, &post_ret);
  
        switch (sense_reason) {
        case TCM_NON_EXISTENT_LUN:
@@@ -2068,13 -2077,11 +2077,13 @@@ static void target_complete_ok_work(str
         */
        if (cmd->transport_complete_callback) {
                sense_reason_t rc;
 +              bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE);
 +              bool zero_dl = !(cmd->data_length);
 +              int post_ret = 0;
  
 -              rc = cmd->transport_complete_callback(cmd, true);
 -              if (!rc && !(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE_POST)) {
 -                      if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
 -                          !cmd->data_length)
 +              rc = cmd->transport_complete_callback(cmd, true, &post_ret);
 +              if (!rc && !post_ret) {
 +                      if (caw && zero_dl)
                                goto queue_rsp;
  
                        return;
  EXPORT_SYMBOL(target_get_sess_cmd);
  
  static void target_release_cmd_kref(struct kref *kref)
 -              __releases(&se_cmd->se_sess->sess_cmd_lock)
  {
        struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
        struct se_session *se_sess = se_cmd->se_sess;
 +      unsigned long flags;
  
 +      spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
        if (list_empty(&se_cmd->se_cmd_list)) {
 -              spin_unlock(&se_sess->sess_cmd_lock);
 +              spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
                se_cmd->se_tfo->release_cmd(se_cmd);
                return;
        }
        if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
 -              spin_unlock(&se_sess->sess_cmd_lock);
 +              spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
                complete(&se_cmd->cmd_wait_comp);
                return;
        }
        list_del(&se_cmd->se_cmd_list);
 -      spin_unlock(&se_sess->sess_cmd_lock);
 +      spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
  
        se_cmd->se_tfo->release_cmd(se_cmd);
  }
@@@ -2542,7 -2548,8 +2551,7 @@@ int target_put_sess_cmd(struct se_cmd *
                se_cmd->se_tfo->release_cmd(se_cmd);
                return 1;
        }
 -      return kref_put_spinlock_irqsave(&se_cmd->cmd_kref, target_release_cmd_kref,
 -                      &se_sess->sess_cmd_lock);
 +      return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
  }
  EXPORT_SYMBOL(target_put_sess_cmd);
  
index 5e6d6cb348fc1c902ea74079357430d5dfb0d44a,a84cd40ab964f51e3410fb8420ca53efa872a696..dd600e5ead719ba95d41ae0b3b4eeed8a9161eab
@@@ -152,6 -152,7 +152,7 @@@ static struct genl_family tcmu_genl_fam
        .maxattr = TCMU_ATTR_MAX,
        .mcgrps = tcmu_mcgrps,
        .n_mcgrps = ARRAY_SIZE(tcmu_mcgrps),
+       .netnsok = true,
  };
  
  static struct tcmu_cmd *tcmu_alloc_cmd(struct se_cmd *se_cmd)
  
  static inline void tcmu_flush_dcache_range(void *vaddr, size_t size)
  {
-       unsigned long offset = (unsigned long) vaddr & ~PAGE_MASK;
+       unsigned long offset = offset_in_page(vaddr);
  
        size = round_up(size+offset, PAGE_SIZE);
        vaddr -= offset;
@@@ -638,7 -639,7 +639,7 @@@ static int tcmu_check_expired_cmd(int i
        if (test_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags))
                return 0;
  
 -      if (!time_after(cmd->deadline, jiffies))
 +      if (!time_after(jiffies, cmd->deadline))
                return 0;
  
        set_bit(TCMU_CMD_BIT_EXPIRED, &cmd->flags);
@@@ -840,7 -841,7 +841,7 @@@ static int tcmu_netlink_event(enum tcmu
  
        genlmsg_end(skb, msg_header);
  
-       ret = genlmsg_multicast(&tcmu_genl_family, skb, 0,
+       ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
                                TCMU_MCGRP_CONFIG, GFP_KERNEL);
  
        /* We don't care if no one is listening */
@@@ -917,8 -918,10 +918,10 @@@ static int tcmu_configure_device(struc
        if (ret)
                goto err_register;
  
+       /* User can set hw_block_size before enable the device */
+       if (dev->dev_attrib.hw_block_size == 0)
+               dev->dev_attrib.hw_block_size = 512;
        /* Other attributes can be configured in userspace */
-       dev->dev_attrib.hw_block_size = 512;
        dev->dev_attrib.hw_max_sectors = 128;
        dev->dev_attrib.hw_queue_depth = 128;
  
@@@ -1101,6 -1104,8 +1104,6 @@@ tcmu_parse_cdb(struct se_cmd *cmd
  
  static const struct target_backend_ops tcmu_ops = {
        .name                   = "user",
 -      .inquiry_prod           = "USER",
 -      .inquiry_rev            = TCMU_VERSION,
        .owner                  = THIS_MODULE,
        .transport_flags        = TRANSPORT_FLAG_PASSTHROUGH,
        .attach_hba             = tcmu_attach_hba,
index be5aab9c13f2da50f6111222f838f142e1f25213,0527308334ac4d2eaadec7f65f1ea1c85dc87b24..af5d922a8f5d692bd5f8be2d9d1942297ca02b34
@@@ -127,12 -127,6 +127,12 @@@ config USB_GADGET_STORAGE_NUM_BUFFER
           a module parameter as well.
           If unsure, say 2.
  
 +config U_SERIAL_CONSOLE
 +      bool "Serial gadget console support"
 +      depends on USB_G_SERIAL
 +      help
 +         It supports the serial gadget can be used as a console.
 +
  source "drivers/usb/gadget/udc/Kconfig"
  
  #
@@@ -205,6 -199,9 +205,9 @@@ config USB_F_HI
  config USB_F_PRINTER
        tristate
  
+ config USB_F_TCM
+       tristate
  choice
        tristate "USB Gadget Drivers"
        default USB_ETH
@@@ -457,6 -454,20 +460,20 @@@ config USB_CONFIGFS_F_PRINTE
          For more information, see Documentation/usb/gadget_printer.txt
          which includes sample code for accessing the device file.
  
+ config USB_CONFIGFS_F_TCM
+       bool "USB Gadget Target Fabric"
+       depends on TARGET_CORE
+       depends on USB_CONFIGFS
+       select USB_LIBCOMPOSITE
+       select USB_F_TCM
+       help
+         This fabric is a USB gadget component. Two USB protocols are
+         supported that is BBB or BOT (Bulk Only Transport) and UAS
+         (USB Attached SCSI). BOT is advertised on alternative
+         interface 0 (primary) and UAS is on alternative interface 1.
+         Both protocols can work on USB2.0 and USB3.0.
+         UAS utilizes the USB 3.0 feature called streams support.
  source "drivers/usb/gadget/legacy/Kconfig"
  
  endchoice
index 7857fa411636be096986c64c73695e17059110a6,c209148202fd8c81e379354662c842b7a005bbc2..0b0bb98319cdcb617e8f0a20623b2b81220ae3df
  #include <target/target_core_fabric.h>
  #include <asm/unaligned.h>
  
- #include "tcm_usb_gadget.h"
+ #include "u_tcm.h"
  
  USB_GADGET_COMPOSITE_OPTIONS();
  
- static inline struct f_uas *to_f_uas(struct usb_function *f)
- {
-       return container_of(f, struct f_uas, function);
- }
- static void usbg_cmd_release(struct kref *);
- static inline void usbg_cleanup_cmd(struct usbg_cmd *cmd)
- {
-       kref_put(&cmd->ref, usbg_cmd_release);
- }
- /* Start bot.c code */
- static int bot_enqueue_cmd_cbw(struct f_uas *fu)
- {
-       int ret;
-       if (fu->flags & USBG_BOT_CMD_PEND)
-               return 0;
-       ret = usb_ep_queue(fu->ep_out, fu->cmd.req, GFP_ATOMIC);
-       if (!ret)
-               fu->flags |= USBG_BOT_CMD_PEND;
-       return ret;
- }
- static void bot_status_complete(struct usb_ep *ep, struct usb_request *req)
- {
-       struct usbg_cmd *cmd = req->context;
-       struct f_uas *fu = cmd->fu;
-       usbg_cleanup_cmd(cmd);
-       if (req->status < 0) {
-               pr_err("ERR %s(%d)\n", __func__, __LINE__);
-               return;
-       }
-       /* CSW completed, wait for next CBW */
-       bot_enqueue_cmd_cbw(fu);
- }
- static void bot_enqueue_sense_code(struct f_uas *fu, struct usbg_cmd *cmd)
- {
-       struct bulk_cs_wrap *csw = &fu->bot_status.csw;
-       int ret;
-       u8 *sense;
-       unsigned int csw_stat;
-       csw_stat = cmd->csw_code;
-       /*
-        * We can't send SENSE as a response. So we take ASC & ASCQ from our
-        * sense buffer and queue it and hope the host sends a REQUEST_SENSE
-        * command where it learns why we failed.
-        */
-       sense = cmd->sense_iu.sense;
-       csw->Tag = cmd->bot_tag;
-       csw->Status = csw_stat;
-       fu->bot_status.req->context = cmd;
-       ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_ATOMIC);
-       if (ret)
-               pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
- }
- static void bot_err_compl(struct usb_ep *ep, struct usb_request *req)
- {
-       struct usbg_cmd *cmd = req->context;
-       struct f_uas *fu = cmd->fu;
-       if (req->status < 0)
-               pr_err("ERR %s(%d)\n", __func__, __LINE__);
-       if (cmd->data_len) {
-               if (cmd->data_len > ep->maxpacket) {
-                       req->length = ep->maxpacket;
-                       cmd->data_len -= ep->maxpacket;
-               } else {
-                       req->length = cmd->data_len;
-                       cmd->data_len = 0;
-               }
-               usb_ep_queue(ep, req, GFP_ATOMIC);
-               return ;
-       }
-       bot_enqueue_sense_code(fu, cmd);
- }
- static void bot_send_bad_status(struct usbg_cmd *cmd)
- {
-       struct f_uas *fu = cmd->fu;
-       struct bulk_cs_wrap *csw = &fu->bot_status.csw;
-       struct usb_request *req;
-       struct usb_ep *ep;
-       csw->Residue = cpu_to_le32(cmd->data_len);
-       if (cmd->data_len) {
-               if (cmd->is_read) {
-                       ep = fu->ep_in;
-                       req = fu->bot_req_in;
-               } else {
-                       ep = fu->ep_out;
-                       req = fu->bot_req_out;
-               }
-               if (cmd->data_len > fu->ep_in->maxpacket) {
-                       req->length = ep->maxpacket;
-                       cmd->data_len -= ep->maxpacket;
-               } else {
-                       req->length = cmd->data_len;
-                       cmd->data_len = 0;
-               }
-               req->complete = bot_err_compl;
-               req->context = cmd;
-               req->buf = fu->cmd.buf;
-               usb_ep_queue(ep, req, GFP_KERNEL);
-       } else {
-               bot_enqueue_sense_code(fu, cmd);
-       }
- }
- static int bot_send_status(struct usbg_cmd *cmd, bool moved_data)
- {
-       struct f_uas *fu = cmd->fu;
-       struct bulk_cs_wrap *csw = &fu->bot_status.csw;
-       int ret;
-       if (cmd->se_cmd.scsi_status == SAM_STAT_GOOD) {
-               if (!moved_data && cmd->data_len) {
-                       /*
-                        * the host wants to move data, we don't. Fill / empty
-                        * the pipe and then send the csw with reside set.
-                        */
-                       cmd->csw_code = US_BULK_STAT_OK;
-                       bot_send_bad_status(cmd);
-                       return 0;
-               }
-               csw->Tag = cmd->bot_tag;
-               csw->Residue = cpu_to_le32(0);
-               csw->Status = US_BULK_STAT_OK;
-               fu->bot_status.req->context = cmd;
-               ret = usb_ep_queue(fu->ep_in, fu->bot_status.req, GFP_KERNEL);
-               if (ret)
-                       pr_err("%s(%d) ERR: %d\n", __func__, __LINE__, ret);
-       } else {
-               cmd->csw_code = US_BULK_STAT_FAIL;
-               bot_send_bad_status(cmd);
-       }
-       return 0;
- }
- /*
-  * Called after command (no data transfer) or after the write (to device)
-  * operation is completed
-  */
- static int bot_send_status_response(struct usbg_cmd *cmd)
- {
-       bool moved_data = false;
-       if (!cmd->is_read)
-               moved_data = true;
-       return bot_send_status(cmd, moved_data);
- }
- /* Read request completed, now we have to send the CSW */
- static void bot_read_compl(struct usb_ep *ep, struct usb_request *req)
- {
-       struct usbg_cmd *cmd = req->context;
-       if (req->status < 0)
-               pr_err("ERR %s(%d)\n", __func__, __LINE__);
-       bot_send_status(cmd, true);
- }
- static int bot_send_read_response(struct usbg_cmd *cmd)
- {
-       struct f_uas *fu = cmd->fu;
-       struct se_cmd *se_cmd = &cmd->se_cmd;
-       struct usb_gadget *gadget = fuas_to_gadget(fu);
-       int ret;
-       if (!cmd->data_len) {
-               cmd->csw_code = US_BULK_STAT_PHASE;
-               bot_send_bad_status(cmd);
-               return 0;
-       }
-       if (!gadget->sg_supported) {
-               cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
-               if (!cmd->data_buf)
-                       return -ENOMEM;
-               sg_copy_to_buffer(se_cmd->t_data_sg,
-                               se_cmd->t_data_nents,
-                               cmd->data_buf,
-                               se_cmd->data_length);
-               fu->bot_req_in->buf = cmd->data_buf;
-       } else {
-               fu->bot_req_in->buf = NULL;
-               fu->bot_req_in->num_sgs = se_cmd->t_data_nents;
-               fu->bot_req_in->sg = se_cmd->t_data_sg;
-       }
-       fu->bot_req_in->complete = bot_read_compl;
-       fu->bot_req_in->length = se_cmd->data_length;
-       fu->bot_req_in->context = cmd;
-       ret = usb_ep_queue(fu->ep_in, fu->bot_req_in, GFP_ATOMIC);
-       if (ret)
-               pr_err("%s(%d)\n", __func__, __LINE__);
-       return 0;
- }
- static void usbg_data_write_cmpl(struct usb_ep *, struct usb_request *);
- static int usbg_prepare_w_request(struct usbg_cmd *, struct usb_request *);
- static int bot_send_write_request(struct usbg_cmd *cmd)
- {
-       struct f_uas *fu = cmd->fu;
-       struct se_cmd *se_cmd = &cmd->se_cmd;
-       struct usb_gadget *gadget = fuas_to_gadget(fu);
-       int ret;
-       init_completion(&cmd->write_complete);
-       cmd->fu = fu;
-       if (!cmd->data_len) {
-               cmd->csw_code = US_BULK_STAT_PHASE;
-               return -EINVAL;
-       }
-       if (!gadget->sg_supported) {
-               cmd->data_buf = kmalloc(se_cmd->data_length, GFP_KERNEL);
-               if (!cmd->data_buf)
-                       return -ENOMEM;
-               fu->bot_req_out->buf = cmd->data_buf;
-       } else {
-               fu->bot_req_out->buf = NULL;
-               fu->bot_req_out->num_sgs = se_cmd->t_data_nents;
-               fu->bot_req_out->sg = se_cmd->t_data_sg;
-       }
-       fu->bot_req_out->complete = usbg_data_write_cmpl;
-       fu->bot_req_out->length = se_cmd->data_length;
-       fu->bot_req_out->context = cmd;
-       ret = usbg_prepare_w_request(cmd, fu->bot_req_out);
-       if (ret)
-               goto cleanup;
-       ret = usb_ep_queue(fu->ep_out, fu->bot_req_out, GFP_KERNEL);
-       if (ret)
-               pr_err("%s(%d)\n", __func__, __LINE__);
-       wait_for_completion(&cmd->write_complete);
-       target_execute_cmd(se_cmd);
- cleanup:
-       return ret;
- }
- static int bot_submit_command(struct f_uas *, void *, unsigned int);
- static void bot_cmd_complete(struct usb_ep *ep, struct usb_request *req)
- {
-       struct f_uas *fu = req->context;
-       int ret;
-       fu->flags &= ~USBG_BOT_CMD_PEND;
-       if (req->status < 0)
-               return;
-       ret = bot_submit_command(fu, req->buf, req->actual);
-       if (ret)
-               pr_err("%s(%d): %d\n", __func__, __LINE__, ret);
- }
- static int bot_prepare_reqs(struct f_uas *fu)
- {
-       int ret;
-       fu->bot_req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
-       if (!fu->bot_req_in)
-               goto err;
-       fu->bot_req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
-       if (!fu->bot_req_out)
-               goto err_out;
-       fu->cmd.req = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
-       if (!fu->cmd.req)
-               goto err_cmd;
-       fu->bot_status.req = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
-       if (!fu->bot_status.req)
-               goto err_sts;
-       fu->bot_status.req->buf = &fu->bot_status.csw;
-       fu->bot_status.req->length = US_BULK_CS_WRAP_LEN;
-       fu->bot_status.req->complete = bot_status_complete;
-       fu->bot_status.csw.Signature = cpu_to_le32(US_BULK_CS_SIGN);
-       fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);
-       if (!fu->cmd.buf)
-               goto err_buf;
-       fu->cmd.req->complete = bot_cmd_complete;
-       fu->cmd.req->buf = fu->cmd.buf;
-       fu->cmd.req->length = fu->ep_out->maxpacket;
-       fu->cmd.req->context = fu;
-       ret = bot_enqueue_cmd_cbw(fu);
-       if (ret)
-               goto err_queue;
-       return 0;
- err_queue:
-       kfree(fu->cmd.buf);
-       fu->cmd.buf = NULL;
- err_buf:
-       usb_ep_free_request(fu->ep_in, fu->bot_status.req);
- err_sts:
-       usb_ep_free_request(fu->ep_out, fu->cmd.req);
-       fu->cmd.req = NULL;
- err_cmd:
-       usb_ep_free_request(fu->ep_out, fu->bot_req_out);
-       fu->bot_req_out = NULL;
- err_out:
-       usb_ep_free_request(fu->ep_in, fu->bot_req_in);
-       fu->bot_req_in = NULL;
- err:
-       pr_err("BOT: endpoint setup failed\n");
-       return -ENOMEM;
- }
- static void bot_cleanup_old_alt(struct f_uas *fu)
- {
-       if (!(fu->flags & USBG_ENABLED))
-               return;
-       usb_ep_disable(fu->ep_in);
-       usb_ep_disable(fu->ep_out);
-       if (!fu->bot_req_in)
-               return;
-       usb_ep_free_request(fu->ep_in, fu->bot_req_in);
-       usb_ep_free_request(fu->ep_out, fu->bot_req_out);
-       usb_ep_free_request(fu->ep_out, fu->cmd.req);
-       usb_ep_free_request(fu->ep_out, fu->bot_status.req);
-       kfree(fu->cmd.buf);
-       fu->bot_req_in = NULL;
-       fu->bot_req_out = NULL;
-       fu->cmd.req = NULL;
-       fu->bot_status.req = NULL;
-       fu->cmd.buf = NULL;
- }
- static void bot_set_alt(struct f_uas *fu)
- {
-       struct usb_function *f = &fu->function;
-       struct usb_gadget *gadget = f->config->cdev->gadget;
-       int ret;
-       fu->flags = USBG_IS_BOT;
-       config_ep_by_speed(gadget, f, fu->ep_in);
-       ret = usb_ep_enable(fu->ep_in);
-       if (ret)
-               goto err_b_in;
-       config_ep_by_speed(gadget, f, fu->ep_out);
-       ret = usb_ep_enable(fu->ep_out);
-       if (ret)
-               goto err_b_out;
-       ret = bot_prepare_reqs(fu);
-       if (ret)
-               goto err_wq;
-       fu->flags |= USBG_ENABLED;
-       pr_info("Using the BOT protocol\n");
-       return;
- err_wq:
-       usb_ep_disable(fu->ep_out);
- err_b_out:
-       usb_ep_disable(fu->ep_in);
- err_b_in:
-       fu->flags = USBG_IS_BOT;
- }
- static int usbg_bot_setup(struct usb_function *f,
-               const struct usb_ctrlrequest *ctrl)
- {
-       struct f_uas *fu = to_f_uas(f);
-       struct usb_composite_dev *cdev = f->config->cdev;
-       u16 w_value = le16_to_cpu(ctrl->wValue);
-       u16 w_length = le16_to_cpu(ctrl->wLength);
-       int luns;
-       u8 *ret_lun;
-       switch (ctrl->bRequest) {
-       case US_BULK_GET_MAX_LUN:
-               if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_CLASS |
-                                       USB_RECIP_INTERFACE))
-                       return -ENOTSUPP;
-               if (w_length < 1)
-                       return -EINVAL;
-               if (w_value != 0)
-                       return -EINVAL;
-               luns = atomic_read(&fu->tpg->tpg_port_count);
-               if (!luns) {
-                       pr_err("No LUNs configured?\n");
-                       return -EINVAL;
-               }
-               /*
-                * If 4 LUNs are present we return 3 i.e. LUN 0..3 can be
-                * accessed. The upper limit is 0xf
-                */
-               luns--;
-               if (luns > 0xf) {
-                       pr_info_once("Limiting the number of luns to 16\n");
-                       luns = 0xf;
-               }
-               ret_lun = cdev->req->buf;
-               *ret_lun = luns;
-               cdev->req->length = 1;
-               return usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
-               break;
-       case US_BULK_RESET_REQUEST:
-               /* XXX maybe we should remove previous requests for IN + OUT */
-               bot_enqueue_cmd_cbw(fu);
-               return 0;
-               break;
-       }
-       return -ENOTSUPP;
- }
- /* Start uas.c code */
- static void uasp_cleanup_one_stream(struct f_uas *fu, struct uas_stream *stream)
- {
-       /* We have either all three allocated or none */
-       if (!stream->req_in)
-               return;
-       usb_ep_free_request(fu->ep_in, stream->req_in);
-       usb_ep_free_request(fu->ep_out, stream->req_out);
-       usb_ep_free_request(fu->ep_status, stream->req_status);
-       stream->req_in = NULL;
-       stream->req_out = NULL;
-       stream->req_status = NULL;
- }
- static void uasp_free_cmdreq(struct f_uas *fu)
- {
-       usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
-       kfree(fu->cmd.buf);
-       fu->cmd.req = NULL;
-       fu->cmd.buf = NULL;
- }
- static void uasp_cleanup_old_alt(struct f_uas *fu)
- {
-       int i;
-       if (!(fu->flags & USBG_ENABLED))
-               return;
-       usb_ep_disable(fu->ep_in);
-       usb_ep_disable(fu->ep_out);
-       usb_ep_disable(fu->ep_status);
-       usb_ep_disable(fu->ep_cmd);
-       for (i = 0; i < UASP_SS_EP_COMP_NUM_STREAMS; i++)
-               uasp_cleanup_one_stream(fu, &fu->stream[i]);
-       uasp_free_cmdreq(fu);
- }
- static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req);
- static int uasp_prepare_r_request(struct usbg_cmd *cmd)
- {
-       struct se_cmd *se_cmd = &cmd->se_cmd;
-       struct f_uas *fu = cmd->fu;
-       struct usb_gadget *gadget = fuas_to_gadget(fu);
-       struct uas_stream *stream = cmd->stream;
-       if (!gadget->sg_supported) {
-               cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
-               if (!cmd->data_buf)
-                       return -ENOMEM;
-               sg_copy_to_buffer(se_cmd->t_data_sg,
-                               se_cmd->t_data_nents,
-                               cmd->data_buf,
-                               se_cmd->data_length);
-               stream->req_in->buf = cmd->data_buf;
-       } else {
-               stream->req_in->buf = NULL;
-               stream->req_in->num_sgs = se_cmd->t_data_nents;
-               stream->req_in->sg = se_cmd->t_data_sg;
-       }
-       stream->req_in->complete = uasp_status_data_cmpl;
-       stream->req_in->length = se_cmd->data_length;
-       stream->req_in->context = cmd;
-       cmd->state = UASP_SEND_STATUS;
-       return 0;
- }
- static void uasp_prepare_status(struct usbg_cmd *cmd)
- {
-       struct se_cmd *se_cmd = &cmd->se_cmd;
-       struct sense_iu *iu = &cmd->sense_iu;
-       struct uas_stream *stream = cmd->stream;
-       cmd->state = UASP_QUEUE_COMMAND;
-       iu->iu_id = IU_ID_STATUS;
-       iu->tag = cpu_to_be16(cmd->tag);
-       /*
-        * iu->status_qual = cpu_to_be16(STATUS QUALIFIER SAM-4. Where R U?);
-        */
-       iu->len = cpu_to_be16(se_cmd->scsi_sense_length);
-       iu->status = se_cmd->scsi_status;
-       stream->req_status->context = cmd;
-       stream->req_status->length = se_cmd->scsi_sense_length + 16;
-       stream->req_status->buf = iu;
-       stream->req_status->complete = uasp_status_data_cmpl;
- }
- static void uasp_status_data_cmpl(struct usb_ep *ep, struct usb_request *req)
- {
-       struct usbg_cmd *cmd = req->context;
-       struct uas_stream *stream = cmd->stream;
-       struct f_uas *fu = cmd->fu;
-       int ret;
-       if (req->status < 0)
-               goto cleanup;
-       switch (cmd->state) {
-       case UASP_SEND_DATA:
-               ret = uasp_prepare_r_request(cmd);
-               if (ret)
-                       goto cleanup;
-               ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
-               if (ret)
-                       pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
-               break;
-       case UASP_RECEIVE_DATA:
-               ret = usbg_prepare_w_request(cmd, stream->req_out);
-               if (ret)
-                       goto cleanup;
-               ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
-               if (ret)
-                       pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
-               break;
-       case UASP_SEND_STATUS:
-               uasp_prepare_status(cmd);
-               ret = usb_ep_queue(fu->ep_status, stream->req_status,
-                               GFP_ATOMIC);
-               if (ret)
-                       pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
-               break;
-       case UASP_QUEUE_COMMAND:
-               usbg_cleanup_cmd(cmd);
-               usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
-               break;
-       default:
-               BUG();
-       }
-       return;
- cleanup:
-       usbg_cleanup_cmd(cmd);
- }
- static int uasp_send_status_response(struct usbg_cmd *cmd)
- {
-       struct f_uas *fu = cmd->fu;
-       struct uas_stream *stream = cmd->stream;
-       struct sense_iu *iu = &cmd->sense_iu;
-       iu->tag = cpu_to_be16(cmd->tag);
-       stream->req_status->complete = uasp_status_data_cmpl;
-       stream->req_status->context = cmd;
-       cmd->fu = fu;
-       uasp_prepare_status(cmd);
-       return usb_ep_queue(fu->ep_status, stream->req_status, GFP_ATOMIC);
- }
- static int uasp_send_read_response(struct usbg_cmd *cmd)
- {
-       struct f_uas *fu = cmd->fu;
-       struct uas_stream *stream = cmd->stream;
-       struct sense_iu *iu = &cmd->sense_iu;
-       int ret;
-       cmd->fu = fu;
-       iu->tag = cpu_to_be16(cmd->tag);
-       if (fu->flags & USBG_USE_STREAMS) {
-               ret = uasp_prepare_r_request(cmd);
-               if (ret)
-                       goto out;
-               ret = usb_ep_queue(fu->ep_in, stream->req_in, GFP_ATOMIC);
-               if (ret) {
-                       pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
-                       kfree(cmd->data_buf);
-                       cmd->data_buf = NULL;
-               }
-       } else {
-               iu->iu_id = IU_ID_READ_READY;
-               iu->tag = cpu_to_be16(cmd->tag);
-               stream->req_status->complete = uasp_status_data_cmpl;
-               stream->req_status->context = cmd;
-               cmd->state = UASP_SEND_DATA;
-               stream->req_status->buf = iu;
-               stream->req_status->length = sizeof(struct iu);
-               ret = usb_ep_queue(fu->ep_status, stream->req_status,
-                               GFP_ATOMIC);
-               if (ret)
-                       pr_err("%s(%d) => %d\n", __func__, __LINE__, ret);
-       }
- out:
-       return ret;
- }
- static int uasp_send_write_request(struct usbg_cmd *cmd)
- {
-       struct f_uas *fu = cmd->fu;
-       struct se_cmd *se_cmd = &cmd->se_cmd;
-       struct uas_stream *stream = cmd->stream;
-       struct sense_iu *iu = &cmd->sense_iu;
-       int ret;
-       init_completion(&cmd->write_complete);
-       cmd->fu = fu;
-       iu->tag = cpu_to_be16(cmd->tag);
-       if (fu->flags & USBG_USE_STREAMS) {
-               ret = usbg_prepare_w_request(cmd, stream->req_out);
-               if (ret)
-                       goto cleanup;
-               ret = usb_ep_queue(fu->ep_out, stream->req_out, GFP_ATOMIC);
-               if (ret)
-                       pr_err("%s(%d)\n", __func__, __LINE__);
-       } else {
-               iu->iu_id = IU_ID_WRITE_READY;
-               iu->tag = cpu_to_be16(cmd->tag);
-               stream->req_status->complete = uasp_status_data_cmpl;
-               stream->req_status->context = cmd;
-               cmd->state = UASP_RECEIVE_DATA;
-               stream->req_status->buf = iu;
-               stream->req_status->length = sizeof(struct iu);
-               ret = usb_ep_queue(fu->ep_status, stream->req_status,
-                               GFP_ATOMIC);
-               if (ret)
-                       pr_err("%s(%d)\n", __func__, __LINE__);
-       }
-       wait_for_completion(&cmd->write_complete);
-       target_execute_cmd(se_cmd);
- cleanup:
-       return ret;
- }
- static int usbg_submit_command(struct f_uas *, void *, unsigned int);
- static void uasp_cmd_complete(struct usb_ep *ep, struct usb_request *req)
- {
-       struct f_uas *fu = req->context;
-       int ret;
-       if (req->status < 0)
-               return;
-       ret = usbg_submit_command(fu, req->buf, req->actual);
-       /*
-        * Once we tune for performance enqueue the command req here again so
-        * we can receive a second command while we processing this one. Pay
-        * attention to properly sync STAUS endpoint with DATA IN + OUT so you
-        * don't break HS.
-        */
-       if (!ret)
-               return;
-       usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
- }
- static int uasp_alloc_stream_res(struct f_uas *fu, struct uas_stream *stream)
- {
-       stream->req_in = usb_ep_alloc_request(fu->ep_in, GFP_KERNEL);
-       if (!stream->req_in)
-               goto out;
-       stream->req_out = usb_ep_alloc_request(fu->ep_out, GFP_KERNEL);
-       if (!stream->req_out)
-               goto err_out;
-       stream->req_status = usb_ep_alloc_request(fu->ep_status, GFP_KERNEL);
-       if (!stream->req_status)
-               goto err_sts;
-       return 0;
- err_sts:
-       usb_ep_free_request(fu->ep_status, stream->req_status);
-       stream->req_status = NULL;
- err_out:
-       usb_ep_free_request(fu->ep_out, stream->req_out);
-       stream->req_out = NULL;
- out:
-       return -ENOMEM;
- }
- static int uasp_alloc_cmd(struct f_uas *fu)
- {
-       fu->cmd.req = usb_ep_alloc_request(fu->ep_cmd, GFP_KERNEL);
-       if (!fu->cmd.req)
-               goto err;
-       fu->cmd.buf = kmalloc(fu->ep_cmd->maxpacket, GFP_KERNEL);
-       if (!fu->cmd.buf)
-               goto err_buf;
-       fu->cmd.req->complete = uasp_cmd_complete;
-       fu->cmd.req->buf = fu->cmd.buf;
-       fu->cmd.req->length = fu->ep_cmd->maxpacket;
-       fu->cmd.req->context = fu;
-       return 0;
- err_buf:
-       usb_ep_free_request(fu->ep_cmd, fu->cmd.req);
- err:
-       return -ENOMEM;
- }
- static void uasp_setup_stream_res(struct f_uas *fu, int max_streams)
- {
-       int i;
-       for (i = 0; i < max_streams; i++) {
-               struct uas_stream *s = &fu->stream[i];
-               s->req_in->stream_id = i + 1;
-               s->req_out->stream_id = i + 1;
-               s->req_status->stream_id = i + 1;
-       }
- }
- static int uasp_prepare_reqs(struct f_uas *fu)
- {
-       int ret;
-       int i;
-       int max_streams;
-       if (fu->flags & USBG_USE_STREAMS)
-               max_streams = UASP_SS_EP_COMP_NUM_STREAMS;
-       else
-               max_streams = 1;
-       for (i = 0; i < max_streams; i++) {
-               ret = uasp_alloc_stream_res(fu, &fu->stream[i]);
-               if (ret)
-                       goto err_cleanup;
-       }
-       ret = uasp_alloc_cmd(fu);
-       if (ret)
-               goto err_free_stream;
-       uasp_setup_stream_res(fu, max_streams);
-       ret = usb_ep_queue(fu->ep_cmd, fu->cmd.req, GFP_ATOMIC);
-       if (ret)
-               goto err_free_stream;
-       return 0;
- err_free_stream:
-       uasp_free_cmdreq(fu);
- err_cleanup:
-       if (i) {
-               do {
-                       uasp_cleanup_one_stream(fu, &fu->stream[i - 1]);
-                       i--;
-               } while (i);
-       }
-       pr_err("UASP: endpoint setup failed\n");
-       return ret;
- }
- static void uasp_set_alt(struct f_uas *fu)
- {
-       struct usb_function *f = &fu->function;
-       struct usb_gadget *gadget = f->config->cdev->gadget;
-       int ret;
-       fu->flags = USBG_IS_UAS;
-       if (gadget->speed == USB_SPEED_SUPER)
-               fu->flags |= USBG_USE_STREAMS;
-       config_ep_by_speed(gadget, f, fu->ep_in);
-       ret = usb_ep_enable(fu->ep_in);
-       if (ret)
-               goto err_b_in;
-       config_ep_by_speed(gadget, f, fu->ep_out);
-       ret = usb_ep_enable(fu->ep_out);
-       if (ret)
-               goto err_b_out;
-       config_ep_by_speed(gadget, f, fu->ep_cmd);
-       ret = usb_ep_enable(fu->ep_cmd);
-       if (ret)
-               goto err_cmd;
-       config_ep_by_speed(gadget, f, fu->ep_status);
-       ret = usb_ep_enable(fu->ep_status);
-       if (ret)
-               goto err_status;
-       ret = uasp_prepare_reqs(fu);
-       if (ret)
-               goto err_wq;
-       fu->flags |= USBG_ENABLED;
-       pr_info("Using the UAS protocol\n");
-       return;
- err_wq:
-       usb_ep_disable(fu->ep_status);
- err_status:
-       usb_ep_disable(fu->ep_cmd);
- err_cmd:
-       usb_ep_disable(fu->ep_out);
- err_b_out:
-       usb_ep_disable(fu->ep_in);
- err_b_in:
-       fu->flags = 0;
- }
- static int get_cmd_dir(const unsigned char *cdb)
- {
-       int ret;
-       switch (cdb[0]) {
-       case READ_6:
-       case READ_10:
-       case READ_12:
-       case READ_16:
-       case INQUIRY:
-       case MODE_SENSE:
-       case MODE_SENSE_10:
-       case SERVICE_ACTION_IN_16:
-       case MAINTENANCE_IN:
-       case PERSISTENT_RESERVE_IN:
-       case SECURITY_PROTOCOL_IN:
-       case ACCESS_CONTROL_IN:
-       case REPORT_LUNS:
-       case READ_BLOCK_LIMITS:
-       case READ_POSITION:
-       case READ_CAPACITY:
-       case READ_TOC:
-       case READ_FORMAT_CAPACITIES:
-       case REQUEST_SENSE:
-               ret = DMA_FROM_DEVICE;
-               break;
-       case WRITE_6:
-       case WRITE_10:
-       case WRITE_12:
-       case WRITE_16:
-       case MODE_SELECT:
-       case MODE_SELECT_10:
-       case WRITE_VERIFY:
-       case WRITE_VERIFY_12:
-       case PERSISTENT_RESERVE_OUT:
-       case MAINTENANCE_OUT:
-       case SECURITY_PROTOCOL_OUT:
-       case ACCESS_CONTROL_OUT:
-               ret = DMA_TO_DEVICE;
-               break;
-       case ALLOW_MEDIUM_REMOVAL:
-       case TEST_UNIT_READY:
-       case SYNCHRONIZE_CACHE:
-       case START_STOP:
-       case ERASE:
-       case REZERO_UNIT:
-       case SEEK_10:
-       case SPACE:
-       case VERIFY:
-       case WRITE_FILEMARKS:
-               ret = DMA_NONE;
-               break;
-       default:
-               pr_warn("target: Unknown data direction for SCSI Opcode "
-                               "0x%02x\n", cdb[0]);
-               ret = -EINVAL;
-       }
-       return ret;
- }
- static void usbg_data_write_cmpl(struct usb_ep *ep, struct usb_request *req)
- {
-       struct usbg_cmd *cmd = req->context;
-       struct se_cmd *se_cmd = &cmd->se_cmd;
-       if (req->status < 0) {
-               pr_err("%s() state %d transfer failed\n", __func__, cmd->state);
-               goto cleanup;
-       }
-       if (req->num_sgs == 0) {
-               sg_copy_from_buffer(se_cmd->t_data_sg,
-                               se_cmd->t_data_nents,
-                               cmd->data_buf,
-                               se_cmd->data_length);
-       }
-       complete(&cmd->write_complete);
-       return;
- cleanup:
-       usbg_cleanup_cmd(cmd);
- }
- static int usbg_prepare_w_request(struct usbg_cmd *cmd, struct usb_request *req)
- {
-       struct se_cmd *se_cmd = &cmd->se_cmd;
-       struct f_uas *fu = cmd->fu;
-       struct usb_gadget *gadget = fuas_to_gadget(fu);
-       if (!gadget->sg_supported) {
-               cmd->data_buf = kmalloc(se_cmd->data_length, GFP_ATOMIC);
-               if (!cmd->data_buf)
-                       return -ENOMEM;
-               req->buf = cmd->data_buf;
-       } else {
-               req->buf = NULL;
-               req->num_sgs = se_cmd->t_data_nents;
-               req->sg = se_cmd->t_data_sg;
-       }
-       req->complete = usbg_data_write_cmpl;
-       req->length = se_cmd->data_length;
-       req->context = cmd;
-       return 0;
- }
- static int usbg_send_status_response(struct se_cmd *se_cmd)
- {
-       struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
-                       se_cmd);
-       struct f_uas *fu = cmd->fu;
-       if (fu->flags & USBG_IS_BOT)
-               return bot_send_status_response(cmd);
-       else
-               return uasp_send_status_response(cmd);
- }
- static int usbg_send_write_request(struct se_cmd *se_cmd)
- {
-       struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
-                       se_cmd);
-       struct f_uas *fu = cmd->fu;
-       if (fu->flags & USBG_IS_BOT)
-               return bot_send_write_request(cmd);
-       else
-               return uasp_send_write_request(cmd);
- }
- static int usbg_send_read_response(struct se_cmd *se_cmd)
- {
-       struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
-                       se_cmd);
-       struct f_uas *fu = cmd->fu;
-       if (fu->flags & USBG_IS_BOT)
-               return bot_send_read_response(cmd);
-       else
-               return uasp_send_read_response(cmd);
- }
- static void usbg_cmd_work(struct work_struct *work)
- {
-       struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
-       struct se_cmd *se_cmd;
-       struct tcm_usbg_nexus *tv_nexus;
-       struct usbg_tpg *tpg;
-       int dir;
-       se_cmd = &cmd->se_cmd;
-       tpg = cmd->fu->tpg;
-       tv_nexus = tpg->tpg_nexus;
-       dir = get_cmd_dir(cmd->cmd_buf);
-       if (dir < 0) {
-               transport_init_se_cmd(se_cmd,
-                               tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
-                               tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
-                               cmd->prio_attr, cmd->sense_iu.sense);
-               goto out;
-       }
-       if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
-                       cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
-                       0, cmd->prio_attr, dir, TARGET_SCF_UNKNOWN_SIZE) < 0)
-               goto out;
-       return;
- out:
-       transport_send_check_condition_and_sense(se_cmd,
-                       TCM_UNSUPPORTED_SCSI_OPCODE, 1);
-       usbg_cleanup_cmd(cmd);
- }
- static int usbg_submit_command(struct f_uas *fu,
-               void *cmdbuf, unsigned int len)
- {
-       struct command_iu *cmd_iu = cmdbuf;
-       struct usbg_cmd *cmd;
-       struct usbg_tpg *tpg;
-       struct se_cmd *se_cmd;
-       struct tcm_usbg_nexus *tv_nexus;
-       u32 cmd_len;
-       int ret;
-       if (cmd_iu->iu_id != IU_ID_COMMAND) {
-               pr_err("Unsupported type %d\n", cmd_iu->iu_id);
-               return -EINVAL;
-       }
-       cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
-       if (!cmd)
-               return -ENOMEM;
-       cmd->fu = fu;
-       /* XXX until I figure out why I can't free in on complete */
-       kref_init(&cmd->ref);
-       kref_get(&cmd->ref);
-       tpg = fu->tpg;
-       cmd_len = (cmd_iu->len & ~0x3) + 16;
-       if (cmd_len > USBG_MAX_CMD)
-               goto err;
-       memcpy(cmd->cmd_buf, cmd_iu->cdb, cmd_len);
-       cmd->tag = be16_to_cpup(&cmd_iu->tag);
-       cmd->se_cmd.tag = cmd->tag;
-       if (fu->flags & USBG_USE_STREAMS) {
-               if (cmd->tag > UASP_SS_EP_COMP_NUM_STREAMS)
-                       goto err;
-               if (!cmd->tag)
-                       cmd->stream = &fu->stream[0];
-               else
-                       cmd->stream = &fu->stream[cmd->tag - 1];
-       } else {
-               cmd->stream = &fu->stream[0];
-       }
-       tv_nexus = tpg->tpg_nexus;
-       if (!tv_nexus) {
-               pr_err("Missing nexus, ignoring command\n");
-               goto err;
-       }
-       switch (cmd_iu->prio_attr & 0x7) {
-       case UAS_HEAD_TAG:
-               cmd->prio_attr = TCM_HEAD_TAG;
-               break;
-       case UAS_ORDERED_TAG:
-               cmd->prio_attr = TCM_ORDERED_TAG;
-               break;
-       case UAS_ACA:
-               cmd->prio_attr = TCM_ACA_TAG;
-               break;
-       default:
-               pr_debug_once("Unsupported prio_attr: %02x.\n",
-                               cmd_iu->prio_attr);
-       case UAS_SIMPLE_TAG:
-               cmd->prio_attr = TCM_SIMPLE_TAG;
-               break;
-       }
-       se_cmd = &cmd->se_cmd;
-       cmd->unpacked_lun = scsilun_to_int(&cmd_iu->lun);
-       INIT_WORK(&cmd->work, usbg_cmd_work);
-       ret = queue_work(tpg->workqueue, &cmd->work);
-       if (ret < 0)
-               goto err;
-       return 0;
- err:
-       kfree(cmd);
-       return -EINVAL;
- }
- static void bot_cmd_work(struct work_struct *work)
- {
-       struct usbg_cmd *cmd = container_of(work, struct usbg_cmd, work);
-       struct se_cmd *se_cmd;
-       struct tcm_usbg_nexus *tv_nexus;
-       struct usbg_tpg *tpg;
-       int dir;
-       se_cmd = &cmd->se_cmd;
-       tpg = cmd->fu->tpg;
-       tv_nexus = tpg->tpg_nexus;
-       dir = get_cmd_dir(cmd->cmd_buf);
-       if (dir < 0) {
-               transport_init_se_cmd(se_cmd,
-                               tv_nexus->tvn_se_sess->se_tpg->se_tpg_tfo,
-                               tv_nexus->tvn_se_sess, cmd->data_len, DMA_NONE,
-                               cmd->prio_attr, cmd->sense_iu.sense);
-               goto out;
-       }
-       if (target_submit_cmd(se_cmd, tv_nexus->tvn_se_sess,
-                       cmd->cmd_buf, cmd->sense_iu.sense, cmd->unpacked_lun,
-                       cmd->data_len, cmd->prio_attr, dir, 0) < 0)
-               goto out;
-       return;
- out:
-       transport_send_check_condition_and_sense(se_cmd,
-                               TCM_UNSUPPORTED_SCSI_OPCODE, 1);
-       usbg_cleanup_cmd(cmd);
- }
- static int bot_submit_command(struct f_uas *fu,
-               void *cmdbuf, unsigned int len)
- {
-       struct bulk_cb_wrap *cbw = cmdbuf;
-       struct usbg_cmd *cmd;
-       struct usbg_tpg *tpg;
-       struct se_cmd *se_cmd;
-       struct tcm_usbg_nexus *tv_nexus;
-       u32 cmd_len;
-       int ret;
-       if (cbw->Signature != cpu_to_le32(US_BULK_CB_SIGN)) {
-               pr_err("Wrong signature on CBW\n");
-               return -EINVAL;
-       }
-       if (len != 31) {
-               pr_err("Wrong length for CBW\n");
-               return -EINVAL;
-       }
-       cmd_len = cbw->Length;
-       if (cmd_len < 1 || cmd_len > 16)
-               return -EINVAL;
-       cmd = kzalloc(sizeof *cmd, GFP_ATOMIC);
-       if (!cmd)
-               return -ENOMEM;
-       cmd->fu = fu;
-       /* XXX until I figure out why I can't free in on complete */
-       kref_init(&cmd->ref);
-       kref_get(&cmd->ref);
-       tpg = fu->tpg;
-       memcpy(cmd->cmd_buf, cbw->CDB, cmd_len);
-       cmd->bot_tag = cbw->Tag;
-       tv_nexus = tpg->tpg_nexus;
-       if (!tv_nexus) {
-               pr_err("Missing nexus, ignoring command\n");
-               goto err;
-       }
-       cmd->prio_attr = TCM_SIMPLE_TAG;
-       se_cmd = &cmd->se_cmd;
-       cmd->unpacked_lun = cbw->Lun;
-       cmd->is_read = cbw->Flags & US_BULK_FLAG_IN ? 1 : 0;
-       cmd->data_len = le32_to_cpu(cbw->DataTransferLength);
-       cmd->se_cmd.tag = le32_to_cpu(cmd->bot_tag);
-       INIT_WORK(&cmd->work, bot_cmd_work);
-       ret = queue_work(tpg->workqueue, &cmd->work);
-       if (ret < 0)
-               goto err;
-       return 0;
- err:
-       kfree(cmd);
-       return -EINVAL;
- }
- /* Start fabric.c code */
- static int usbg_check_true(struct se_portal_group *se_tpg)
- {
-       return 1;
- }
- static int usbg_check_false(struct se_portal_group *se_tpg)
- {
-       return 0;
- }
- static char *usbg_get_fabric_name(void)
- {
-       return "usb_gadget";
- }
- static char *usbg_get_fabric_wwn(struct se_portal_group *se_tpg)
- {
-       struct usbg_tpg *tpg = container_of(se_tpg,
-                               struct usbg_tpg, se_tpg);
-       struct usbg_tport *tport = tpg->tport;
-       return &tport->tport_name[0];
- }
- static u16 usbg_get_tag(struct se_portal_group *se_tpg)
- {
-       struct usbg_tpg *tpg = container_of(se_tpg,
-                               struct usbg_tpg, se_tpg);
-       return tpg->tport_tpgt;
- }
- static u32 usbg_tpg_get_inst_index(struct se_portal_group *se_tpg)
- {
-       return 1;
- }
- static void usbg_cmd_release(struct kref *ref)
- {
-       struct usbg_cmd *cmd = container_of(ref, struct usbg_cmd,
-                       ref);
-       transport_generic_free_cmd(&cmd->se_cmd, 0);
- }
- static void usbg_release_cmd(struct se_cmd *se_cmd)
- {
-       struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
-                       se_cmd);
-       kfree(cmd->data_buf);
-       kfree(cmd);
-       return;
- }
- static int usbg_shutdown_session(struct se_session *se_sess)
- {
-       return 0;
- }
- static void usbg_close_session(struct se_session *se_sess)
- {
-       return;
- }
- static u32 usbg_sess_get_index(struct se_session *se_sess)
- {
-       return 0;
- }
- /*
-  * XXX Error recovery: return != 0 if we expect writes. Dunno when that could be
-  */
- static int usbg_write_pending_status(struct se_cmd *se_cmd)
- {
-       return 0;
- }
- static void usbg_set_default_node_attrs(struct se_node_acl *nacl)
- {
-       return;
- }
- static int usbg_get_cmd_state(struct se_cmd *se_cmd)
- {
-       return 0;
- }
- static void usbg_queue_tm_rsp(struct se_cmd *se_cmd)
- {
- }
- static void usbg_aborted_task(struct se_cmd *se_cmd)
- {
-       return;
- }
- static const char *usbg_check_wwn(const char *name)
- {
-       const char *n;
-       unsigned int len;
-       n = strstr(name, "naa.");
-       if (!n)
-               return NULL;
-       n += 4;
-       len = strlen(n);
-       if (len == 0 || len > USBG_NAMELEN - 1)
-               return NULL;
-       return n;
- }
- static int usbg_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
- {
-       if (!usbg_check_wwn(name))
-               return -EINVAL;
-       return 0;
- }
- struct usbg_tpg *the_only_tpg_I_currently_have;
- static struct se_portal_group *usbg_make_tpg(
-       struct se_wwn *wwn,
-       struct config_group *group,
-       const char *name)
- {
-       struct usbg_tport *tport = container_of(wwn, struct usbg_tport,
-                       tport_wwn);
-       struct usbg_tpg *tpg;
-       unsigned long tpgt;
-       int ret;
-       if (strstr(name, "tpgt_") != name)
-               return ERR_PTR(-EINVAL);
-       if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX)
-               return ERR_PTR(-EINVAL);
-       if (the_only_tpg_I_currently_have) {
-               pr_err("Until the gadget framework can't handle multiple\n");
-               pr_err("gadgets, you can't do this here.\n");
-               return ERR_PTR(-EBUSY);
-       }
-       tpg = kzalloc(sizeof(struct usbg_tpg), GFP_KERNEL);
-       if (!tpg)
-               return ERR_PTR(-ENOMEM);
-       mutex_init(&tpg->tpg_mutex);
-       atomic_set(&tpg->tpg_port_count, 0);
-       tpg->workqueue = alloc_workqueue("tcm_usb_gadget", 0, 1);
-       if (!tpg->workqueue) {
-               kfree(tpg);
-               return NULL;
-       }
-       tpg->tport = tport;
-       tpg->tport_tpgt = tpgt;
-       /*
-        * SPC doesn't assign a protocol identifier for USB-SCSI, so we
-        * pretend to be SAS..
-        */
-       ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SAS);
-       if (ret < 0) {
-               destroy_workqueue(tpg->workqueue);
-               kfree(tpg);
-               return NULL;
-       }
-       the_only_tpg_I_currently_have = tpg;
-       return &tpg->se_tpg;
- }
- static void usbg_drop_tpg(struct se_portal_group *se_tpg)
- {
-       struct usbg_tpg *tpg = container_of(se_tpg,
-                               struct usbg_tpg, se_tpg);
-       core_tpg_deregister(se_tpg);
-       destroy_workqueue(tpg->workqueue);
-       kfree(tpg);
-       the_only_tpg_I_currently_have = NULL;
- }
- static struct se_wwn *usbg_make_tport(
-       struct target_fabric_configfs *tf,
-       struct config_group *group,
-       const char *name)
- {
-       struct usbg_tport *tport;
-       const char *wnn_name;
-       u64 wwpn = 0;
-       wnn_name = usbg_check_wwn(name);
-       if (!wnn_name)
-               return ERR_PTR(-EINVAL);
-       tport = kzalloc(sizeof(struct usbg_tport), GFP_KERNEL);
-       if (!(tport))
-               return ERR_PTR(-ENOMEM);
-       tport->tport_wwpn = wwpn;
-       snprintf(tport->tport_name, sizeof(tport->tport_name), "%s", wnn_name);
-       return &tport->tport_wwn;
- }
- static void usbg_drop_tport(struct se_wwn *wwn)
- {
-       struct usbg_tport *tport = container_of(wwn,
-                               struct usbg_tport, tport_wwn);
-       kfree(tport);
- }
- /*
-  * If somebody feels like dropping the version property, go ahead.
-  */
- static ssize_t usbg_wwn_version_show(struct config_item *item, char *page)
- {
-       return sprintf(page, "usb-gadget fabric module\n");
- }
- CONFIGFS_ATTR_RO(usbg_wwn_, version);
- static struct configfs_attribute *usbg_wwn_attrs[] = {
-       &usbg_wwn_attr_version,
-       NULL,
- };
- static ssize_t tcm_usbg_tpg_enable_show(struct config_item *item, char *page)
- {
-       struct se_portal_group *se_tpg = to_tpg(item);
-       struct usbg_tpg  *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
-       return snprintf(page, PAGE_SIZE, "%u\n", tpg->gadget_connect);
- }
- static int usbg_attach(struct usbg_tpg *);
- static void usbg_detach(struct usbg_tpg *);
- static ssize_t tcm_usbg_tpg_enable_store(struct config_item *item,
-               const char *page, size_t count)
- {
-       struct se_portal_group *se_tpg = to_tpg(item);
-       struct usbg_tpg  *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
-       unsigned long op;
-       ssize_t ret;
-       ret = kstrtoul(page, 0, &op);
-       if (ret < 0)
-               return -EINVAL;
-       if (op > 1)
-               return -EINVAL;
-       if (op && tpg->gadget_connect)
-               goto out;
-       if (!op && !tpg->gadget_connect)
-               goto out;
-       if (op) {
-               ret = usbg_attach(tpg);
-               if (ret)
-                       goto out;
-       } else {
-               usbg_detach(tpg);
-       }
-       tpg->gadget_connect = op;
- out:
-       return count;
- }
- static ssize_t tcm_usbg_tpg_nexus_show(struct config_item *item, char *page)
- {
-       struct se_portal_group *se_tpg = to_tpg(item);
-       struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
-       struct tcm_usbg_nexus *tv_nexus;
-       ssize_t ret;
-       mutex_lock(&tpg->tpg_mutex);
-       tv_nexus = tpg->tpg_nexus;
-       if (!tv_nexus) {
-               ret = -ENODEV;
-               goto out;
-       }
-       ret = snprintf(page, PAGE_SIZE, "%s\n",
-                       tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
- out:
-       mutex_unlock(&tpg->tpg_mutex);
-       return ret;
- }
- static int tcm_usbg_make_nexus(struct usbg_tpg *tpg, char *name)
- {
-       struct se_portal_group *se_tpg;
-       struct tcm_usbg_nexus *tv_nexus;
-       int ret;
-       mutex_lock(&tpg->tpg_mutex);
-       if (tpg->tpg_nexus) {
-               ret = -EEXIST;
-               pr_debug("tpg->tpg_nexus already exists\n");
-               goto err_unlock;
-       }
-       se_tpg = &tpg->se_tpg;
-       ret = -ENOMEM;
-       tv_nexus = kzalloc(sizeof(*tv_nexus), GFP_KERNEL);
-       if (!tv_nexus)
-               goto err_unlock;
-       tv_nexus->tvn_se_sess = transport_init_session(TARGET_PROT_NORMAL);
-       if (IS_ERR(tv_nexus->tvn_se_sess))
-               goto err_free;
-       /*
-        * Since we are running in 'demo mode' this call with generate a
-        * struct se_node_acl for the tcm_vhost struct se_portal_group with
-        * the SCSI Initiator port name of the passed configfs group 'name'.
-        */
-       tv_nexus->tvn_se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
-                       se_tpg, name);
-       if (!tv_nexus->tvn_se_sess->se_node_acl) {
-               pr_debug("core_tpg_check_initiator_node_acl() failed"
-                               " for %s\n", name);
-               goto err_session;
-       }
-       /*
-        * Now register the TCM vHost virtual I_T Nexus as active.
-        */
-       transport_register_session(se_tpg, tv_nexus->tvn_se_sess->se_node_acl,
-                       tv_nexus->tvn_se_sess, tv_nexus);
-       tpg->tpg_nexus = tv_nexus;
-       mutex_unlock(&tpg->tpg_mutex);
-       return 0;
- err_session:
-       transport_free_session(tv_nexus->tvn_se_sess);
- err_free:
-       kfree(tv_nexus);
- err_unlock:
-       mutex_unlock(&tpg->tpg_mutex);
-       return ret;
- }
- static int tcm_usbg_drop_nexus(struct usbg_tpg *tpg)
- {
-       struct se_session *se_sess;
-       struct tcm_usbg_nexus *tv_nexus;
-       int ret = -ENODEV;
-       mutex_lock(&tpg->tpg_mutex);
-       tv_nexus = tpg->tpg_nexus;
-       if (!tv_nexus)
-               goto out;
-       se_sess = tv_nexus->tvn_se_sess;
-       if (!se_sess)
-               goto out;
-       if (atomic_read(&tpg->tpg_port_count)) {
-               ret = -EPERM;
-               pr_err("Unable to remove Host I_T Nexus with"
-                               " active TPG port count: %d\n",
-                               atomic_read(&tpg->tpg_port_count));
-               goto out;
-       }
-       pr_debug("Removing I_T Nexus to Initiator Port: %s\n",
-                       tv_nexus->tvn_se_sess->se_node_acl->initiatorname);
-       /*
-        * Release the SCSI I_T Nexus to the emulated vHost Target Port
-        */
-       transport_deregister_session(tv_nexus->tvn_se_sess);
-       tpg->tpg_nexus = NULL;
-       kfree(tv_nexus);
-       ret = 0;
- out:
-       mutex_unlock(&tpg->tpg_mutex);
-       return ret;
- }
- static ssize_t tcm_usbg_tpg_nexus_store(struct config_item *item,
-               const char *page, size_t count)
- {
-       struct se_portal_group *se_tpg = to_tpg(item);
-       struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
-       unsigned char i_port[USBG_NAMELEN], *ptr;
-       int ret;
-       if (!strncmp(page, "NULL", 4)) {
-               ret = tcm_usbg_drop_nexus(tpg);
-               return (!ret) ? count : ret;
-       }
-       if (strlen(page) >= USBG_NAMELEN) {
-               pr_err("Emulated NAA Sas Address: %s, exceeds"
-                               " max: %d\n", page, USBG_NAMELEN);
-               return -EINVAL;
-       }
-       snprintf(i_port, USBG_NAMELEN, "%s", page);
-       ptr = strstr(i_port, "naa.");
-       if (!ptr) {
-               pr_err("Missing 'naa.' prefix\n");
-               return -EINVAL;
-       }
-       if (i_port[strlen(i_port) - 1] == '\n')
-               i_port[strlen(i_port) - 1] = '\0';
-       ret = tcm_usbg_make_nexus(tpg, &i_port[4]);
-       if (ret < 0)
-               return ret;
-       return count;
- }
- CONFIGFS_ATTR(tcm_usbg_tpg_, enable);
- CONFIGFS_ATTR(tcm_usbg_tpg_, nexus);
- static struct configfs_attribute *usbg_base_attrs[] = {
-       &tcm_usbg_tpg_attr_enable,
-       &tcm_usbg_tpg_attr_nexus,
-       NULL,
- };
- static int usbg_port_link(struct se_portal_group *se_tpg, struct se_lun *lun)
- {
-       struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
-       atomic_inc(&tpg->tpg_port_count);
-       smp_mb__after_atomic();
-       return 0;
- }
- static void usbg_port_unlink(struct se_portal_group *se_tpg,
-               struct se_lun *se_lun)
- {
-       struct usbg_tpg *tpg = container_of(se_tpg, struct usbg_tpg, se_tpg);
-       atomic_dec(&tpg->tpg_port_count);
-       smp_mb__after_atomic();
- }
- static int usbg_check_stop_free(struct se_cmd *se_cmd)
- {
-       struct usbg_cmd *cmd = container_of(se_cmd, struct usbg_cmd,
-                       se_cmd);
-       kref_put(&cmd->ref, usbg_cmd_release);
-       return 1;
- }
- static const struct target_core_fabric_ops usbg_ops = {
-       .module                         = THIS_MODULE,
-       .name                           = "usb_gadget",
-       .get_fabric_name                = usbg_get_fabric_name,
-       .tpg_get_wwn                    = usbg_get_fabric_wwn,
-       .tpg_get_tag                    = usbg_get_tag,
-       .tpg_check_demo_mode            = usbg_check_true,
-       .tpg_check_demo_mode_cache      = usbg_check_false,
-       .tpg_check_demo_mode_write_protect = usbg_check_false,
-       .tpg_check_prod_mode_write_protect = usbg_check_false,
-       .tpg_get_inst_index             = usbg_tpg_get_inst_index,
-       .release_cmd                    = usbg_release_cmd,
-       .shutdown_session               = usbg_shutdown_session,
-       .close_session                  = usbg_close_session,
-       .sess_get_index                 = usbg_sess_get_index,
-       .sess_get_initiator_sid         = NULL,
-       .write_pending                  = usbg_send_write_request,
-       .write_pending_status           = usbg_write_pending_status,
-       .set_default_node_attributes    = usbg_set_default_node_attrs,
-       .get_cmd_state                  = usbg_get_cmd_state,
-       .queue_data_in                  = usbg_send_read_response,
-       .queue_status                   = usbg_send_status_response,
-       .queue_tm_rsp                   = usbg_queue_tm_rsp,
-       .aborted_task                   = usbg_aborted_task,
-       .check_stop_free                = usbg_check_stop_free,
-       .fabric_make_wwn                = usbg_make_tport,
-       .fabric_drop_wwn                = usbg_drop_tport,
-       .fabric_make_tpg                = usbg_make_tpg,
-       .fabric_drop_tpg                = usbg_drop_tpg,
-       .fabric_post_link               = usbg_port_link,
-       .fabric_pre_unlink              = usbg_port_unlink,
-       .fabric_init_nodeacl            = usbg_init_nodeacl,
-       .tfc_wwn_attrs                  = usbg_wwn_attrs,
-       .tfc_tpg_base_attrs             = usbg_base_attrs,
- };
- /* Start gadget.c code */
- static struct usb_interface_descriptor bot_intf_desc = {
-       .bLength =              sizeof(bot_intf_desc),
-       .bDescriptorType =      USB_DT_INTERFACE,
-       .bNumEndpoints =        2,
-       .bAlternateSetting =    USB_G_ALT_INT_BBB,
-       .bInterfaceClass =      USB_CLASS_MASS_STORAGE,
-       .bInterfaceSubClass =   USB_SC_SCSI,
-       .bInterfaceProtocol =   USB_PR_BULK,
- };
- static struct usb_interface_descriptor uasp_intf_desc = {
-       .bLength =              sizeof(uasp_intf_desc),
-       .bDescriptorType =      USB_DT_INTERFACE,
-       .bNumEndpoints =        4,
-       .bAlternateSetting =    USB_G_ALT_INT_UAS,
-       .bInterfaceClass =      USB_CLASS_MASS_STORAGE,
-       .bInterfaceSubClass =   USB_SC_SCSI,
-       .bInterfaceProtocol =   USB_PR_UAS,
- };
- static struct usb_endpoint_descriptor uasp_bi_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_IN,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(512),
- };
- static struct usb_endpoint_descriptor uasp_fs_bi_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_IN,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
- };
- static struct usb_pipe_usage_descriptor uasp_bi_pipe_desc = {
-       .bLength =              sizeof(uasp_bi_pipe_desc),
-       .bDescriptorType =      USB_DT_PIPE_USAGE,
-       .bPipeID =              DATA_IN_PIPE_ID,
- };
- static struct usb_endpoint_descriptor uasp_ss_bi_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_IN,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(1024),
- };
- static struct usb_ss_ep_comp_descriptor uasp_bi_ep_comp_desc = {
-       .bLength =              sizeof(uasp_bi_ep_comp_desc),
-       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
-       .bMaxBurst =            0,
-       .bmAttributes =         UASP_SS_EP_COMP_LOG_STREAMS,
-       .wBytesPerInterval =    0,
- };
- static struct usb_ss_ep_comp_descriptor bot_bi_ep_comp_desc = {
-       .bLength =              sizeof(bot_bi_ep_comp_desc),
-       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
-       .bMaxBurst =            0,
- };
- static struct usb_endpoint_descriptor uasp_bo_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_OUT,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(512),
- };
- static struct usb_endpoint_descriptor uasp_fs_bo_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_OUT,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
- };
- static struct usb_pipe_usage_descriptor uasp_bo_pipe_desc = {
-       .bLength =              sizeof(uasp_bo_pipe_desc),
-       .bDescriptorType =      USB_DT_PIPE_USAGE,
-       .bPipeID =              DATA_OUT_PIPE_ID,
- };
- static struct usb_endpoint_descriptor uasp_ss_bo_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_OUT,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(0x400),
- };
- static struct usb_ss_ep_comp_descriptor uasp_bo_ep_comp_desc = {
-       .bLength =              sizeof(uasp_bo_ep_comp_desc),
-       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
-       .bmAttributes =         UASP_SS_EP_COMP_LOG_STREAMS,
- };
- static struct usb_ss_ep_comp_descriptor bot_bo_ep_comp_desc = {
-       .bLength =              sizeof(bot_bo_ep_comp_desc),
-       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
- };
- static struct usb_endpoint_descriptor uasp_status_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_IN,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(512),
- };
- static struct usb_endpoint_descriptor uasp_fs_status_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_IN,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
- };
- static struct usb_pipe_usage_descriptor uasp_status_pipe_desc = {
-       .bLength =              sizeof(uasp_status_pipe_desc),
-       .bDescriptorType =      USB_DT_PIPE_USAGE,
-       .bPipeID =              STATUS_PIPE_ID,
- };
- static struct usb_endpoint_descriptor uasp_ss_status_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_IN,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(1024),
- };
- static struct usb_ss_ep_comp_descriptor uasp_status_in_ep_comp_desc = {
-       .bLength =              sizeof(uasp_status_in_ep_comp_desc),
-       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
-       .bmAttributes =         UASP_SS_EP_COMP_LOG_STREAMS,
- };
- static struct usb_endpoint_descriptor uasp_cmd_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_OUT,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(512),
- };
- static struct usb_endpoint_descriptor uasp_fs_cmd_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_OUT,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
- };
- static struct usb_pipe_usage_descriptor uasp_cmd_pipe_desc = {
-       .bLength =              sizeof(uasp_cmd_pipe_desc),
-       .bDescriptorType =      USB_DT_PIPE_USAGE,
-       .bPipeID =              CMD_PIPE_ID,
- };
- static struct usb_endpoint_descriptor uasp_ss_cmd_desc = {
-       .bLength =              USB_DT_ENDPOINT_SIZE,
-       .bDescriptorType =      USB_DT_ENDPOINT,
-       .bEndpointAddress =     USB_DIR_OUT,
-       .bmAttributes =         USB_ENDPOINT_XFER_BULK,
-       .wMaxPacketSize =       cpu_to_le16(1024),
- };
- static struct usb_ss_ep_comp_descriptor uasp_cmd_comp_desc = {
-       .bLength =              sizeof(uasp_cmd_comp_desc),
-       .bDescriptorType =      USB_DT_SS_ENDPOINT_COMP,
- };
- static struct usb_descriptor_header *uasp_fs_function_desc[] = {
-       (struct usb_descriptor_header *) &bot_intf_desc,
-       (struct usb_descriptor_header *) &uasp_fs_bi_desc,
-       (struct usb_descriptor_header *) &uasp_fs_bo_desc,
-       (struct usb_descriptor_header *) &uasp_intf_desc,
-       (struct usb_descriptor_header *) &uasp_fs_bi_desc,
-       (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_fs_bo_desc,
-       (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_fs_status_desc,
-       (struct usb_descriptor_header *) &uasp_status_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_fs_cmd_desc,
-       (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
-       NULL,
- };
- static struct usb_descriptor_header *uasp_hs_function_desc[] = {
-       (struct usb_descriptor_header *) &bot_intf_desc,
-       (struct usb_descriptor_header *) &uasp_bi_desc,
-       (struct usb_descriptor_header *) &uasp_bo_desc,
-       (struct usb_descriptor_header *) &uasp_intf_desc,
-       (struct usb_descriptor_header *) &uasp_bi_desc,
-       (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_bo_desc,
-       (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_status_desc,
-       (struct usb_descriptor_header *) &uasp_status_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_cmd_desc,
-       (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
-       NULL,
- };
- static struct usb_descriptor_header *uasp_ss_function_desc[] = {
-       (struct usb_descriptor_header *) &bot_intf_desc,
-       (struct usb_descriptor_header *) &uasp_ss_bi_desc,
-       (struct usb_descriptor_header *) &bot_bi_ep_comp_desc,
-       (struct usb_descriptor_header *) &uasp_ss_bo_desc,
-       (struct usb_descriptor_header *) &bot_bo_ep_comp_desc,
-       (struct usb_descriptor_header *) &uasp_intf_desc,
-       (struct usb_descriptor_header *) &uasp_ss_bi_desc,
-       (struct usb_descriptor_header *) &uasp_bi_ep_comp_desc,
-       (struct usb_descriptor_header *) &uasp_bi_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_ss_bo_desc,
-       (struct usb_descriptor_header *) &uasp_bo_ep_comp_desc,
-       (struct usb_descriptor_header *) &uasp_bo_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_ss_status_desc,
-       (struct usb_descriptor_header *) &uasp_status_in_ep_comp_desc,
-       (struct usb_descriptor_header *) &uasp_status_pipe_desc,
-       (struct usb_descriptor_header *) &uasp_ss_cmd_desc,
-       (struct usb_descriptor_header *) &uasp_cmd_comp_desc,
-       (struct usb_descriptor_header *) &uasp_cmd_pipe_desc,
-       NULL,
- };
  #define UAS_VENDOR_ID 0x0525  /* NetChip */
  #define UAS_PRODUCT_ID        0xa4a5  /* Linux-USB File-backed Storage Gadget */
  
  static struct usb_device_descriptor usbg_device_desc = {
        .bLength =              sizeof(usbg_device_desc),
        .bDescriptorType =      USB_DT_DEVICE,
 -      .bcdUSB =               cpu_to_le16(0x0200),
 +      /* .bcdUSB = DYNAMIC */
        .bDeviceClass =         USB_CLASS_PER_INTERFACE,
        .idVendor =             cpu_to_le16(UAS_VENDOR_ID),
        .idProduct =            cpu_to_le16(UAS_PRODUCT_ID),
        .bNumConfigurations =   1,
  };
  
+ #define USB_G_STR_CONFIG USB_GADGET_FIRST_AVAIL_IDX
  static struct usb_string      usbg_us_strings[] = {
        [USB_GADGET_MANUFACTURER_IDX].s = "Target Manufactor",
        [USB_GADGET_PRODUCT_IDX].s      = "Target Product",
        [USB_GADGET_SERIAL_IDX].s       = "000000000001",
        [USB_G_STR_CONFIG].s            = "default config",
-       [USB_G_STR_INT_UAS].s           = "USB Attached SCSI",
-       [USB_G_STR_INT_BBB].s           = "Bulk Only Transport",
        { },
  };
  
@@@ -2001,184 -58,42 +58,42 @@@ static struct usb_gadget_strings *usbg_
        NULL,
  };
  
- static int guas_unbind(struct usb_composite_dev *cdev)
- {
-       return 0;
- }
- static struct usb_configuration usbg_config_driver = {
-       .label                  = "Linux Target",
-       .bConfigurationValue    = 1,
-       .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
- };
+ static struct usb_function_instance *fi_tcm;
+ static struct usb_function *f_tcm;
  
- static int usbg_bind(struct usb_configuration *c, struct usb_function *f)
+ static int guas_unbind(struct usb_composite_dev *cdev)
  {
-       struct f_uas            *fu = to_f_uas(f);
-       struct usb_gadget       *gadget = c->cdev->gadget;
-       struct usb_ep           *ep;
-       int                     iface;
-       int                     ret;
-       iface = usb_interface_id(c, f);
-       if (iface < 0)
-               return iface;
-       bot_intf_desc.bInterfaceNumber = iface;
-       uasp_intf_desc.bInterfaceNumber = iface;
-       fu->iface = iface;
-       ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bi_desc,
-                       &uasp_bi_ep_comp_desc);
-       if (!ep)
-               goto ep_fail;
-       fu->ep_in = ep;
-       ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_bo_desc,
-                       &uasp_bo_ep_comp_desc);
-       if (!ep)
-               goto ep_fail;
-       fu->ep_out = ep;
-       ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_status_desc,
-                       &uasp_status_in_ep_comp_desc);
-       if (!ep)
-               goto ep_fail;
-       fu->ep_status = ep;
-       ep = usb_ep_autoconfig_ss(gadget, &uasp_ss_cmd_desc,
-                       &uasp_cmd_comp_desc);
-       if (!ep)
-               goto ep_fail;
-       fu->ep_cmd = ep;
-       /* Assume endpoint addresses are the same for both speeds */
-       uasp_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
-       uasp_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
-       uasp_status_desc.bEndpointAddress =
-               uasp_ss_status_desc.bEndpointAddress;
-       uasp_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
-       uasp_fs_bi_desc.bEndpointAddress = uasp_ss_bi_desc.bEndpointAddress;
-       uasp_fs_bo_desc.bEndpointAddress = uasp_ss_bo_desc.bEndpointAddress;
-       uasp_fs_status_desc.bEndpointAddress =
-               uasp_ss_status_desc.bEndpointAddress;
-       uasp_fs_cmd_desc.bEndpointAddress = uasp_ss_cmd_desc.bEndpointAddress;
-       ret = usb_assign_descriptors(f, uasp_fs_function_desc,
-                       uasp_hs_function_desc, uasp_ss_function_desc);
-       if (ret)
-               goto ep_fail;
+       if (!IS_ERR_OR_NULL(f_tcm))
+               usb_put_function(f_tcm);
  
        return 0;
- ep_fail:
-       pr_err("Can't claim all required eps\n");
-       return -ENOTSUPP;
- }
- static void usbg_unbind(struct usb_configuration *c, struct usb_function *f)
- {
-       struct f_uas *fu = to_f_uas(f);
-       usb_free_all_descriptors(f);
-       kfree(fu);
- }
- struct guas_setup_wq {
-       struct work_struct work;
-       struct f_uas *fu;
-       unsigned int alt;
- };
- static void usbg_delayed_set_alt(struct work_struct *wq)
- {
-       struct guas_setup_wq *work = container_of(wq, struct guas_setup_wq,
-                       work);
-       struct f_uas *fu = work->fu;
-       int alt = work->alt;
-       kfree(work);
-       if (fu->flags & USBG_IS_BOT)
-               bot_cleanup_old_alt(fu);
-       if (fu->flags & USBG_IS_UAS)
-               uasp_cleanup_old_alt(fu);
-       if (alt == USB_G_ALT_INT_BBB)
-               bot_set_alt(fu);
-       else if (alt == USB_G_ALT_INT_UAS)
-               uasp_set_alt(fu);
-       usb_composite_setup_continue(fu->function.config->cdev);
  }
  
- static int usbg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
+ static int tcm_do_config(struct usb_configuration *c)
  {
-       struct f_uas *fu = to_f_uas(f);
+       int status;
  
-       if ((alt == USB_G_ALT_INT_BBB) || (alt == USB_G_ALT_INT_UAS)) {
-               struct guas_setup_wq *work;
+       f_tcm = usb_get_function(fi_tcm);
+       if (IS_ERR(f_tcm))
+               return PTR_ERR(f_tcm);
  
-               work = kmalloc(sizeof(*work), GFP_ATOMIC);
-               if (!work)
-                       return -ENOMEM;
-               INIT_WORK(&work->work, usbg_delayed_set_alt);
-               work->fu = fu;
-               work->alt = alt;
-               schedule_work(&work->work);
-               return USB_GADGET_DELAYED_STATUS;
+       status = usb_add_function(c, f_tcm);
+       if (status < 0) {
+               usb_put_function(f_tcm);
+               return status;
        }
-       return -EOPNOTSUPP;
- }
- static void usbg_disable(struct usb_function *f)
- {
-       struct f_uas *fu = to_f_uas(f);
-       if (fu->flags & USBG_IS_UAS)
-               uasp_cleanup_old_alt(fu);
-       else if (fu->flags & USBG_IS_BOT)
-               bot_cleanup_old_alt(fu);
-       fu->flags = 0;
- }
- static int usbg_setup(struct usb_function *f,
-               const struct usb_ctrlrequest *ctrl)
- {
-       struct f_uas *fu = to_f_uas(f);
-       if (!(fu->flags & USBG_IS_BOT))
-               return -EOPNOTSUPP;
  
-       return usbg_bot_setup(f, ctrl);
+       return 0;
  }
  
- static int usbg_cfg_bind(struct usb_configuration *c)
- {
-       struct f_uas *fu;
-       int ret;
-       fu = kzalloc(sizeof(*fu), GFP_KERNEL);
-       if (!fu)
-               return -ENOMEM;
-       fu->function.name = "Target Function";
-       fu->function.bind = usbg_bind;
-       fu->function.unbind = usbg_unbind;
-       fu->function.set_alt = usbg_set_alt;
-       fu->function.setup = usbg_setup;
-       fu->function.disable = usbg_disable;
-       fu->tpg = the_only_tpg_I_currently_have;
-       bot_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_BBB].id;
-       uasp_intf_desc.iInterface = usbg_us_strings[USB_G_STR_INT_UAS].id;
-       ret = usb_add_function(c, &fu->function);
-       if (ret)
-               goto err;
+ static struct usb_configuration usbg_config_driver = {
+       .label                  = "Linux Target",
+       .bConfigurationValue    = 1,
+       .bmAttributes           = USB_CONFIG_ATT_SELFPOWER,
+ };
  
-       return 0;
- err:
-       kfree(fu);
-       return ret;
- }
+ static int usbg_attach(struct usb_function_instance *f);
+ static void usbg_detach(struct usb_function_instance *f);
  
  static int usb_target_bind(struct usb_composite_dev *cdev)
  {
        usbg_config_driver.iConfiguration =
                usbg_us_strings[USB_G_STR_CONFIG].id;
  
-       ret = usb_add_config(cdev, &usbg_config_driver,
-                       usbg_cfg_bind);
+       ret = usb_add_config(cdev, &usbg_config_driver, tcm_do_config);
        if (ret)
                return ret;
        usb_composite_overwrite_options(cdev, &coverwrite);
@@@ -2213,25 -127,44 +127,44 @@@ static struct usb_composite_driver usbg
        .unbind         = guas_unbind,
  };
  
- static int usbg_attach(struct usbg_tpg *tpg)
+ static int usbg_attach(struct usb_function_instance *f)
  {
        return usb_composite_probe(&usbg_driver);
  }
  
- static void usbg_detach(struct usbg_tpg *tpg)
+ static void usbg_detach(struct usb_function_instance *f)
  {
        usb_composite_unregister(&usbg_driver);
  }
  
  static int __init usb_target_gadget_init(void)
  {
-       return target_register_template(&usbg_ops);
+       struct f_tcm_opts *tcm_opts;
+       fi_tcm = usb_get_function_instance("tcm");
+       if (IS_ERR(fi_tcm))
+               return PTR_ERR(fi_tcm);
+       tcm_opts = container_of(fi_tcm, struct f_tcm_opts, func_inst);
+       mutex_lock(&tcm_opts->dep_lock);
+       tcm_opts->tcm_register_callback = usbg_attach;
+       tcm_opts->tcm_unregister_callback = usbg_detach;
+       tcm_opts->dependent = THIS_MODULE;
+       tcm_opts->can_attach = true;
+       tcm_opts->has_dep = true;
+       mutex_unlock(&tcm_opts->dep_lock);
+       fi_tcm->set_inst_name(fi_tcm, "tcm-legacy");
+       return 0;
  }
  module_init(usb_target_gadget_init);
  
  static void __exit usb_target_gadget_exit(void)
  {
-       target_unregister_template(&usbg_ops);
+       if (!IS_ERR_OR_NULL(fi_tcm))
+               usb_put_function_instance(fi_tcm);
  }
  module_exit(usb_target_gadget_exit);
  
diff --combined fs/configfs/dir.c
index 7ae97e83f12154a52b90642d2a7b557e956c86e1,8fd032ad692092095edbfd586552cb3f035cd193..cab612b2ae767bbe2ffeb187a6f630f891cd52f3
@@@ -255,12 -255,6 +255,12 @@@ static void configfs_init_file(struct i
        inode->i_fop = &configfs_file_operations;
  }
  
 +static void configfs_init_bin_file(struct inode *inode)
 +{
 +      inode->i_size = 0;
 +      inode->i_fop = &configfs_bin_file_operations;
 +}
 +
  static void init_symlink(struct inode * inode)
  {
        inode->i_op = &configfs_symlink_inode_operations;
@@@ -429,9 -423,7 +429,9 @@@ static int configfs_attach_attr(struct 
        spin_unlock(&configfs_dirent_lock);
  
        error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
 -                              configfs_init_file);
 +                              (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ?
 +                                      configfs_init_bin_file :
 +                                      configfs_init_file);
        if (error) {
                configfs_put(sd);
                return error;
@@@ -591,7 -583,6 +591,7 @@@ static int populate_attrs(struct config
  {
        struct config_item_type *t = item->ci_type;
        struct configfs_attribute *attr;
 +      struct configfs_bin_attribute *bin_attr;
        int error = 0;
        int i;
  
                                break;
                }
        }
 +      if (t->ct_bin_attrs) {
 +              for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
 +                      error = configfs_create_bin_file(item, bin_attr);
 +                      if (error)
 +                              break;
 +              }
 +      }
  
        if (error)
                detach_attrs(item);
        return ret;
  }
  
+ static int configfs_do_depend_item(struct dentry *subsys_dentry,
+                                  struct config_item *target)
+ {
+       struct configfs_dirent *p;
+       int ret;
+       spin_lock(&configfs_dirent_lock);
+       /* Scan the tree, return 0 if found */
+       ret = configfs_depend_prep(subsys_dentry, target);
+       if (ret)
+               goto out_unlock_dirent_lock;
+       /*
+        * We are sure that the item is not about to be removed by rmdir(), and
+        * not in the middle of attachment by mkdir().
+        */
+       p = target->ci_dentry->d_fsdata;
+       p->s_dependent_count += 1;
+ out_unlock_dirent_lock:
+       spin_unlock(&configfs_dirent_lock);
+       return ret;
+ }
+ static inline struct configfs_dirent *
+ configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
+                           struct config_item *subsys_item)
+ {
+       struct configfs_dirent *p;
+       struct configfs_dirent *ret = NULL;
+       list_for_each_entry(p, &root_sd->s_children, s_sibling) {
+               if (p->s_type & CONFIGFS_DIR &&
+                   p->s_element == subsys_item) {
+                       ret = p;
+                       break;
+               }
+       }
+       return ret;
+ }
  int configfs_depend_item(struct configfs_subsystem *subsys,
                         struct config_item *target)
  {
        int ret;
-       struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
+       struct configfs_dirent *subsys_sd;
        struct config_item *s_item = &subsys->su_group.cg_item;
        struct dentry *root;
  
         */
        mutex_lock(&d_inode(root)->i_mutex);
  
-       root_sd = root->d_fsdata;
-       list_for_each_entry(p, &root_sd->s_children, s_sibling) {
-               if (p->s_type & CONFIGFS_DIR) {
-                       if (p->s_element == s_item) {
-                               subsys_sd = p;
-                               break;
-                       }
-               }
-       }
+       subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
        if (!subsys_sd) {
                ret = -ENOENT;
                goto out_unlock_fs;
        }
  
        /* Ok, now we can trust subsys/s_item */
+       ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
  
-       spin_lock(&configfs_dirent_lock);
-       /* Scan the tree, return 0 if found */
-       ret = configfs_depend_prep(subsys_sd->s_dentry, target);
-       if (ret)
-               goto out_unlock_dirent_lock;
-       /*
-        * We are sure that the item is not about to be removed by rmdir(), and
-        * not in the middle of attachment by mkdir().
-        */
-       p = target->ci_dentry->d_fsdata;
-       p->s_dependent_count += 1;
- out_unlock_dirent_lock:
-       spin_unlock(&configfs_dirent_lock);
  out_unlock_fs:
        mutex_unlock(&d_inode(root)->i_mutex);
  
@@@ -1144,8 -1148,7 +1164,7 @@@ EXPORT_SYMBOL(configfs_depend_item)
   * configfs_depend_item() because we know that that the client driver is
   * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
   */
- void configfs_undepend_item(struct configfs_subsystem *subsys,
-                           struct config_item *target)
+ void configfs_undepend_item(struct config_item *target)
  {
        struct configfs_dirent *sd;
  
  }
  EXPORT_SYMBOL(configfs_undepend_item);
  
+ /*
+  * caller_subsys is a caller's subsystem not target's. This is used to
+  * determine if we should lock root and check subsys or not. When we are
+  * in the same subsystem as our target there is no need to do locking as
+  * we know that subsys is valid and is not unregistered during this function
+  * as we are called from callback of one of his children and VFS holds a lock
+  * on some inode. Otherwise we have to lock our root to  ensure that target's
+  * subsystem it is not unregistered during this function.
+  */
+ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
+                                 struct config_item *target)
+ {
+       struct configfs_subsystem *target_subsys;
+       struct config_group *root, *parent;
+       struct configfs_dirent *subsys_sd;
+       int ret = -ENOENT;
+       /* Disallow this function for configfs root */
+       if (configfs_is_root(target))
+               return -EINVAL;
+       parent = target->ci_group;
+       /*
+        * This may happen when someone is trying to depend root
+        * directory of some subsystem
+        */
+       if (configfs_is_root(&parent->cg_item)) {
+               target_subsys = to_configfs_subsystem(to_config_group(target));
+               root = parent;
+       } else {
+               target_subsys = parent->cg_subsys;
+               /* Find a cofnigfs root as we may need it for locking */
+               for (root = parent; !configfs_is_root(&root->cg_item);
+                    root = root->cg_item.ci_group)
+                       ;
+       }
+       if (target_subsys != caller_subsys) {
+               /*
+                * We are in other configfs subsystem, so we have to do
+                * additional locking to prevent other subsystem from being
+                * unregistered
+                */
+               mutex_lock(&d_inode(root->cg_item.ci_dentry)->i_mutex);
+               /*
+                * As we are trying to depend item from other subsystem
+                * we have to check if this subsystem is still registered
+                */
+               subsys_sd = configfs_find_subsys_dentry(
+                               root->cg_item.ci_dentry->d_fsdata,
+                               &target_subsys->su_group.cg_item);
+               if (!subsys_sd)
+                       goto out_root_unlock;
+       } else {
+               subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
+       }
+       /* Now we can execute core of depend item */
+       ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
+       if (target_subsys != caller_subsys)
+ out_root_unlock:
+               /*
+                * We were called from subsystem other than our target so we
+                * took some locks so now it's time to release them
+                */
+               mutex_unlock(&d_inode(root->cg_item.ci_dentry)->i_mutex);
+       return ret;
+ }
+ EXPORT_SYMBOL(configfs_depend_item_unlocked);
  static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  {
        int ret = 0;
diff --combined include/linux/configfs.h
index f7300d023dbee10be54beed582718066a162c368,7ee1a014c56b26fb84359e40719a583611cd59eb..f8165c129ccb260498b44e7451153a9281a0a79a
@@@ -51,7 -51,6 +51,7 @@@ struct module
  struct configfs_item_operations;
  struct configfs_group_operations;
  struct configfs_attribute;
 +struct configfs_bin_attribute;
  struct configfs_subsystem;
  
  struct config_item {
@@@ -85,7 -84,6 +85,7 @@@ struct config_item_type 
        struct configfs_item_operations         *ct_item_ops;
        struct configfs_group_operations        *ct_group_ops;
        struct configfs_attribute               **ct_attrs;
 +      struct configfs_bin_attribute           **ct_bin_attrs;
  };
  
  /**
@@@ -156,54 -154,6 +156,54 @@@ static struct configfs_attribute _pfx##
        .store          = _pfx##_name##_store,          \
  }
  
 +struct file;
 +struct vm_area_struct;
 +
 +struct configfs_bin_attribute {
 +      struct configfs_attribute cb_attr;      /* std. attribute */
 +      void *cb_private;                       /* for user       */
 +      size_t cb_max_size;                     /* max core size  */
 +      ssize_t (*read)(struct config_item *, void *, size_t);
 +      ssize_t (*write)(struct config_item *, const void *, size_t);
 +};
 +
 +#define CONFIGFS_BIN_ATTR(_pfx, _name, _priv, _maxsz)         \
 +static struct configfs_bin_attribute _pfx##attr_##_name = {   \
 +      .cb_attr = {                                            \
 +              .ca_name        = __stringify(_name),           \
 +              .ca_mode        = S_IRUGO | S_IWUSR,            \
 +              .ca_owner       = THIS_MODULE,                  \
 +      },                                                      \
 +      .cb_private     = _priv,                                \
 +      .cb_max_size    = _maxsz,                               \
 +      .read           = _pfx##_name##_read,                   \
 +      .write          = _pfx##_name##_write,                  \
 +}
 +
 +#define CONFIGFS_BIN_ATTR_RO(_pfx, _name, _priv, _maxsz)      \
 +static struct configfs_attribute _pfx##attr_##_name = {               \
 +      .cb_attr = {                                            \
 +              .ca_name        = __stringify(_name),           \
 +              .ca_mode        = S_IRUGO,                      \
 +              .ca_owner       = THIS_MODULE,                  \
 +      },                                                      \
 +      .cb_private     = _priv,                                \
 +      .cb_max_size    = _maxsz,                               \
 +      .read           = _pfx##_name##_read,                   \
 +}
 +
 +#define CONFIGFS_BIN_ATTR_WO(_pfx, _name, _priv, _maxsz)      \
 +static struct configfs_attribute _pfx##attr_##_name = {               \
 +      .cb_attr = {                                            \
 +              .ca_name        = __stringify(_name),           \
 +              .ca_mode        = S_IWUSR,                      \
 +              .ca_owner       = THIS_MODULE,                  \
 +      },                                                      \
 +      .cb_private     = _priv,                                \
 +      .cb_max_size    = _maxsz,                               \
 +      .write          = _pfx##_name##_write,                  \
 +}
 +
  /*
   * If allow_link() exists, the item can symlink(2) out to other
   * items.  If the item is a group, it may support mkdir(2).
@@@ -259,7 -209,24 +259,24 @@@ void configfs_unregister_default_group(
  
  /* These functions can sleep and can alloc with GFP_KERNEL */
  /* WARNING: These cannot be called underneath configfs callbacks!! */
- int configfs_depend_item(struct configfs_subsystem *subsys, struct config_item *target);
- void configfs_undepend_item(struct configfs_subsystem *subsys, struct config_item *target);
+ int configfs_depend_item(struct configfs_subsystem *subsys,
+                        struct config_item *target);
+ void configfs_undepend_item(struct config_item *target);
+ /*
+  * These functions can sleep and can alloc with GFP_KERNEL
+  * NOTE: These should be called only underneath configfs callbacks.
+  * NOTE: First parameter is a caller's subsystem, not target's.
+  * WARNING: These cannot be called on newly created item
+  *        (in make_group()/make_item() callback)
+  */
+ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
+                                 struct config_item *target);
+ static inline void configfs_undepend_item_unlocked(struct config_item *target)
+ {
+       configfs_undepend_item(target);
+ }
  
  #endif /* _CONFIGFS_H_ */
index aabf0aca017157a72dd84accb5975c87fe98e79b,a4bed07f92a93b5845053ddda7be039b4d6e22b9..5d82816cc4e3dbb34068748d95dd86b3bbb5a537
@@@ -63,6 -63,8 +63,8 @@@
  #define DA_UNMAP_GRANULARITY_DEFAULT          0
  /* Default unmap_granularity_alignment */
  #define DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT        0
+ /* Default unmap_zeroes_data */
+ #define DA_UNMAP_ZEROES_DATA_DEFAULT          0
  /* Default max_write_same_len, disabled by default */
  #define DA_MAX_WRITE_SAME_LEN                 0
  /* Use a model alias based on the configfs backend device name */
@@@ -474,7 -476,7 +476,7 @@@ struct se_cmd 
        struct completion       cmd_wait_comp;
        const struct target_core_fabric_ops *se_tfo;
        sense_reason_t          (*execute_cmd)(struct se_cmd *);
 -      sense_reason_t (*transport_complete_callback)(struct se_cmd *, bool);
 +      sense_reason_t (*transport_complete_callback)(struct se_cmd *, bool, int *);
        void                    *protocol_data;
  
        unsigned char           *t_task_cdb;
        unsigned int            t_prot_nents;
        sense_reason_t          pi_err;
        sector_t                bad_sector;
+       int                     cpuid;
  };
  
  struct se_ua {
@@@ -674,6 -677,7 +677,7 @@@ struct se_dev_attrib 
        int             force_pr_aptpl;
        int             is_nonrot;
        int             emulate_rest_reord;
+       int             unmap_zeroes_data;
        u32             hw_block_size;
        u32             block_size;
        u32             hw_max_sectors;
@@@ -864,8 -868,6 +868,6 @@@ struct se_portal_group 
         * Negative values can be used by fabric drivers for internal use TPGs.
         */
        int                     proto_id;
-       /* Number of ACLed Initiator Nodes for this TPG */
-       u32                     num_node_acls;
        /* Used for PR SPEC_I_PT=1 and REGISTER_AND_MOVE */
        atomic_t                tpg_pr_ref_count;
        /* Spinlock for adding/removing ACLed Nodes */
This page took 0.215222 seconds and 4 git commands to generate.