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