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