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