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