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 | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 6 | * Copyright IBM Corporation 2002, 2008 |
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 | |
Christof Schmitt | b03670e | 2007-05-07 16:35:04 +0200 | [diff] [blame] | 115 | static void zfcp_qdio_reqid_check(struct zfcp_adapter *adapter, |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 116 | unsigned long req_id, int sbal_idx) |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 117 | { |
| 118 | struct zfcp_fsf_req *fsf_req; |
| 119 | unsigned long flags; |
| 120 | |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 121 | spin_lock_irqsave(&adapter->req_list_lock, flags); |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 122 | fsf_req = zfcp_reqlist_find(adapter, req_id); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 123 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 124 | if (!fsf_req) |
| 125 | /* |
| 126 | * Unknown request means that we have potentially memory |
| 127 | * corruption and must stop the machine immediatly. |
| 128 | */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 129 | panic("error: unknown request id (%lx) on adapter %s.\n", |
Christof Schmitt | b225cf9 | 2008-12-19 16:57:00 +0100 | [diff] [blame] | 130 | req_id, dev_name(&adapter->ccw_device->dev)); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 131 | |
Heiko Carstens | ca2d02c | 2007-05-08 11:17:54 +0200 | [diff] [blame] | 132 | zfcp_reqlist_remove(adapter, fsf_req); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 133 | spin_unlock_irqrestore(&adapter->req_list_lock, flags); |
| 134 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 135 | fsf_req->sbal_response = sbal_idx; |
Stefan Raspl | 0997f1c | 2008-10-16 08:23:39 +0200 | [diff] [blame] | 136 | fsf_req->qdio_inb_usage = atomic_read(&adapter->resp_q.count); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 137 | zfcp_fsf_req_complete(fsf_req); |
Volker Sameske | fea9d6c | 2006-08-02 11:05:16 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 140 | 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] | 141 | { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 142 | struct zfcp_qdio_queue *queue = &adapter->resp_q; |
| 143 | struct ccw_device *cdev = adapter->ccw_device; |
| 144 | u8 count, start = queue->first; |
| 145 | unsigned int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 147 | count = atomic_read(&queue->count) + processed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 149 | retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 151 | if (unlikely(retval)) { |
| 152 | atomic_set(&queue->count, count); |
| 153 | /* FIXME: Recover this with an adapter reopen? */ |
| 154 | } else { |
| 155 | queue->first += count; |
| 156 | queue->first %= QDIO_MAX_BUFFERS_PER_Q; |
| 157 | atomic_set(&queue->count, 0); |
| 158 | } |
| 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 161 | static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err, |
| 162 | int queue_no, int first, int count, |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 163 | unsigned long parm) |
| 164 | { |
| 165 | struct zfcp_adapter *adapter = (struct zfcp_adapter *) parm; |
| 166 | struct zfcp_qdio_queue *queue = &adapter->resp_q; |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 167 | struct qdio_buffer_element *sbale; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 168 | int sbal_idx, sbale_idx, sbal_no; |
| 169 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 170 | if (unlikely(qdio_err)) { |
| 171 | zfcp_hba_dbf_event_qdio(adapter, qdio_err, first, count); |
Swen Schillig | 5ffd51a | 2009-03-02 13:09:04 +0100 | [diff] [blame] | 172 | zfcp_qdio_handler_error(adapter, "qdires1"); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | /* |
| 177 | * go through all SBALs from input queue currently |
| 178 | * returned by QDIO layer |
| 179 | */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 180 | for (sbal_no = 0; sbal_no < count; sbal_no++) { |
| 181 | sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | /* go through all SBALEs of SBAL */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 184 | for (sbale_idx = 0; sbale_idx < QDIO_MAX_ELEMENTS_PER_BUFFER; |
| 185 | sbale_idx++) { |
| 186 | sbale = zfcp_qdio_sbale(queue, sbal_idx, sbale_idx); |
Christof Schmitt | b03670e | 2007-05-07 16:35:04 +0200 | [diff] [blame] | 187 | zfcp_qdio_reqid_check(adapter, |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 188 | (unsigned long) sbale->addr, |
| 189 | sbal_idx); |
| 190 | if (likely(sbale->flags & SBAL_FLAGS_LAST_ENTRY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | break; |
| 192 | }; |
| 193 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 194 | if (unlikely(!(sbale->flags & SBAL_FLAGS_LAST_ENTRY))) |
| 195 | dev_warn(&adapter->ccw_device->dev, |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 196 | "A QDIO protocol error occurred, " |
| 197 | "operations continue\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
| 201 | * put range of SBALs back to response queue |
| 202 | * (including SBALs which have already been free before) |
| 203 | */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 204 | zfcp_qdio_resp_put_back(adapter, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | /** |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 208 | * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req |
| 209 | * @fsf_req: pointer to struct fsf_req |
| 210 | * Returns: pointer to qdio_buffer_element (SBALE) structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | */ |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 212 | struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_fsf_req *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 214 | return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 218 | * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req |
| 219 | * @fsf_req: pointer to struct fsf_req |
| 220 | * Returns: pointer to qdio_buffer_element (SBALE) structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | */ |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 222 | struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_fsf_req *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 224 | return zfcp_qdio_sbale(&req->adapter->req_q, req->sbal_last, |
| 225 | req->sbale_curr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 228 | static void zfcp_qdio_sbal_limit(struct zfcp_fsf_req *fsf_req, int max_sbals) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 230 | int count = atomic_read(&fsf_req->adapter->req_q.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | count = min(count, max_sbals); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 232 | fsf_req->sbal_limit = (fsf_req->sbal_first + count - 1) |
| 233 | % QDIO_MAX_BUFFERS_PER_Q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 236 | static struct qdio_buffer_element * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | zfcp_qdio_sbal_chain(struct zfcp_fsf_req *fsf_req, unsigned long sbtype) |
| 238 | { |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 239 | struct qdio_buffer_element *sbale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | /* set last entry flag in current SBALE of current SBAL */ |
| 242 | sbale = zfcp_qdio_sbale_curr(fsf_req); |
| 243 | sbale->flags |= SBAL_FLAGS_LAST_ENTRY; |
| 244 | |
| 245 | /* don't exceed last allowed SBAL */ |
Martin Peschke | e891bff | 2008-05-19 12:17:43 +0200 | [diff] [blame] | 246 | if (fsf_req->sbal_last == fsf_req->sbal_limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return NULL; |
| 248 | |
| 249 | /* set chaining flag in first SBALE of current SBAL */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 250 | sbale = zfcp_qdio_sbale_req(fsf_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | sbale->flags |= SBAL_FLAGS0_MORE_SBALS; |
| 252 | |
| 253 | /* calculate index of next SBAL */ |
Martin Peschke | e891bff | 2008-05-19 12:17:43 +0200 | [diff] [blame] | 254 | fsf_req->sbal_last++; |
| 255 | fsf_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | /* keep this requests number of SBALs up-to-date */ |
| 258 | fsf_req->sbal_number++; |
| 259 | |
| 260 | /* start at first SBALE of new SBAL */ |
| 261 | fsf_req->sbale_curr = 0; |
| 262 | |
| 263 | /* set storage-block type for new SBAL */ |
| 264 | sbale = zfcp_qdio_sbale_curr(fsf_req); |
| 265 | sbale->flags |= sbtype; |
| 266 | |
| 267 | return sbale; |
| 268 | } |
| 269 | |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 270 | static struct qdio_buffer_element * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | zfcp_qdio_sbale_next(struct zfcp_fsf_req *fsf_req, unsigned long sbtype) |
| 272 | { |
| 273 | if (fsf_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL) |
| 274 | return zfcp_qdio_sbal_chain(fsf_req, sbtype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | fsf_req->sbale_curr++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | return zfcp_qdio_sbale_curr(fsf_req); |
| 277 | } |
| 278 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 279 | static void zfcp_qdio_undo_sbals(struct zfcp_fsf_req *fsf_req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 281 | struct qdio_buffer **sbal = fsf_req->adapter->req_q.sbal; |
| 282 | int first = fsf_req->sbal_first; |
| 283 | int last = fsf_req->sbal_last; |
| 284 | int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) % |
| 285 | QDIO_MAX_BUFFERS_PER_Q + 1; |
| 286 | zfcp_qdio_zero_sbals(sbal, first, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 289 | static int zfcp_qdio_fill_sbals(struct zfcp_fsf_req *fsf_req, |
| 290 | unsigned int sbtype, void *start_addr, |
| 291 | unsigned int total_length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 293 | struct qdio_buffer_element *sbale; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | unsigned long remaining, length; |
| 295 | void *addr; |
| 296 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 297 | /* split segment up */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | for (addr = start_addr, remaining = total_length; remaining > 0; |
| 299 | addr += length, remaining -= length) { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 300 | sbale = zfcp_qdio_sbale_next(fsf_req, sbtype); |
| 301 | if (!sbale) { |
Stefan Raspl | 2450d3e | 2008-10-01 12:42:14 +0200 | [diff] [blame] | 302 | atomic_inc(&fsf_req->adapter->qdio_outb_full); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 303 | zfcp_qdio_undo_sbals(fsf_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | return -EINVAL; |
| 305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 307 | /* new piece must not exceed next page boundary */ |
| 308 | length = min(remaining, |
| 309 | (PAGE_SIZE - ((unsigned long)addr & |
| 310 | (PAGE_SIZE - 1)))); |
| 311 | sbale->addr = addr; |
| 312 | sbale->length = length; |
| 313 | } |
| 314 | return 0; |
| 315 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list |
| 319 | * @fsf_req: request to be processed |
| 320 | * @sbtype: SBALE flags |
| 321 | * @sg: scatter-gather list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | * @max_sbals: upper bound for number of SBALs to be used |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 323 | * Returns: number of bytes, or error (negativ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 325 | int zfcp_qdio_sbals_from_sg(struct zfcp_fsf_req *fsf_req, unsigned long sbtype, |
| 326 | struct scatterlist *sg, int max_sbals) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 328 | struct qdio_buffer_element *sbale; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 329 | int retval, bytes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
| 331 | /* figure out last allowed SBAL */ |
| 332 | zfcp_qdio_sbal_limit(fsf_req, max_sbals); |
| 333 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 334 | /* set storage-block type for this request */ |
| 335 | sbale = zfcp_qdio_sbale_req(fsf_req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | sbale->flags |= sbtype; |
| 337 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 338 | for (; sg; sg = sg_next(sg)) { |
| 339 | retval = zfcp_qdio_fill_sbals(fsf_req, sbtype, sg_virt(sg), |
| 340 | sg->length); |
| 341 | if (retval < 0) |
| 342 | return retval; |
| 343 | bytes += sg->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /* assume that no other SBALEs are to follow in the same SBAL */ |
| 347 | sbale = zfcp_qdio_sbale_curr(fsf_req); |
| 348 | sbale->flags |= SBAL_FLAGS_LAST_ENTRY; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return bytes; |
| 351 | } |
| 352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | /** |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 354 | * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO |
| 355 | * @fsf_req: pointer to struct zfcp_fsf_req |
| 356 | * Returns: 0 on success, error otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | */ |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 358 | int zfcp_qdio_send(struct zfcp_fsf_req *fsf_req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 360 | struct zfcp_adapter *adapter = fsf_req->adapter; |
| 361 | struct zfcp_qdio_queue *req_q = &adapter->req_q; |
| 362 | int first = fsf_req->sbal_first; |
| 363 | int count = fsf_req->sbal_number; |
Christof Schmitt | 21ddaa5 | 2009-03-02 13:09:05 +0100 | [diff] [blame^] | 364 | int retval; |
| 365 | unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 366 | |
Martin Peschke | 94506fd | 2009-03-02 13:08:56 +0100 | [diff] [blame] | 367 | zfcp_qdio_account(adapter); |
| 368 | |
Christof Schmitt | 21ddaa5 | 2009-03-02 13:09:05 +0100 | [diff] [blame^] | 369 | retval = do_QDIO(adapter->ccw_device, qdio_flags, 0, first, count); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 370 | if (unlikely(retval)) { |
| 371 | zfcp_qdio_zero_sbals(req_q->sbal, first, count); |
| 372 | return retval; |
| 373 | } |
| 374 | |
| 375 | /* account for transferred buffers */ |
| 376 | atomic_sub(count, &req_q->count); |
| 377 | req_q->first += count; |
| 378 | req_q->first %= QDIO_MAX_BUFFERS_PER_Q; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 379 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 382 | /** |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 383 | * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data |
| 384 | * @adapter: pointer to struct zfcp_adapter |
| 385 | * Returns: -ENOMEM on memory allocation error or return value from |
| 386 | * qdio_allocate |
| 387 | */ |
| 388 | int zfcp_qdio_allocate(struct zfcp_adapter *adapter) |
| 389 | { |
| 390 | struct qdio_initialize *init_data; |
| 391 | |
| 392 | if (zfcp_qdio_buffers_enqueue(adapter->req_q.sbal) || |
| 393 | zfcp_qdio_buffers_enqueue(adapter->resp_q.sbal)) |
| 394 | return -ENOMEM; |
| 395 | |
| 396 | init_data = &adapter->qdio_init_data; |
| 397 | |
| 398 | init_data->cdev = adapter->ccw_device; |
| 399 | init_data->q_format = QDIO_ZFCP_QFMT; |
Christof Schmitt | b225cf9 | 2008-12-19 16:57:00 +0100 | [diff] [blame] | 400 | memcpy(init_data->adapter_name, dev_name(&adapter->ccw_device->dev), 8); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 401 | ASCEBC(init_data->adapter_name, 8); |
| 402 | init_data->qib_param_field_format = 0; |
| 403 | init_data->qib_param_field = NULL; |
| 404 | init_data->input_slib_elements = NULL; |
| 405 | init_data->output_slib_elements = NULL; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 406 | init_data->no_input_qs = 1; |
| 407 | init_data->no_output_qs = 1; |
| 408 | init_data->input_handler = zfcp_qdio_int_resp; |
| 409 | init_data->output_handler = zfcp_qdio_int_req; |
| 410 | init_data->int_parm = (unsigned long) adapter; |
| 411 | init_data->flags = QDIO_INBOUND_0COPY_SBALS | |
| 412 | QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS; |
| 413 | init_data->input_sbal_addr_array = |
| 414 | (void **) (adapter->resp_q.sbal); |
| 415 | init_data->output_sbal_addr_array = |
| 416 | (void **) (adapter->req_q.sbal); |
| 417 | |
| 418 | return qdio_allocate(init_data); |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * zfcp_close_qdio - close qdio queues for an adapter |
| 423 | */ |
| 424 | void zfcp_qdio_close(struct zfcp_adapter *adapter) |
| 425 | { |
| 426 | struct zfcp_qdio_queue *req_q; |
| 427 | int first, count; |
| 428 | |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 429 | if (!(atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 430 | return; |
| 431 | |
| 432 | /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */ |
| 433 | req_q = &adapter->req_q; |
Christof Schmitt | 0406289 | 2008-10-01 12:42:20 +0200 | [diff] [blame] | 434 | spin_lock_bh(&adapter->req_q_lock); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 435 | atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &adapter->status); |
Christof Schmitt | 0406289 | 2008-10-01 12:42:20 +0200 | [diff] [blame] | 436 | spin_unlock_bh(&adapter->req_q_lock); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 437 | |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 438 | qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 439 | |
| 440 | /* cleanup used outbound sbals */ |
| 441 | count = atomic_read(&req_q->count); |
| 442 | if (count < QDIO_MAX_BUFFERS_PER_Q) { |
| 443 | first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q; |
| 444 | count = QDIO_MAX_BUFFERS_PER_Q - count; |
| 445 | zfcp_qdio_zero_sbals(req_q->sbal, first, count); |
| 446 | } |
| 447 | req_q->first = 0; |
| 448 | atomic_set(&req_q->count, 0); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 449 | adapter->resp_q.first = 0; |
| 450 | atomic_set(&adapter->resp_q.count, 0); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * zfcp_qdio_open - prepare and initialize response queue |
| 455 | * @adapter: pointer to struct zfcp_adapter |
| 456 | * Returns: 0 on success, otherwise -EIO |
| 457 | */ |
| 458 | int zfcp_qdio_open(struct zfcp_adapter *adapter) |
| 459 | { |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 460 | struct qdio_buffer_element *sbale; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 461 | int cc; |
| 462 | |
Swen Schillig | 44cc76f | 2008-10-01 12:42:16 +0200 | [diff] [blame] | 463 | if (atomic_read(&adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 464 | return -EIO; |
| 465 | |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 466 | if (qdio_establish(&adapter->qdio_init_data)) |
| 467 | goto failed_establish; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 468 | |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 469 | if (qdio_activate(adapter->ccw_device)) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 470 | goto failed_qdio; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 471 | |
| 472 | for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) { |
| 473 | sbale = &(adapter->resp_q.sbal[cc]->element[0]); |
| 474 | sbale->length = 0; |
| 475 | sbale->flags = SBAL_FLAGS_LAST_ENTRY; |
| 476 | sbale->addr = NULL; |
| 477 | } |
| 478 | |
| 479 | if (do_QDIO(adapter->ccw_device, QDIO_FLAG_SYNC_INPUT, 0, 0, |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 480 | QDIO_MAX_BUFFERS_PER_Q)) |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 481 | goto failed_qdio; |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 482 | |
| 483 | /* set index of first avalable SBALS / number of available SBALS */ |
| 484 | adapter->req_q.first = 0; |
| 485 | atomic_set(&adapter->req_q.count, QDIO_MAX_BUFFERS_PER_Q); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 486 | |
| 487 | return 0; |
| 488 | |
| 489 | failed_qdio: |
Jan Glauber | 779e6e1 | 2008-07-17 17:16:48 +0200 | [diff] [blame] | 490 | qdio_shutdown(adapter->ccw_device, QDIO_FLAG_CLEANUP_USING_CLEAR); |
Christof Schmitt | ff3b24f | 2008-10-01 12:42:15 +0200 | [diff] [blame] | 491 | failed_establish: |
| 492 | dev_err(&adapter->ccw_device->dev, |
| 493 | "Setting up the QDIO connection to the FCP adapter failed\n"); |
Swen Schillig | 00bab91 | 2008-06-10 18:20:57 +0200 | [diff] [blame] | 494 | return -EIO; |
| 495 | } |