Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 2 | * zfcp device driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 4 | * Setup and helper functions to access QDIO. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 6 | * Copyright IBM Corporation 2002, 2009 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Christof Schmitt | ecf39d4 | 2008-12-25 13:39:53 +0100 | [diff] [blame] | 9 | #define KMSG_COMPONENT "zfcp" |
| 10 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 11 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "zfcp_ext.h" |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 14 | #include "zfcp_qdio.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Christof Schmitt | 5d4e226 | 2008-07-02 10:56:34 +0200 | [diff] [blame] | 16 | #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 18 | static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal) |
Swen Schillig | b4e4459 | 2007-07-18 10:55:13 +0200 | [diff] [blame] | 19 | { |
| 20 | int pos; |
| 21 | |
| 22 | for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 23 | sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL); |
| 24 | if (!sbal[pos]) |
Swen Schillig | b4e4459 | 2007-07-18 10:55:13 +0200 | [diff] [blame] | 25 | return -ENOMEM; |
Swen Schillig | b4e4459 | 2007-07-18 10:55:13 +0200 | [diff] [blame] | 26 | } |
| 27 | for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++) |
| 28 | if (pos % QBUFF_PER_PAGE) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 29 | sbal[pos] = sbal[pos - 1] + 1; |
Swen Schillig | b4e4459 | 2007-07-18 10:55:13 +0200 | [diff] [blame] | 30 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 33 | static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 34 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 35 | struct zfcp_adapter *adapter = qdio->adapter; |
| 36 | |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 37 | dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n"); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 38 | |
| 39 | zfcp_erp_adapter_reopen(adapter, |
| 40 | ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED | |
| 41 | ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL); |
| 42 | } |
| 43 | |
Christof Schmitt | 5d4e226 | 2008-07-02 10:56:34 +0200 | [diff] [blame] | 44 | static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt) |
| 45 | { |
| 46 | int i, sbal_idx; |
| 47 | |
| 48 | for (i = first; i < first + cnt; i++) { |
| 49 | sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q; |
| 50 | memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer)); |
| 51 | } |
| 52 | } |
| 53 | |
Martin Peschke | 94506fd | 2009-03-02 13:08:56 +0100 | [diff] [blame] | 54 | /* this needs to be called prior to updating the queue fill level */ |
Heiko Carstens | 41e05a1 | 2009-08-18 15:43:32 +0200 | [diff] [blame] | 55 | static inline void zfcp_qdio_account(struct zfcp_qdio *qdio) |
Martin Peschke | 94506fd | 2009-03-02 13:08:56 +0100 | [diff] [blame] | 56 | { |
Heiko Carstens | 41e05a1 | 2009-08-18 15:43:32 +0200 | [diff] [blame] | 57 | unsigned long long now, span; |
Martin Peschke | 94506fd | 2009-03-02 13:08:56 +0100 | [diff] [blame] | 58 | int free, used; |
| 59 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 60 | spin_lock(&qdio->stat_lock); |
Heiko Carstens | 41e05a1 | 2009-08-18 15:43:32 +0200 | [diff] [blame] | 61 | now = get_clock_monotonic(); |
| 62 | span = (now - qdio->req_q_time) >> 12; |
| 63 | free = atomic_read(&qdio->req_q.count); |
Martin Peschke | 94506fd | 2009-03-02 13:08:56 +0100 | [diff] [blame] | 64 | used = QDIO_MAX_BUFFERS_PER_Q - free; |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 65 | qdio->req_q_util += used * span; |
| 66 | qdio->req_q_time = now; |
| 67 | spin_unlock(&qdio->stat_lock); |
Martin Peschke | 94506fd | 2009-03-02 13:08:56 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 70 | static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err, |
| 71 | int queue_no, int first, int count, |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 72 | unsigned long parm) |
| 73 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 74 | struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm; |
| 75 | struct zfcp_qdio_queue *queue = &qdio->req_q; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 76 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 77 | if (unlikely(qdio_err)) { |
Swen Schillig | 5771710 | 2009-08-18 15:43:21 +0200 | [diff] [blame] | 78 | zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first, |
| 79 | count); |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 80 | zfcp_qdio_handler_error(qdio, "qdireq1"); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
| 84 | /* cleanup all SBALs being program-owned now */ |
| 85 | zfcp_qdio_zero_sbals(queue->sbal, first, count); |
| 86 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 87 | zfcp_qdio_account(qdio); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 88 | atomic_add(count, &queue->count); |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 89 | wake_up(&qdio->req_q_wq); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 92 | static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 94 | struct zfcp_qdio_queue *queue = &qdio->resp_q; |
| 95 | struct ccw_device *cdev = qdio->adapter->ccw_device; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 96 | u8 count, start = queue->first; |
| 97 | unsigned int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 99 | count = atomic_read(&queue->count) + processed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 101 | retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 103 | if (unlikely(retval)) { |
| 104 | atomic_set(&queue->count, count); |
Christof Schmitt | 452b505 | 2010-02-17 11:18:51 +0100 | [diff] [blame] | 105 | zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdrpb_1", NULL); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 106 | } else { |
| 107 | queue->first += count; |
| 108 | queue->first %= QDIO_MAX_BUFFERS_PER_Q; |
| 109 | atomic_set(&queue->count, 0); |
| 110 | } |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 113 | static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err, |
| 114 | int queue_no, int first, int count, |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 115 | unsigned long parm) |
| 116 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 117 | struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm; |
Swen Schillig | bd63eaf | 2009-08-18 15:43:13 +0200 | [diff] [blame] | 118 | int sbal_idx, sbal_no; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 119 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 120 | if (unlikely(qdio_err)) { |
Swen Schillig | 5771710 | 2009-08-18 15:43:21 +0200 | [diff] [blame] | 121 | zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first, |
| 122 | count); |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 123 | zfcp_qdio_handler_error(qdio, "qdires1"); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* |
| 128 | * go through all SBALs from input queue currently |
| 129 | * returned by QDIO layer |
| 130 | */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 131 | for (sbal_no = 0; sbal_no < count; sbal_no++) { |
| 132 | sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /* go through all SBALEs of SBAL */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 134 | zfcp_fsf_reqid_check(qdio, sbal_idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
| 138 | * put range of SBALs back to response queue |
| 139 | * (including SBALs which have already been free before) |
| 140 | */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 141 | zfcp_qdio_resp_put_back(qdio, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 144 | static void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio, |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 145 | struct zfcp_qdio_req *q_req, int max_sbals) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 147 | int count = atomic_read(&qdio->req_q.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | count = min(count, max_sbals); |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 149 | q_req->sbal_limit = (q_req->sbal_first + count - 1) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 150 | % QDIO_MAX_BUFFERS_PER_Q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 153 | static struct qdio_buffer_element * |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 154 | zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req, |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 155 | unsigned long sbtype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 157 | struct qdio_buffer_element *sbale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* set last entry flag in current SBALE of current SBAL */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 160 | sbale = zfcp_qdio_sbale_curr(qdio, q_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | sbale->flags |= SBAL_FLAGS_LAST_ENTRY; |
| 162 | |
| 163 | /* don't exceed last allowed SBAL */ |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 164 | if (q_req->sbal_last == q_req->sbal_limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | return NULL; |
| 166 | |
| 167 | /* set chaining flag in first SBALE of current SBAL */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 168 | sbale = zfcp_qdio_sbale_req(qdio, q_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | sbale->flags |= SBAL_FLAGS0_MORE_SBALS; |
| 170 | |
| 171 | /* calculate index of next SBAL */ |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 172 | q_req->sbal_last++; |
| 173 | q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | /* keep this requests number of SBALs up-to-date */ |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 176 | q_req->sbal_number++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | /* start at first SBALE of new SBAL */ |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 179 | q_req->sbale_curr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | /* set storage-block type for new SBAL */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 182 | sbale = zfcp_qdio_sbale_curr(qdio, q_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | sbale->flags |= sbtype; |
| 184 | |
| 185 | return sbale; |
| 186 | } |
| 187 | |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 188 | static struct qdio_buffer_element * |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 189 | zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req, |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 190 | unsigned int sbtype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 192 | if (q_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL) |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 193 | return zfcp_qdio_sbal_chain(qdio, q_req, sbtype); |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 194 | q_req->sbale_curr++; |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 195 | return zfcp_qdio_sbale_curr(qdio, q_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 198 | static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio, |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 199 | struct zfcp_qdio_req *q_req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 201 | struct qdio_buffer **sbal = qdio->req_q.sbal; |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 202 | int first = q_req->sbal_first; |
| 203 | int last = q_req->sbal_last; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 204 | int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) % |
| 205 | QDIO_MAX_BUFFERS_PER_Q + 1; |
| 206 | zfcp_qdio_zero_sbals(sbal, first, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | /** |
| 210 | * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list |
| 211 | * @fsf_req: request to be processed |
| 212 | * @sbtype: SBALE flags |
| 213 | * @sg: scatter-gather list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * @max_sbals: upper bound for number of SBALs to be used |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 215 | * Returns: number of bytes, or error (negativ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | */ |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 217 | int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req, |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 218 | unsigned long sbtype, struct scatterlist *sg, |
| 219 | int max_sbals) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 221 | struct qdio_buffer_element *sbale; |
Christof Schmitt | 6832298 | 2010-04-30 18:09:33 +0200 | [diff] [blame^] | 222 | int bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | /* figure out last allowed SBAL */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 225 | zfcp_qdio_sbal_limit(qdio, q_req, max_sbals); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 227 | /* set storage-block type for this request */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 228 | sbale = zfcp_qdio_sbale_req(qdio, q_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | sbale->flags |= sbtype; |
| 230 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 231 | for (; sg; sg = sg_next(sg)) { |
Christof Schmitt | 6832298 | 2010-04-30 18:09:33 +0200 | [diff] [blame^] | 232 | sbale = zfcp_qdio_sbale_next(qdio, q_req, sbtype); |
| 233 | if (!sbale) { |
| 234 | atomic_inc(&qdio->req_q_full); |
| 235 | zfcp_qdio_undo_sbals(qdio, q_req); |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | sbale->addr = sg_virt(sg); |
| 240 | sbale->length = sg->length; |
| 241 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 242 | bytes += sg->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | /* assume that no other SBALEs are to follow in the same SBAL */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 246 | sbale = zfcp_qdio_sbale_curr(qdio, q_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | sbale->flags |= SBAL_FLAGS_LAST_ENTRY; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return bytes; |
| 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /** |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 253 | * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 254 | * @qdio: pointer to struct zfcp_qdio |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 255 | * @q_req: pointer to struct zfcp_qdio_req |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 256 | * Returns: 0 on success, error otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | */ |
Christof Schmitt | 34c2b71 | 2010-02-17 11:18:59 +0100 | [diff] [blame] | 258 | int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 260 | struct zfcp_qdio_queue *req_q = &qdio->req_q; |
Swen Schillig | 42428f7 | 2009-08-18 15:43:18 +0200 | [diff] [blame] | 261 | int first = q_req->sbal_first; |
| 262 | int count = q_req->sbal_number; |
Christof Schmitt | 21ddaa5 | 2009-03-02 13:09:05 +0100 | [diff] [blame] | 263 | int retval; |
| 264 | unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 265 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 266 | zfcp_qdio_account(qdio); |
Martin Peschke | 94506fd | 2009-03-02 13:08:56 +0100 | [diff] [blame] | 267 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 268 | retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first, |
| 269 | count); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 270 | if (unlikely(retval)) { |
| 271 | zfcp_qdio_zero_sbals(req_q->sbal, first, count); |
| 272 | return retval; |
| 273 | } |
| 274 | |
| 275 | /* account for transferred buffers */ |
| 276 | atomic_sub(count, &req_q->count); |
| 277 | req_q->first += count; |
| 278 | req_q->first %= QDIO_MAX_BUFFERS_PER_Q; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 279 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 282 | |
| 283 | static void zfcp_qdio_setup_init_data(struct qdio_initialize *id, |
| 284 | struct zfcp_qdio *qdio) |
| 285 | { |
| 286 | |
| 287 | id->cdev = qdio->adapter->ccw_device; |
| 288 | id->q_format = QDIO_ZFCP_QFMT; |
| 289 | memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8); |
| 290 | ASCEBC(id->adapter_name, 8); |
| 291 | id->qib_param_field_format = 0; |
| 292 | id->qib_param_field = NULL; |
| 293 | id->input_slib_elements = NULL; |
| 294 | id->output_slib_elements = NULL; |
| 295 | id->no_input_qs = 1; |
| 296 | id->no_output_qs = 1; |
| 297 | id->input_handler = zfcp_qdio_int_resp; |
| 298 | id->output_handler = zfcp_qdio_int_req; |
| 299 | id->int_parm = (unsigned long) qdio; |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 300 | id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal); |
| 301 | id->output_sbal_addr_array = (void **) (qdio->req_q.sbal); |
| 302 | |
| 303 | } |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 304 | /** |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 305 | * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data |
| 306 | * @adapter: pointer to struct zfcp_adapter |
| 307 | * Returns: -ENOMEM on memory allocation error or return value from |
| 308 | * qdio_allocate |
| 309 | */ |
Swen Schillig | d5a282a | 2009-08-18 15:43:22 +0200 | [diff] [blame] | 310 | static int zfcp_qdio_allocate(struct zfcp_qdio *qdio) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 311 | { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 312 | struct qdio_initialize init_data; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 313 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 314 | if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) || |
| 315 | zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal)) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 316 | return -ENOMEM; |
| 317 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 318 | zfcp_qdio_setup_init_data(&init_data, qdio); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 319 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 320 | return qdio_allocate(&init_data); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /** |
| 324 | * zfcp_close_qdio - close qdio queues for an adapter |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 325 | * @qdio: pointer to structure zfcp_qdio |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 326 | */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 327 | void zfcp_qdio_close(struct zfcp_qdio *qdio) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 328 | { |
| 329 | struct zfcp_qdio_queue *req_q; |
| 330 | int first, count; |
| 331 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 332 | if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 333 | return; |
| 334 | |
| 335 | /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 336 | req_q = &qdio->req_q; |
| 337 | spin_lock_bh(&qdio->req_q_lock); |
| 338 | atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status); |
| 339 | spin_unlock_bh(&qdio->req_q_lock); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 340 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 341 | qdio_shutdown(qdio->adapter->ccw_device, |
| 342 | QDIO_FLAG_CLEANUP_USING_CLEAR); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 343 | |
| 344 | /* cleanup used outbound sbals */ |
| 345 | count = atomic_read(&req_q->count); |
| 346 | if (count < QDIO_MAX_BUFFERS_PER_Q) { |
| 347 | first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q; |
| 348 | count = QDIO_MAX_BUFFERS_PER_Q - count; |
| 349 | zfcp_qdio_zero_sbals(req_q->sbal, first, count); |
| 350 | } |
| 351 | req_q->first = 0; |
| 352 | atomic_set(&req_q->count, 0); |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 353 | qdio->resp_q.first = 0; |
| 354 | atomic_set(&qdio->resp_q.count, 0); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
| 358 | * zfcp_qdio_open - prepare and initialize response queue |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 359 | * @qdio: pointer to struct zfcp_qdio |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 360 | * Returns: 0 on success, otherwise -EIO |
| 361 | */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 362 | int zfcp_qdio_open(struct zfcp_qdio *qdio) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 363 | { |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 364 | struct qdio_buffer_element *sbale; |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 365 | struct qdio_initialize init_data; |
| 366 | struct ccw_device *cdev = qdio->adapter->ccw_device; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 367 | int cc; |
| 368 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 369 | if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 370 | return -EIO; |
| 371 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 372 | zfcp_qdio_setup_init_data(&init_data, qdio); |
| 373 | |
| 374 | if (qdio_establish(&init_data)) |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 375 | goto failed_establish; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 376 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 377 | if (qdio_activate(cdev)) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 378 | goto failed_qdio; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 379 | |
| 380 | for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) { |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 381 | sbale = &(qdio->resp_q.sbal[cc]->element[0]); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 382 | sbale->length = 0; |
| 383 | sbale->flags = SBAL_FLAGS_LAST_ENTRY; |
| 384 | sbale->addr = NULL; |
| 385 | } |
| 386 | |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 387 | if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0, |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 388 | QDIO_MAX_BUFFERS_PER_Q)) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 389 | goto failed_qdio; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 390 | |
| 391 | /* set index of first avalable SBALS / number of available SBALS */ |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 392 | qdio->req_q.first = 0; |
| 393 | atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 394 | |
| 395 | return 0; |
| 396 | |
| 397 | failed_qdio: |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 398 | qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR); |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 399 | failed_establish: |
Swen Schillig | 564e1c8 | 2009-08-18 15:43:19 +0200 | [diff] [blame] | 400 | dev_err(&cdev->dev, |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 401 | "Setting up the QDIO connection to the FCP adapter failed\n"); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 402 | return -EIO; |
| 403 | } |
Swen Schillig | d5a282a | 2009-08-18 15:43:22 +0200 | [diff] [blame] | 404 | |
| 405 | void zfcp_qdio_destroy(struct zfcp_qdio *qdio) |
| 406 | { |
| 407 | struct qdio_buffer **sbal_req, **sbal_resp; |
| 408 | int p; |
| 409 | |
| 410 | if (!qdio) |
| 411 | return; |
| 412 | |
| 413 | if (qdio->adapter->ccw_device) |
| 414 | qdio_free(qdio->adapter->ccw_device); |
| 415 | |
| 416 | sbal_req = qdio->req_q.sbal; |
| 417 | sbal_resp = qdio->resp_q.sbal; |
| 418 | |
| 419 | for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) { |
| 420 | free_page((unsigned long) sbal_req[p]); |
| 421 | free_page((unsigned long) sbal_resp[p]); |
| 422 | } |
| 423 | |
| 424 | kfree(qdio); |
| 425 | } |
| 426 | |
| 427 | int zfcp_qdio_setup(struct zfcp_adapter *adapter) |
| 428 | { |
| 429 | struct zfcp_qdio *qdio; |
| 430 | |
| 431 | qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL); |
| 432 | if (!qdio) |
| 433 | return -ENOMEM; |
| 434 | |
| 435 | qdio->adapter = adapter; |
| 436 | |
| 437 | if (zfcp_qdio_allocate(qdio)) { |
| 438 | zfcp_qdio_destroy(qdio); |
| 439 | return -ENOMEM; |
| 440 | } |
| 441 | |
| 442 | spin_lock_init(&qdio->req_q_lock); |
| 443 | spin_lock_init(&qdio->stat_lock); |
| 444 | |
| 445 | adapter->qdio = qdio; |
| 446 | return 0; |
| 447 | } |
| 448 | |