25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <scsi/scsi.h>
33 #include <asm/iommu.h>
39 #define INITIAL_SRP_LIMIT 16
40 #define DEFAULT_MAX_SECTORS 256
42 #define TGT_NAME "ibmvstgt"
47 #define h_copy_rdma(l, sa, sb, da, db) \
48 plpar_hcall_norets(H_COPY_RDMA, l, sa, sb, da, db)
49 #define h_send_crq(ua, l, h) \
50 plpar_hcall_norets(H_SEND_CRQ, ua, l, h)
51 #define h_reg_crq(ua, tok, sz)\
52 plpar_hcall_norets(H_REG_CRQ, ua, tok, sz);
53 #define h_free_crq(ua) \
54 plpar_hcall_norets(H_FREE_CRQ, ua);
57 #define eprintk(fmt, args...) \
59 printk("%s(%d) " fmt, __func__, __LINE__, ##args); \
62 #define dprintk(fmt, args...)
84 static char system_id[64] =
"";
86 static unsigned int partition_number = -1;
113 eprintk(
"Error %ld transferring data\n", rc);
115 crq.cooked.valid = 0x80;
116 crq.cooked.format =
format;
117 crq.cooked.reserved = 0x00;
118 crq.cooked.timeout = 0x00;
119 crq.cooked.IU_length =
length;
120 crq.cooked.IU_data_ptr = vio_iu(iue)->
srp.
rsp.tag;
123 crq.cooked.status = 0x99;
125 crq.cooked.status = 0x00;
130 eprintk(
"%ld sending response\n", rc1);
137 #define SRP_RSP_SENSE_DATA_LEN 18
151 iu->
srp.
rsp.req_lim_delta = 1;
157 iu->
srp.
rsp.data_in_res_cnt = 0;
158 iu->
srp.
rsp.data_out_res_cnt = 0;
162 iu->
srp.
rsp.resp_data_len = 0;
177 sense[0] = (0x1 << 7 | 0x70);
193 static void handle_cmd_queue(
struct srp_target *target)
207 spin_unlock_irqrestore(&target->
lock, flags);
208 cmd = iue->
sbuf->buf;
210 (
unsigned long)rport, 0);
212 eprintk(
"cannot queue cmd %p %d\n", cmd, err);
219 spin_unlock_irqrestore(&target->
lock, flags);
228 struct vio_port *vport = target_to_port(target);
231 unsigned int done = 0;
237 for (i = 0; i < nmd &&
rest; i++) {
238 unsigned int mdone, mlen;
240 mlen =
min(rest, md[i].len);
241 for (mdone = 0; mlen;) {
257 if (err != H_SUCCESS) {
258 eprintk(
"rdma error %d %d %ld\n", dir, slen, err);
273 eprintk(
"out of sg %p %d %d\n",
285 static int ibmvstgt_cmd_done(
struct scsi_cmnd *sc,
293 dprintk(
"%p %p %x %u\n", iue, target, vio_iu(iue)->
srp.
cmd.cdb[0],
296 if (scsi_sg_count(sc))
301 spin_unlock_irqrestore(&target->
lock, flags);
304 eprintk(
"operation failed %p %d %x\n",
305 iue, sc->
result, vio_iu(iue)->srp.cmd.cdb[0]);
319 struct vio_port *vport = target_to_port(target);
328 eprintk(
"bad dma_alloc_coherent %p\n", target);
334 vport->
liobn, data_token);
335 if (err == H_SUCCESS) {
336 dprintk(
"Client connect: %s (%d)\n",
340 memset(info, 0,
sizeof(*info));
352 vport->
riobn, remote_buffer);
356 if (err != H_SUCCESS) {
357 eprintk(
"Error sending adapter info %d\n", err);
364 static void process_login(
struct iu_entry *iue)
370 struct srp_target *target = host_to_srp_target(shost);
371 struct vio_port *vport = target_to_port(target);
374 memset(&ids, 0,
sizeof(ids));
395 static inline void queue_cmd(
struct iu_entry *iue)
402 spin_unlock_irqrestore(&target->
lock, flags);
405 static int process_tsk_mgmt(
struct iu_entry *iue)
433 (
unsigned long)iue->
target->shost,
444 static int process_mad_iu(
struct iu_entry *iue)
452 eprintk(
"%s\n",
"Unsupported EMPTY MAD IU");
455 eprintk(
"%s\n",
"Unsupported ERROR LOG MAD IU");
477 static int process_srp_iu(
struct iu_entry *iue)
488 done = process_tsk_mgmt(iue);
502 eprintk(
"Unsupported type %u\n", opcode);
505 eprintk(
"Unknown type %u\n", opcode);
513 struct vio_port *vport = target_to_port(target);
520 eprintk(
"Error getting IU from pool, %p\n", target);
529 if (err != H_SUCCESS) {
530 eprintk(
"%ld transferring data error %p\n", err, iue);
535 done = process_mad_iu(iue);
537 done = process_srp_iu(iue);
546 struct vio_port *vport = target_to_port(target);
548 vio_disable_interrupts(vport->
dma_dev);
557 struct vio_port *vport = target_to_port(target);
577 if (err == H_RESOURCE) {
580 }
while (err == H_BUSY || H_IS_LONG_BUSY(err));
586 if (err != H_SUCCESS && err != 2) {
587 eprintk(
"Error 0x%x opening virtual adapter\n", err);
596 vio_enable_interrupts(vport->
dma_dev);
608 }
while (err == H_BUSY || H_IS_LONG_BUSY(err));
620 static void crq_queue_destroy(
struct srp_target *target)
622 struct vio_port *vport = target_to_port(target);
629 }
while (err == H_BUSY || H_IS_LONG_BUSY(err));
639 struct vio_port *vport = target_to_port(target);
642 switch (crq->
valid) {
648 0xC002000000000000, 0);
664 process_iu(crq, target);
687 crq = &queue->
msgs[queue->
cur];
688 if (crq->
valid & 0x80) {
689 if (++queue->
cur == queue->
size)
693 spin_unlock_irqrestore(&queue->
lock, flags);
707 process_crq(crq, target);
711 vio_enable_interrupts(vport->
dma_dev);
715 vio_disable_interrupts(vport->
dma_dev);
716 process_crq(crq, target);
722 handle_cmd_queue(target);
726 static int ibmvstgt_eh_abort_handler(
struct scsi_cmnd *sc)
732 dprintk(
"%p %p %x\n", iue, target, vio_iu(iue)->
srp.
cmd.cdb[0]);
736 spin_unlock_irqrestore(&target->
lock, flags);
743 static int ibmvstgt_tsk_mgmt_response(
struct Scsi_Host *shost,
750 eprintk(
"%p %d\n", iue, result);
764 send_rsp(iue,
NULL, status, asc);
770 static int ibmvstgt_it_nexus_response(
struct Scsi_Host *shost,
u64 itn_id,
773 struct srp_target *target = host_to_srp_target(shost);
774 struct vio_port *vport = target_to_port(target);
777 eprintk(
"%p %d\n", shost, result);
800 struct srp_target *target = host_to_srp_target(shost);
801 struct vio_port *vport = target_to_port(target);
811 &dev_attr_partition_number,
812 &dev_attr_unit_address,
823 .transfer_response = ibmvstgt_cmd_done,
824 .eh_abort_handler = ibmvstgt_eh_abort_handler,
825 .shost_attrs = ibmvstgt_attrs,
835 unsigned int *
dma, dma_size;
844 shost->
transportt = ibmvstgt_transport_template;
846 target = host_to_srp_target(shost);
849 target->
ldata = vport;
858 if (!dma || dma_size != 40) {
859 eprintk(
"Couldn't get window property %d\n", dma_size);
861 goto free_srp_target;
863 vport->
liobn = dma[0];
864 vport->
riobn = dma[5];
868 err = scsi_add_host(shost, target->
dev);
870 goto free_srp_target;
876 err = crq_queue_create(&vport->
crq_queue, target);
894 static int ibmvstgt_remove(
struct vio_dev *dev)
900 crq_queue_destroy(target);
911 {
"v-scsi-host",
"IBM,v-scsi-host"},
918 .id_table = ibmvstgt_device_table,
919 .probe = ibmvstgt_probe,
920 .remove = ibmvstgt_remove,
924 static int get_system_info(
void)
928 const unsigned int *
num;
937 snprintf(system_id,
sizeof(system_id),
"%s-%s", model,
id);
945 partition_number = *num;
952 .tsk_mgmt_response = ibmvstgt_tsk_mgmt_response,
953 .it_nexus_response = ibmvstgt_it_nexus_response,
956 static int __init ibmvstgt_init(
void)
960 printk(
"IBM eServer i/pSeries Virtual SCSI Target Driver\n");
962 ibmvstgt_transport_template =
964 if (!ibmvstgt_transport_template)
969 goto release_transport;
971 err = get_system_info();
987 static void __exit ibmvstgt_exit(
void)
989 printk(
"Unregister IBM virtual SCSI driver\n");