1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69#include <linux/module.h>
70#include <linux/moduleparam.h>
71#include <linux/dma-mapping.h>
72#include <linux/delay.h>
73#include <linux/of.h>
74#include <asm/firmware.h>
75#include <asm/vio.h>
76#include <scsi/scsi.h>
77#include <scsi/scsi_cmnd.h>
78#include <scsi/scsi_host.h>
79#include <scsi/scsi_device.h>
80#include <scsi/scsi_transport_srp.h>
81#include "ibmvscsi.h"
82
83
84
85
86
87
88static int max_id = 64;
89static int max_channel = 3;
90static int init_timeout = 300;
91static int login_timeout = 60;
92static int info_timeout = 30;
93static int abort_timeout = 60;
94static int reset_timeout = 60;
95static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
96static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
97static int fast_fail = 1;
98static int client_reserve = 1;
99
100static struct scsi_transport_template *ibmvscsi_transport_template;
101
102#define IBMVSCSI_VERSION "1.5.8"
103
104static struct ibmvscsi_ops *ibmvscsi_ops;
105
106MODULE_DESCRIPTION("IBM Virtual SCSI");
107MODULE_AUTHOR("Dave Boutcher");
108MODULE_LICENSE("GPL");
109MODULE_VERSION(IBMVSCSI_VERSION);
110
111module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
112MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
113module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
114MODULE_PARM_DESC(max_channel, "Largest channel value");
115module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
116MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
117module_param_named(max_requests, max_requests, int, S_IRUGO);
118MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
119module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
120MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
121module_param_named(client_reserve, client_reserve, int, S_IRUGO );
122MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
123
124
125
126
127
128
129
130
131
132
133
134
135static int initialize_event_pool(struct event_pool *pool,
136 int size, struct ibmvscsi_host_data *hostdata)
137{
138 int i;
139
140 pool->size = size;
141 pool->next = 0;
142 pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
143 if (!pool->events)
144 return -ENOMEM;
145
146 pool->iu_storage =
147 dma_alloc_coherent(hostdata->dev,
148 pool->size * sizeof(*pool->iu_storage),
149 &pool->iu_token, 0);
150 if (!pool->iu_storage) {
151 kfree(pool->events);
152 return -ENOMEM;
153 }
154
155 for (i = 0; i < pool->size; ++i) {
156 struct srp_event_struct *evt = &pool->events[i];
157 memset(&evt->crq, 0x00, sizeof(evt->crq));
158 atomic_set(&evt->free, 1);
159 evt->crq.valid = 0x80;
160 evt->crq.IU_length = sizeof(*evt->xfer_iu);
161 evt->crq.IU_data_ptr = pool->iu_token +
162 sizeof(*evt->xfer_iu) * i;
163 evt->xfer_iu = pool->iu_storage + i;
164 evt->hostdata = hostdata;
165 evt->ext_list = NULL;
166 evt->ext_list_token = 0;
167 }
168
169 return 0;
170}
171
172
173
174
175
176
177
178
179static void release_event_pool(struct event_pool *pool,
180 struct ibmvscsi_host_data *hostdata)
181{
182 int i, in_use = 0;
183 for (i = 0; i < pool->size; ++i) {
184 if (atomic_read(&pool->events[i].free) != 1)
185 ++in_use;
186 if (pool->events[i].ext_list) {
187 dma_free_coherent(hostdata->dev,
188 SG_ALL * sizeof(struct srp_direct_buf),
189 pool->events[i].ext_list,
190 pool->events[i].ext_list_token);
191 }
192 }
193 if (in_use)
194 dev_warn(hostdata->dev, "releasing event pool with %d "
195 "events still in use?\n", in_use);
196 kfree(pool->events);
197 dma_free_coherent(hostdata->dev,
198 pool->size * sizeof(*pool->iu_storage),
199 pool->iu_storage, pool->iu_token);
200}
201
202
203
204
205
206
207
208
209static int valid_event_struct(struct event_pool *pool,
210 struct srp_event_struct *evt)
211{
212 int index = evt - pool->events;
213 if (index < 0 || index >= pool->size)
214 return 0;
215 if (evt != pool->events + index)
216 return 0;
217 return 1;
218}
219
220
221
222
223
224
225
226static void free_event_struct(struct event_pool *pool,
227 struct srp_event_struct *evt)
228{
229 if (!valid_event_struct(pool, evt)) {
230 dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
231 "(not in pool %p)\n", evt, pool->events);
232 return;
233 }
234 if (atomic_inc_return(&evt->free) != 1) {
235 dev_err(evt->hostdata->dev, "Freeing event_struct %p "
236 "which is not in use!\n", evt);
237 return;
238 }
239}
240
241
242
243
244
245
246
247
248
249static struct srp_event_struct *get_event_struct(struct event_pool *pool)
250{
251 int i;
252 int poolsize = pool->size;
253 int offset = pool->next;
254
255 for (i = 0; i < poolsize; i++) {
256 offset = (offset + 1) % poolsize;
257 if (!atomic_dec_if_positive(&pool->events[offset].free)) {
258 pool->next = offset;
259 return &pool->events[offset];
260 }
261 }
262
263 printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
264 return NULL;
265}
266
267
268
269
270
271
272
273
274
275static void init_event_struct(struct srp_event_struct *evt_struct,
276 void (*done) (struct srp_event_struct *),
277 u8 format,
278 int timeout)
279{
280 evt_struct->cmnd = NULL;
281 evt_struct->cmnd_done = NULL;
282 evt_struct->sync_srp = NULL;
283 evt_struct->crq.format = format;
284 evt_struct->crq.timeout = timeout;
285 evt_struct->done = done;
286}
287
288
289
290
291
292
293
294
295
296
297static void set_srp_direction(struct scsi_cmnd *cmd,
298 struct srp_cmd *srp_cmd,
299 int numbuf)
300{
301 u8 fmt;
302
303 if (numbuf == 0)
304 return;
305
306 if (numbuf == 1)
307 fmt = SRP_DATA_DESC_DIRECT;
308 else {
309 fmt = SRP_DATA_DESC_INDIRECT;
310 numbuf = min(numbuf, MAX_INDIRECT_BUFS);
311
312 if (cmd->sc_data_direction == DMA_TO_DEVICE)
313 srp_cmd->data_out_desc_cnt = numbuf;
314 else
315 srp_cmd->data_in_desc_cnt = numbuf;
316 }
317
318 if (cmd->sc_data_direction == DMA_TO_DEVICE)
319 srp_cmd->buf_fmt = fmt << 4;
320 else
321 srp_cmd->buf_fmt = fmt;
322}
323
324static void unmap_sg_list(int num_entries,
325 struct device *dev,
326 struct srp_direct_buf *md)
327{
328 int i;
329
330 for (i = 0; i < num_entries; ++i)
331 dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
332}
333
334
335
336
337
338
339
340static void unmap_cmd_data(struct srp_cmd *cmd,
341 struct srp_event_struct *evt_struct,
342 struct device *dev)
343{
344 u8 out_fmt, in_fmt;
345
346 out_fmt = cmd->buf_fmt >> 4;
347 in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
348
349 if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
350 return;
351 else if (out_fmt == SRP_DATA_DESC_DIRECT ||
352 in_fmt == SRP_DATA_DESC_DIRECT) {
353 struct srp_direct_buf *data =
354 (struct srp_direct_buf *) cmd->add_data;
355 dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
356 } else {
357 struct srp_indirect_buf *indirect =
358 (struct srp_indirect_buf *) cmd->add_data;
359 int num_mapped = indirect->table_desc.len /
360 sizeof(struct srp_direct_buf);
361
362 if (num_mapped <= MAX_INDIRECT_BUFS) {
363 unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
364 return;
365 }
366
367 unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
368 }
369}
370
371static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
372 struct srp_direct_buf *md)
373{
374 int i;
375 struct scatterlist *sg;
376 u64 total_length = 0;
377
378 scsi_for_each_sg(cmd, sg, nseg, i) {
379 struct srp_direct_buf *descr = md + i;
380 descr->va = sg_dma_address(sg);
381 descr->len = sg_dma_len(sg);
382 descr->key = 0;
383 total_length += sg_dma_len(sg);
384 }
385 return total_length;
386}
387
388
389
390
391
392
393
394
395
396
397static int map_sg_data(struct scsi_cmnd *cmd,
398 struct srp_event_struct *evt_struct,
399 struct srp_cmd *srp_cmd, struct device *dev)
400{
401
402 int sg_mapped;
403 u64 total_length = 0;
404 struct srp_direct_buf *data =
405 (struct srp_direct_buf *) srp_cmd->add_data;
406 struct srp_indirect_buf *indirect =
407 (struct srp_indirect_buf *) data;
408
409 sg_mapped = scsi_dma_map(cmd);
410 if (!sg_mapped)
411 return 1;
412 else if (sg_mapped < 0)
413 return 0;
414
415 set_srp_direction(cmd, srp_cmd, sg_mapped);
416
417
418 if (sg_mapped == 1) {
419 map_sg_list(cmd, sg_mapped, data);
420 return 1;
421 }
422
423 indirect->table_desc.va = 0;
424 indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
425 indirect->table_desc.key = 0;
426
427 if (sg_mapped <= MAX_INDIRECT_BUFS) {
428 total_length = map_sg_list(cmd, sg_mapped,
429 &indirect->desc_list[0]);
430 indirect->len = total_length;
431 return 1;
432 }
433
434
435 if (!evt_struct->ext_list) {
436 evt_struct->ext_list = (struct srp_direct_buf *)
437 dma_alloc_coherent(dev,
438 SG_ALL * sizeof(struct srp_direct_buf),
439 &evt_struct->ext_list_token, 0);
440 if (!evt_struct->ext_list) {
441 if (!firmware_has_feature(FW_FEATURE_CMO))
442 sdev_printk(KERN_ERR, cmd->device,
443 "Can't allocate memory "
444 "for indirect table\n");
445 scsi_dma_unmap(cmd);
446 return 0;
447 }
448 }
449
450 total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
451
452 indirect->len = total_length;
453 indirect->table_desc.va = evt_struct->ext_list_token;
454 indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
455 memcpy(indirect->desc_list, evt_struct->ext_list,
456 MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
457 return 1;
458}
459
460
461
462
463
464
465
466
467
468
469static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
470 struct srp_event_struct *evt_struct,
471 struct srp_cmd *srp_cmd, struct device *dev)
472{
473 switch (cmd->sc_data_direction) {
474 case DMA_FROM_DEVICE:
475 case DMA_TO_DEVICE:
476 break;
477 case DMA_NONE:
478 return 1;
479 case DMA_BIDIRECTIONAL:
480 sdev_printk(KERN_ERR, cmd->device,
481 "Can't map DMA_BIDIRECTIONAL to read/write\n");
482 return 0;
483 default:
484 sdev_printk(KERN_ERR, cmd->device,
485 "Unknown data direction 0x%02x; can't map!\n",
486 cmd->sc_data_direction);
487 return 0;
488 }
489
490 return map_sg_data(cmd, evt_struct, srp_cmd, dev);
491}
492
493
494
495
496
497static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
498{
499 struct srp_event_struct *tmp_evt, *pos;
500 unsigned long flags;
501
502 spin_lock_irqsave(hostdata->host->host_lock, flags);
503 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
504 list_del(&tmp_evt->list);
505 del_timer(&tmp_evt->timer);
506 if (tmp_evt->cmnd) {
507 tmp_evt->cmnd->result = (error_code << 16);
508 unmap_cmd_data(&tmp_evt->iu.srp.cmd,
509 tmp_evt,
510 tmp_evt->hostdata->dev);
511 if (tmp_evt->cmnd_done)
512 tmp_evt->cmnd_done(tmp_evt->cmnd);
513 } else if (tmp_evt->done)
514 tmp_evt->done(tmp_evt);
515 free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
516 }
517 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
518}
519
520
521
522
523
524static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
525{
526 scsi_block_requests(hostdata->host);
527 atomic_set(&hostdata->request_limit, 0);
528
529 purge_requests(hostdata, DID_ERROR);
530 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
531 (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
532 (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
533 atomic_set(&hostdata->request_limit, -1);
534 dev_err(hostdata->dev, "error after reset\n");
535 }
536
537 scsi_unblock_requests(hostdata->host);
538}
539
540
541
542
543
544
545
546static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
547{
548 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
549
550 dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
551 evt_struct->iu.srp.cmd.opcode);
552
553 ibmvscsi_reset_host(hostdata);
554}
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
570 struct ibmvscsi_host_data *hostdata,
571 unsigned long timeout)
572{
573 u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
574 int request_status = 0;
575 int rc;
576
577
578
579
580
581
582
583 if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
584 request_status =
585 atomic_dec_if_positive(&hostdata->request_limit);
586
587
588
589 if (request_status < -1)
590 goto send_error;
591
592
593
594
595
596 else if (request_status == -1 &&
597 evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
598 goto send_busy;
599
600
601
602
603 else if (request_status < 2 &&
604 evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
605
606
607
608
609
610
611
612 int server_limit = request_status;
613 struct srp_event_struct *tmp_evt;
614
615 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
616 server_limit++;
617 }
618
619 if (server_limit > 2)
620 goto send_busy;
621 }
622 }
623
624
625 *evt_struct->xfer_iu = evt_struct->iu;
626 evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
627
628
629
630
631
632 list_add_tail(&evt_struct->list, &hostdata->sent);
633
634 init_timer(&evt_struct->timer);
635 if (timeout) {
636 evt_struct->timer.data = (unsigned long) evt_struct;
637 evt_struct->timer.expires = jiffies + (timeout * HZ);
638 evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
639 add_timer(&evt_struct->timer);
640 }
641
642 if ((rc =
643 ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
644 list_del(&evt_struct->list);
645 del_timer(&evt_struct->timer);
646
647
648
649
650
651
652 if (rc == H_CLOSED) {
653 dev_warn(hostdata->dev, "send warning. "
654 "Receive queue closed, will retry.\n");
655 goto send_busy;
656 }
657 dev_err(hostdata->dev, "send error %d\n", rc);
658 atomic_inc(&hostdata->request_limit);
659 goto send_error;
660 }
661
662 return 0;
663
664 send_busy:
665 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
666
667 free_event_struct(&hostdata->pool, evt_struct);
668 if (request_status != -1)
669 atomic_inc(&hostdata->request_limit);
670 return SCSI_MLQUEUE_HOST_BUSY;
671
672 send_error:
673 unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
674
675 if (evt_struct->cmnd != NULL) {
676 evt_struct->cmnd->result = DID_ERROR << 16;
677 evt_struct->cmnd_done(evt_struct->cmnd);
678 } else if (evt_struct->done)
679 evt_struct->done(evt_struct);
680
681 free_event_struct(&hostdata->pool, evt_struct);
682 return 0;
683}
684
685
686
687
688
689
690
691
692static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
693{
694 struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
695 struct scsi_cmnd *cmnd = evt_struct->cmnd;
696
697 if (unlikely(rsp->opcode != SRP_RSP)) {
698 if (printk_ratelimit())
699 dev_warn(evt_struct->hostdata->dev,
700 "bad SRP RSP type %d\n", rsp->opcode);
701 }
702
703 if (cmnd) {
704 cmnd->result |= rsp->status;
705 if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
706 memcpy(cmnd->sense_buffer,
707 rsp->data,
708 rsp->sense_data_len);
709 unmap_cmd_data(&evt_struct->iu.srp.cmd,
710 evt_struct,
711 evt_struct->hostdata->dev);
712
713 if (rsp->flags & SRP_RSP_FLAG_DOOVER)
714 scsi_set_resid(cmnd, rsp->data_out_res_cnt);
715 else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
716 scsi_set_resid(cmnd, rsp->data_in_res_cnt);
717 }
718
719 if (evt_struct->cmnd_done)
720 evt_struct->cmnd_done(cmnd);
721}
722
723
724
725
726
727
728static inline u16 lun_from_dev(struct scsi_device *dev)
729{
730 return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
731}
732
733
734
735
736
737
738static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
739 void (*done) (struct scsi_cmnd *))
740{
741 struct srp_cmd *srp_cmd;
742 struct srp_event_struct *evt_struct;
743 struct srp_indirect_buf *indirect;
744 struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
745 u16 lun = lun_from_dev(cmnd->device);
746 u8 out_fmt, in_fmt;
747
748 cmnd->result = (DID_OK << 16);
749 evt_struct = get_event_struct(&hostdata->pool);
750 if (!evt_struct)
751 return SCSI_MLQUEUE_HOST_BUSY;
752
753
754 srp_cmd = &evt_struct->iu.srp.cmd;
755 memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
756 srp_cmd->opcode = SRP_CMD;
757 memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
758 srp_cmd->lun = ((u64) lun) << 48;
759
760 if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
761 if (!firmware_has_feature(FW_FEATURE_CMO))
762 sdev_printk(KERN_ERR, cmnd->device,
763 "couldn't convert cmd to srp_cmd\n");
764 free_event_struct(&hostdata->pool, evt_struct);
765 return SCSI_MLQUEUE_HOST_BUSY;
766 }
767
768 init_event_struct(evt_struct,
769 handle_cmd_rsp,
770 VIOSRP_SRP_FORMAT,
771 cmnd->request->timeout/HZ);
772
773 evt_struct->cmnd = cmnd;
774 evt_struct->cmnd_done = done;
775
776
777 indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
778 out_fmt = srp_cmd->buf_fmt >> 4;
779 in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
780 if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
781 out_fmt == SRP_DATA_DESC_INDIRECT) &&
782 indirect->table_desc.va == 0) {
783 indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
784 offsetof(struct srp_cmd, add_data) +
785 offsetof(struct srp_indirect_buf, desc_list);
786 }
787
788 return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
789}
790
791
792
793
794
795
796
797
798
799
800
801
802static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
803{
804
805 hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
806 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
807
808 if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
809 dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
810 return 1;
811 }
812
813 hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
814 &hostdata->madapter_info,
815 sizeof(hostdata->madapter_info),
816 DMA_BIDIRECTIONAL);
817 if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
818 dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
819 dma_unmap_single(hostdata->dev, hostdata->caps_addr,
820 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
821 return 1;
822 }
823
824 return 0;
825}
826
827
828
829
830
831
832
833static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
834{
835 dma_unmap_single(hostdata->dev, hostdata->caps_addr,
836 sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
837
838 dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
839 sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
840}
841
842
843
844
845
846
847
848
849static void login_rsp(struct srp_event_struct *evt_struct)
850{
851 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
852 switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
853 case SRP_LOGIN_RSP:
854 break;
855 case SRP_LOGIN_REJ:
856 dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
857 evt_struct->xfer_iu->srp.login_rej.reason);
858
859 atomic_set(&hostdata->request_limit, -1);
860 return;
861 default:
862 dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
863 evt_struct->xfer_iu->srp.login_rsp.opcode);
864
865 atomic_set(&hostdata->request_limit, -1);
866 return;
867 }
868
869 dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
870 hostdata->client_migrated = 0;
871
872
873
874
875
876 atomic_set(&hostdata->request_limit,
877 evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
878
879
880 scsi_unblock_requests(hostdata->host);
881}
882
883
884
885
886
887
888
889static int send_srp_login(struct ibmvscsi_host_data *hostdata)
890{
891 int rc;
892 unsigned long flags;
893 struct srp_login_req *login;
894 struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
895
896 BUG_ON(!evt_struct);
897 init_event_struct(evt_struct, login_rsp,
898 VIOSRP_SRP_FORMAT, login_timeout);
899
900 login = &evt_struct->iu.srp.login_req;
901 memset(login, 0, sizeof(*login));
902 login->opcode = SRP_LOGIN_REQ;
903 login->req_it_iu_len = sizeof(union srp_iu);
904 login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
905
906 spin_lock_irqsave(hostdata->host->host_lock, flags);
907
908
909
910
911 atomic_set(&hostdata->request_limit, 0);
912
913 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
914 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
915 dev_info(hostdata->dev, "sent SRP login\n");
916 return rc;
917};
918
919
920
921
922
923
924
925static void capabilities_rsp(struct srp_event_struct *evt_struct)
926{
927 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
928
929 if (evt_struct->xfer_iu->mad.capabilities.common.status) {
930 dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
931 evt_struct->xfer_iu->mad.capabilities.common.status);
932 } else {
933 if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
934 dev_info(hostdata->dev, "Partition migration not supported\n");
935
936 if (client_reserve) {
937 if (hostdata->caps.reserve.common.server_support ==
938 SERVER_SUPPORTS_CAP)
939 dev_info(hostdata->dev, "Client reserve enabled\n");
940 else
941 dev_info(hostdata->dev, "Client reserve not supported\n");
942 }
943 }
944
945 send_srp_login(hostdata);
946}
947
948
949
950
951
952
953static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
954{
955 struct viosrp_capabilities *req;
956 struct srp_event_struct *evt_struct;
957 unsigned long flags;
958 struct device_node *of_node = hostdata->dev->archdata.of_node;
959 const char *location;
960
961 evt_struct = get_event_struct(&hostdata->pool);
962 BUG_ON(!evt_struct);
963
964 init_event_struct(evt_struct, capabilities_rsp,
965 VIOSRP_MAD_FORMAT, info_timeout);
966
967 req = &evt_struct->iu.mad.capabilities;
968 memset(req, 0, sizeof(*req));
969
970 hostdata->caps.flags = CAP_LIST_SUPPORTED;
971 if (hostdata->client_migrated)
972 hostdata->caps.flags |= CLIENT_MIGRATED;
973
974 strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
975 sizeof(hostdata->caps.name));
976 hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
977
978 location = of_get_property(of_node, "ibm,loc-code", NULL);
979 location = location ? location : dev_name(hostdata->dev);
980 strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
981 hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
982
983 req->common.type = VIOSRP_CAPABILITIES_TYPE;
984 req->buffer = hostdata->caps_addr;
985
986 hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
987 hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
988 hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
989 hostdata->caps.migration.ecl = 1;
990
991 if (client_reserve) {
992 hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
993 hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
994 hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
995 hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
996 req->common.length = sizeof(hostdata->caps);
997 } else
998 req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);
999
1000 spin_lock_irqsave(hostdata->host->host_lock, flags);
1001 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1002 dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
1003 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1004};
1005
1006
1007
1008
1009
1010
1011
1012
1013static void fast_fail_rsp(struct srp_event_struct *evt_struct)
1014{
1015 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1016 u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
1017
1018 if (status == VIOSRP_MAD_NOT_SUPPORTED)
1019 dev_err(hostdata->dev, "fast_fail not supported in server\n");
1020 else if (status == VIOSRP_MAD_FAILED)
1021 dev_err(hostdata->dev, "fast_fail request failed\n");
1022 else if (status != VIOSRP_MAD_SUCCESS)
1023 dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
1024
1025 send_mad_capabilities(hostdata);
1026}
1027
1028
1029
1030
1031
1032
1033
1034static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
1035{
1036 int rc;
1037 unsigned long flags;
1038 struct viosrp_fast_fail *fast_fail_mad;
1039 struct srp_event_struct *evt_struct;
1040
1041 if (!fast_fail) {
1042 send_mad_capabilities(hostdata);
1043 return 0;
1044 }
1045
1046 evt_struct = get_event_struct(&hostdata->pool);
1047 BUG_ON(!evt_struct);
1048
1049 init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
1050
1051 fast_fail_mad = &evt_struct->iu.mad.fast_fail;
1052 memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
1053 fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
1054 fast_fail_mad->common.length = sizeof(*fast_fail_mad);
1055
1056 spin_lock_irqsave(hostdata->host->host_lock, flags);
1057 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1058 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1059 return rc;
1060}
1061
1062
1063
1064
1065
1066
1067
1068
1069static void adapter_info_rsp(struct srp_event_struct *evt_struct)
1070{
1071 struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
1072
1073 if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
1074 dev_err(hostdata->dev, "error %d getting adapter info\n",
1075 evt_struct->xfer_iu->mad.adapter_info.common.status);
1076 } else {
1077 dev_info(hostdata->dev, "host srp version: %s, "
1078 "host partition %s (%d), OS %d, max io %u\n",
1079 hostdata->madapter_info.srp_version,
1080 hostdata->madapter_info.partition_name,
1081 hostdata->madapter_info.partition_number,
1082 hostdata->madapter_info.os_type,
1083 hostdata->madapter_info.port_max_txu[0]);
1084
1085 if (hostdata->madapter_info.port_max_txu[0])
1086 hostdata->host->max_sectors =
1087 hostdata->madapter_info.port_max_txu[0] >> 9;
1088
1089 if (hostdata->madapter_info.os_type == 3 &&
1090 strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
1091 dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
1092 hostdata->madapter_info.srp_version);
1093 dev_err(hostdata->dev, "limiting scatterlists to %d\n",
1094 MAX_INDIRECT_BUFS);
1095 hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
1096 }
1097
1098 if (hostdata->madapter_info.os_type == 3) {
1099 enable_fast_fail(hostdata);
1100 return;
1101 }
1102 }
1103
1104 send_srp_login(hostdata);
1105}
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
1117{
1118 struct viosrp_adapter_info *req;
1119 struct srp_event_struct *evt_struct;
1120 unsigned long flags;
1121
1122 evt_struct = get_event_struct(&hostdata->pool);
1123 BUG_ON(!evt_struct);
1124
1125 init_event_struct(evt_struct,
1126 adapter_info_rsp,
1127 VIOSRP_MAD_FORMAT,
1128 info_timeout);
1129
1130 req = &evt_struct->iu.mad.adapter_info;
1131 memset(req, 0x00, sizeof(*req));
1132
1133 req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
1134 req->common.length = sizeof(hostdata->madapter_info);
1135 req->buffer = hostdata->adapter_info_addr;
1136
1137 spin_lock_irqsave(hostdata->host->host_lock, flags);
1138 if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
1139 dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
1140 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1141};
1142
1143
1144
1145
1146
1147static void init_adapter(struct ibmvscsi_host_data *hostdata)
1148{
1149 send_mad_adapter_info(hostdata);
1150}
1151
1152
1153
1154
1155
1156
1157
1158static void sync_completion(struct srp_event_struct *evt_struct)
1159{
1160
1161 if (evt_struct->sync_srp)
1162 *evt_struct->sync_srp = *evt_struct->xfer_iu;
1163
1164 complete(&evt_struct->comp);
1165}
1166
1167
1168
1169
1170
1171static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
1172{
1173 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1174 struct srp_tsk_mgmt *tsk_mgmt;
1175 struct srp_event_struct *evt;
1176 struct srp_event_struct *tmp_evt, *found_evt;
1177 union viosrp_iu srp_rsp;
1178 int rsp_rc;
1179 unsigned long flags;
1180 u16 lun = lun_from_dev(cmd->device);
1181 unsigned long wait_switch = 0;
1182
1183
1184
1185
1186 spin_lock_irqsave(hostdata->host->host_lock, flags);
1187 wait_switch = jiffies + (init_timeout * HZ);
1188 do {
1189 found_evt = NULL;
1190 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1191 if (tmp_evt->cmnd == cmd) {
1192 found_evt = tmp_evt;
1193 break;
1194 }
1195 }
1196
1197 if (!found_evt) {
1198 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1199 return SUCCESS;
1200 }
1201
1202 evt = get_event_struct(&hostdata->pool);
1203 if (evt == NULL) {
1204 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1205 sdev_printk(KERN_ERR, cmd->device,
1206 "failed to allocate abort event\n");
1207 return FAILED;
1208 }
1209
1210 init_event_struct(evt,
1211 sync_completion,
1212 VIOSRP_SRP_FORMAT,
1213 abort_timeout);
1214
1215 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1216
1217
1218 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1219 tsk_mgmt->opcode = SRP_TSK_MGMT;
1220 tsk_mgmt->lun = ((u64) lun) << 48;
1221 tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
1222 tsk_mgmt->task_tag = (u64) found_evt;
1223
1224 evt->sync_srp = &srp_rsp;
1225
1226 init_completion(&evt->comp);
1227 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
1228
1229 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1230 break;
1231
1232 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1233 msleep(10);
1234 spin_lock_irqsave(hostdata->host->host_lock, flags);
1235 } while (time_before(jiffies, wait_switch));
1236
1237 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1238
1239 if (rsp_rc != 0) {
1240 sdev_printk(KERN_ERR, cmd->device,
1241 "failed to send abort() event. rc=%d\n", rsp_rc);
1242 return FAILED;
1243 }
1244
1245 sdev_printk(KERN_INFO, cmd->device,
1246 "aborting command. lun 0x%llx, tag 0x%llx\n",
1247 (((u64) lun) << 48), (u64) found_evt);
1248
1249 wait_for_completion(&evt->comp);
1250
1251
1252 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1253 if (printk_ratelimit())
1254 sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
1255 srp_rsp.srp.rsp.opcode);
1256 return FAILED;
1257 }
1258
1259 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1260 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1261 else
1262 rsp_rc = srp_rsp.srp.rsp.status;
1263
1264 if (rsp_rc) {
1265 if (printk_ratelimit())
1266 sdev_printk(KERN_WARNING, cmd->device,
1267 "abort code %d for task tag 0x%llx\n",
1268 rsp_rc, tsk_mgmt->task_tag);
1269 return FAILED;
1270 }
1271
1272
1273
1274
1275
1276 spin_lock_irqsave(hostdata->host->host_lock, flags);
1277 found_evt = NULL;
1278 list_for_each_entry(tmp_evt, &hostdata->sent, list) {
1279 if (tmp_evt->cmnd == cmd) {
1280 found_evt = tmp_evt;
1281 break;
1282 }
1283 }
1284
1285 if (found_evt == NULL) {
1286 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1287 sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
1288 tsk_mgmt->task_tag);
1289 return SUCCESS;
1290 }
1291
1292 sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
1293 tsk_mgmt->task_tag);
1294
1295 cmd->result = (DID_ABORT << 16);
1296 list_del(&found_evt->list);
1297 unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
1298 found_evt->hostdata->dev);
1299 free_event_struct(&found_evt->hostdata->pool, found_evt);
1300 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1301 atomic_inc(&hostdata->request_limit);
1302 return SUCCESS;
1303}
1304
1305
1306
1307
1308
1309
1310static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
1311{
1312 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1313 struct srp_tsk_mgmt *tsk_mgmt;
1314 struct srp_event_struct *evt;
1315 struct srp_event_struct *tmp_evt, *pos;
1316 union viosrp_iu srp_rsp;
1317 int rsp_rc;
1318 unsigned long flags;
1319 u16 lun = lun_from_dev(cmd->device);
1320 unsigned long wait_switch = 0;
1321
1322 spin_lock_irqsave(hostdata->host->host_lock, flags);
1323 wait_switch = jiffies + (init_timeout * HZ);
1324 do {
1325 evt = get_event_struct(&hostdata->pool);
1326 if (evt == NULL) {
1327 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1328 sdev_printk(KERN_ERR, cmd->device,
1329 "failed to allocate reset event\n");
1330 return FAILED;
1331 }
1332
1333 init_event_struct(evt,
1334 sync_completion,
1335 VIOSRP_SRP_FORMAT,
1336 reset_timeout);
1337
1338 tsk_mgmt = &evt->iu.srp.tsk_mgmt;
1339
1340
1341 memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
1342 tsk_mgmt->opcode = SRP_TSK_MGMT;
1343 tsk_mgmt->lun = ((u64) lun) << 48;
1344 tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
1345
1346 evt->sync_srp = &srp_rsp;
1347
1348 init_completion(&evt->comp);
1349 rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
1350
1351 if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
1352 break;
1353
1354 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1355 msleep(10);
1356 spin_lock_irqsave(hostdata->host->host_lock, flags);
1357 } while (time_before(jiffies, wait_switch));
1358
1359 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1360
1361 if (rsp_rc != 0) {
1362 sdev_printk(KERN_ERR, cmd->device,
1363 "failed to send reset event. rc=%d\n", rsp_rc);
1364 return FAILED;
1365 }
1366
1367 sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
1368 (((u64) lun) << 48));
1369
1370 wait_for_completion(&evt->comp);
1371
1372
1373 if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
1374 if (printk_ratelimit())
1375 sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
1376 srp_rsp.srp.rsp.opcode);
1377 return FAILED;
1378 }
1379
1380 if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
1381 rsp_rc = *((int *)srp_rsp.srp.rsp.data);
1382 else
1383 rsp_rc = srp_rsp.srp.rsp.status;
1384
1385 if (rsp_rc) {
1386 if (printk_ratelimit())
1387 sdev_printk(KERN_WARNING, cmd->device,
1388 "reset code %d for task tag 0x%llx\n",
1389 rsp_rc, tsk_mgmt->task_tag);
1390 return FAILED;
1391 }
1392
1393
1394
1395
1396 spin_lock_irqsave(hostdata->host->host_lock, flags);
1397 list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
1398 if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
1399 if (tmp_evt->cmnd)
1400 tmp_evt->cmnd->result = (DID_RESET << 16);
1401 list_del(&tmp_evt->list);
1402 unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
1403 tmp_evt->hostdata->dev);
1404 free_event_struct(&tmp_evt->hostdata->pool,
1405 tmp_evt);
1406 atomic_inc(&hostdata->request_limit);
1407 if (tmp_evt->cmnd_done)
1408 tmp_evt->cmnd_done(tmp_evt->cmnd);
1409 else if (tmp_evt->done)
1410 tmp_evt->done(tmp_evt);
1411 }
1412 }
1413 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1414 return SUCCESS;
1415}
1416
1417
1418
1419
1420
1421static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
1422{
1423 unsigned long wait_switch = 0;
1424 struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
1425
1426 dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
1427
1428 ibmvscsi_reset_host(hostdata);
1429
1430 for (wait_switch = jiffies + (init_timeout * HZ);
1431 time_before(jiffies, wait_switch) &&
1432 atomic_read(&hostdata->request_limit) < 2;) {
1433
1434 msleep(10);
1435 }
1436
1437 if (atomic_read(&hostdata->request_limit) <= 0)
1438 return FAILED;
1439
1440 return SUCCESS;
1441}
1442
1443
1444
1445
1446
1447
1448
1449void ibmvscsi_handle_crq(struct viosrp_crq *crq,
1450 struct ibmvscsi_host_data *hostdata)
1451{
1452 long rc;
1453 unsigned long flags;
1454 struct srp_event_struct *evt_struct =
1455 (struct srp_event_struct *)crq->IU_data_ptr;
1456 switch (crq->valid) {
1457 case 0xC0:
1458 switch (crq->format) {
1459 case 0x01:
1460 dev_info(hostdata->dev, "partner initialized\n");
1461
1462 if ((rc = ibmvscsi_ops->send_crq(hostdata,
1463 0xC002000000000000LL, 0)) == 0) {
1464
1465 init_adapter(hostdata);
1466 } else {
1467 dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
1468 }
1469
1470 break;
1471 case 0x02:
1472 dev_info(hostdata->dev, "partner initialization complete\n");
1473
1474
1475 init_adapter(hostdata);
1476 break;
1477 default:
1478 dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
1479 }
1480 return;
1481 case 0xFF:
1482 scsi_block_requests(hostdata->host);
1483 atomic_set(&hostdata->request_limit, 0);
1484 if (crq->format == 0x06) {
1485
1486 dev_info(hostdata->dev, "Re-enabling adapter!\n");
1487 hostdata->client_migrated = 1;
1488 purge_requests(hostdata, DID_REQUEUE);
1489 if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
1490 hostdata)) ||
1491 (ibmvscsi_ops->send_crq(hostdata,
1492 0xC001000000000000LL, 0))) {
1493 atomic_set(&hostdata->request_limit,
1494 -1);
1495 dev_err(hostdata->dev, "error after enable\n");
1496 }
1497 } else {
1498 dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
1499 crq->format);
1500
1501 purge_requests(hostdata, DID_ERROR);
1502 if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
1503 hostdata)) ||
1504 (ibmvscsi_ops->send_crq(hostdata,
1505 0xC001000000000000LL, 0))) {
1506 atomic_set(&hostdata->request_limit,
1507 -1);
1508 dev_err(hostdata->dev, "error after reset\n");
1509 }
1510 }
1511 scsi_unblock_requests(hostdata->host);
1512 return;
1513 case 0x80:
1514 break;
1515 default:
1516 dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
1517 crq->valid);
1518 return;
1519 }
1520
1521
1522
1523
1524
1525 if (!valid_event_struct(&hostdata->pool, evt_struct)) {
1526 dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
1527 (void *)crq->IU_data_ptr);
1528 return;
1529 }
1530
1531 if (atomic_read(&evt_struct->free)) {
1532 dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
1533 (void *)crq->IU_data_ptr);
1534 return;
1535 }
1536
1537 if (crq->format == VIOSRP_SRP_FORMAT)
1538 atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
1539 &hostdata->request_limit);
1540
1541 del_timer(&evt_struct->timer);
1542
1543 if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
1544 evt_struct->cmnd->result = DID_ERROR << 16;
1545 if (evt_struct->done)
1546 evt_struct->done(evt_struct);
1547 else
1548 dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
1549
1550
1551
1552
1553
1554 spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
1555 list_del(&evt_struct->list);
1556 free_event_struct(&evt_struct->hostdata->pool, evt_struct);
1557 spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
1558}
1559
1560
1561
1562
1563
1564static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
1565 unsigned char *buffer, int length)
1566{
1567 struct viosrp_host_config *host_config;
1568 struct srp_event_struct *evt_struct;
1569 unsigned long flags;
1570 dma_addr_t addr;
1571 int rc;
1572
1573 evt_struct = get_event_struct(&hostdata->pool);
1574 if (!evt_struct) {
1575 dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
1576 return -1;
1577 }
1578
1579 init_event_struct(evt_struct,
1580 sync_completion,
1581 VIOSRP_MAD_FORMAT,
1582 info_timeout);
1583
1584 host_config = &evt_struct->iu.mad.host_config;
1585
1586
1587 memset(host_config, 0x00, sizeof(*host_config));
1588 host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
1589 host_config->common.length = length;
1590 host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
1591 length,
1592 DMA_BIDIRECTIONAL);
1593
1594 if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
1595 if (!firmware_has_feature(FW_FEATURE_CMO))
1596 dev_err(hostdata->dev,
1597 "dma_mapping error getting host config\n");
1598 free_event_struct(&hostdata->pool, evt_struct);
1599 return -1;
1600 }
1601
1602 init_completion(&evt_struct->comp);
1603 spin_lock_irqsave(hostdata->host->host_lock, flags);
1604 rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
1605 spin_unlock_irqrestore(hostdata->host->host_lock, flags);
1606 if (rc == 0)
1607 wait_for_completion(&evt_struct->comp);
1608 dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
1609
1610 return rc;
1611}
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621static int ibmvscsi_slave_configure(struct scsi_device *sdev)
1622{
1623 struct Scsi_Host *shost = sdev->host;
1624 unsigned long lock_flags = 0;
1625
1626 spin_lock_irqsave(shost->host_lock, lock_flags);
1627 if (sdev->type == TYPE_DISK) {
1628 sdev->allow_restart = 1;
1629 blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
1630 }
1631 scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
1632 spin_unlock_irqrestore(shost->host_lock, lock_flags);
1633 return 0;
1634}
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth)
1645{
1646 if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
1647 qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
1648
1649 scsi_adjust_queue_depth(sdev, 0, qdepth);
1650 return sdev->queue_depth;
1651}
1652
1653
1654
1655
1656static ssize_t show_host_vhost_loc(struct device *dev,
1657 struct device_attribute *attr, char *buf)
1658{
1659 struct Scsi_Host *shost = class_to_shost(dev);
1660 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1661 int len;
1662
1663 len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
1664 hostdata->caps.loc);
1665 return len;
1666}
1667
1668static struct device_attribute ibmvscsi_host_vhost_loc = {
1669 .attr = {
1670 .name = "vhost_loc",
1671 .mode = S_IRUGO,
1672 },
1673 .show = show_host_vhost_loc,
1674};
1675
1676static ssize_t show_host_vhost_name(struct device *dev,
1677 struct device_attribute *attr, char *buf)
1678{
1679 struct Scsi_Host *shost = class_to_shost(dev);
1680 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1681 int len;
1682
1683 len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
1684 hostdata->caps.name);
1685 return len;
1686}
1687
1688static struct device_attribute ibmvscsi_host_vhost_name = {
1689 .attr = {
1690 .name = "vhost_name",
1691 .mode = S_IRUGO,
1692 },
1693 .show = show_host_vhost_name,
1694};
1695
1696static ssize_t show_host_srp_version(struct device *dev,
1697 struct device_attribute *attr, char *buf)
1698{
1699 struct Scsi_Host *shost = class_to_shost(dev);
1700 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1701 int len;
1702
1703 len = snprintf(buf, PAGE_SIZE, "%s\n",
1704 hostdata->madapter_info.srp_version);
1705 return len;
1706}
1707
1708static struct device_attribute ibmvscsi_host_srp_version = {
1709 .attr = {
1710 .name = "srp_version",
1711 .mode = S_IRUGO,
1712 },
1713 .show = show_host_srp_version,
1714};
1715
1716static ssize_t show_host_partition_name(struct device *dev,
1717 struct device_attribute *attr,
1718 char *buf)
1719{
1720 struct Scsi_Host *shost = class_to_shost(dev);
1721 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1722 int len;
1723
1724 len = snprintf(buf, PAGE_SIZE, "%s\n",
1725 hostdata->madapter_info.partition_name);
1726 return len;
1727}
1728
1729static struct device_attribute ibmvscsi_host_partition_name = {
1730 .attr = {
1731 .name = "partition_name",
1732 .mode = S_IRUGO,
1733 },
1734 .show = show_host_partition_name,
1735};
1736
1737static ssize_t show_host_partition_number(struct device *dev,
1738 struct device_attribute *attr,
1739 char *buf)
1740{
1741 struct Scsi_Host *shost = class_to_shost(dev);
1742 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1743 int len;
1744
1745 len = snprintf(buf, PAGE_SIZE, "%d\n",
1746 hostdata->madapter_info.partition_number);
1747 return len;
1748}
1749
1750static struct device_attribute ibmvscsi_host_partition_number = {
1751 .attr = {
1752 .name = "partition_number",
1753 .mode = S_IRUGO,
1754 },
1755 .show = show_host_partition_number,
1756};
1757
1758static ssize_t show_host_mad_version(struct device *dev,
1759 struct device_attribute *attr, char *buf)
1760{
1761 struct Scsi_Host *shost = class_to_shost(dev);
1762 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1763 int len;
1764
1765 len = snprintf(buf, PAGE_SIZE, "%d\n",
1766 hostdata->madapter_info.mad_version);
1767 return len;
1768}
1769
1770static struct device_attribute ibmvscsi_host_mad_version = {
1771 .attr = {
1772 .name = "mad_version",
1773 .mode = S_IRUGO,
1774 },
1775 .show = show_host_mad_version,
1776};
1777
1778static ssize_t show_host_os_type(struct device *dev,
1779 struct device_attribute *attr, char *buf)
1780{
1781 struct Scsi_Host *shost = class_to_shost(dev);
1782 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1783 int len;
1784
1785 len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
1786 return len;
1787}
1788
1789static struct device_attribute ibmvscsi_host_os_type = {
1790 .attr = {
1791 .name = "os_type",
1792 .mode = S_IRUGO,
1793 },
1794 .show = show_host_os_type,
1795};
1796
1797static ssize_t show_host_config(struct device *dev,
1798 struct device_attribute *attr, char *buf)
1799{
1800 struct Scsi_Host *shost = class_to_shost(dev);
1801 struct ibmvscsi_host_data *hostdata = shost_priv(shost);
1802
1803
1804 if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
1805 return strlen(buf);
1806 else
1807 return 0;
1808}
1809
1810static struct device_attribute ibmvscsi_host_config = {
1811 .attr = {
1812 .name = "config",
1813 .mode = S_IRUGO,
1814 },
1815 .show = show_host_config,
1816};
1817
1818static struct device_attribute *ibmvscsi_attrs[] = {
1819 &ibmvscsi_host_vhost_loc,
1820 &ibmvscsi_host_vhost_name,
1821 &ibmvscsi_host_srp_version,
1822 &ibmvscsi_host_partition_name,
1823 &ibmvscsi_host_partition_number,
1824 &ibmvscsi_host_mad_version,
1825 &ibmvscsi_host_os_type,
1826 &ibmvscsi_host_config,
1827 NULL
1828};
1829
1830
1831
1832
1833static struct scsi_host_template driver_template = {
1834 .module = THIS_MODULE,
1835 .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
1836 .proc_name = "ibmvscsi",
1837 .queuecommand = ibmvscsi_queuecommand,
1838 .eh_abort_handler = ibmvscsi_eh_abort_handler,
1839 .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
1840 .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
1841 .slave_configure = ibmvscsi_slave_configure,
1842 .change_queue_depth = ibmvscsi_change_queue_depth,
1843 .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
1844 .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
1845 .this_id = -1,
1846 .sg_tablesize = SG_ALL,
1847 .use_clustering = ENABLE_CLUSTERING,
1848 .shost_attrs = ibmvscsi_attrs,
1849};
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
1860{
1861
1862 unsigned long desired_io = max_events * sizeof(union viosrp_iu);
1863
1864
1865 desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
1866 IBMVSCSI_CMDS_PER_LUN_DEFAULT);
1867
1868 return desired_io;
1869}
1870
1871
1872
1873
1874static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
1875{
1876 struct ibmvscsi_host_data *hostdata;
1877 struct Scsi_Host *host;
1878 struct device *dev = &vdev->dev;
1879 struct srp_rport_identifiers ids;
1880 struct srp_rport *rport;
1881 unsigned long wait_switch = 0;
1882 int rc;
1883
1884 dev_set_drvdata(&vdev->dev, NULL);
1885
1886 host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
1887 if (!host) {
1888 dev_err(&vdev->dev, "couldn't allocate host data\n");
1889 goto scsi_host_alloc_failed;
1890 }
1891
1892 host->transportt = ibmvscsi_transport_template;
1893 hostdata = shost_priv(host);
1894 memset(hostdata, 0x00, sizeof(*hostdata));
1895 INIT_LIST_HEAD(&hostdata->sent);
1896 hostdata->host = host;
1897 hostdata->dev = dev;
1898 atomic_set(&hostdata->request_limit, -1);
1899 hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
1900
1901 if (map_persist_bufs(hostdata)) {
1902 dev_err(&vdev->dev, "couldn't map persistent buffers\n");
1903 goto persist_bufs_failed;
1904 }
1905
1906 rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
1907 if (rc != 0 && rc != H_RESOURCE) {
1908 dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
1909 goto init_crq_failed;
1910 }
1911 if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
1912 dev_err(&vdev->dev, "couldn't initialize event pool\n");
1913 goto init_pool_failed;
1914 }
1915
1916 host->max_lun = 8;
1917 host->max_id = max_id;
1918 host->max_channel = max_channel;
1919 host->max_cmd_len = 16;
1920
1921 if (scsi_add_host(hostdata->host, hostdata->dev))
1922 goto add_host_failed;
1923
1924
1925 memcpy(ids.port_id, hostdata->madapter_info.partition_name,
1926 sizeof(ids.port_id));
1927 ids.roles = SRP_RPORT_ROLE_TARGET;
1928 rport = srp_rport_add(host, &ids);
1929 if (IS_ERR(rport))
1930 goto add_srp_port_failed;
1931
1932
1933
1934
1935
1936 if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
1937 || rc == H_RESOURCE) {
1938
1939
1940
1941
1942
1943
1944 for (wait_switch = jiffies + (init_timeout * HZ);
1945 time_before(jiffies, wait_switch) &&
1946 atomic_read(&hostdata->request_limit) < 2;) {
1947
1948 msleep(10);
1949 }
1950
1951
1952 if (atomic_read(&hostdata->request_limit) > 0)
1953 scsi_scan_host(host);
1954 }
1955
1956 dev_set_drvdata(&vdev->dev, hostdata);
1957 return 0;
1958
1959 add_srp_port_failed:
1960 scsi_remove_host(hostdata->host);
1961 add_host_failed:
1962 release_event_pool(&hostdata->pool, hostdata);
1963 init_pool_failed:
1964 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
1965 init_crq_failed:
1966 unmap_persist_bufs(hostdata);
1967 persist_bufs_failed:
1968 scsi_host_put(host);
1969 scsi_host_alloc_failed:
1970 return -1;
1971}
1972
1973static int ibmvscsi_remove(struct vio_dev *vdev)
1974{
1975 struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
1976 unmap_persist_bufs(hostdata);
1977 release_event_pool(&hostdata->pool, hostdata);
1978 ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
1979 max_events);
1980
1981 srp_remove_host(hostdata->host);
1982 scsi_remove_host(hostdata->host);
1983 scsi_host_put(hostdata->host);
1984
1985 return 0;
1986}
1987
1988
1989
1990
1991
1992static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
1993 {"vscsi", "IBM,v-scsi"},
1994 { "", "" }
1995};
1996MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
1997
1998static struct vio_driver ibmvscsi_driver = {
1999 .id_table = ibmvscsi_device_table,
2000 .probe = ibmvscsi_probe,
2001 .remove = ibmvscsi_remove,
2002 .get_desired_dma = ibmvscsi_get_desired_dma,
2003 .driver = {
2004 .name = "ibmvscsi",
2005 .owner = THIS_MODULE,
2006 }
2007};
2008
2009static struct srp_function_template ibmvscsi_transport_functions = {
2010};
2011
2012int __init ibmvscsi_module_init(void)
2013{
2014 int ret;
2015
2016
2017 driver_template.can_queue = max_requests;
2018 max_events = max_requests + 2;
2019
2020 if (firmware_has_feature(FW_FEATURE_ISERIES))
2021 ibmvscsi_ops = &iseriesvscsi_ops;
2022 else if (firmware_has_feature(FW_FEATURE_VIO))
2023 ibmvscsi_ops = &rpavscsi_ops;
2024 else
2025 return -ENODEV;
2026
2027 ibmvscsi_transport_template =
2028 srp_attach_transport(&ibmvscsi_transport_functions);
2029 if (!ibmvscsi_transport_template)
2030 return -ENOMEM;
2031
2032 ret = vio_register_driver(&ibmvscsi_driver);
2033 if (ret)
2034 srp_release_transport(ibmvscsi_transport_template);
2035 return ret;
2036}
2037
2038void __exit ibmvscsi_module_exit(void)
2039{
2040 vio_unregister_driver(&ibmvscsi_driver);
2041 srp_release_transport(ibmvscsi_transport_template);
2042}
2043
2044module_init(ibmvscsi_module_init);
2045module_exit(ibmvscsi_module_exit);
2046