1 // SPDX-License-Identifier: GPL-2.0
3 * Linux Driver for Mylex DAC960/AcceleRAID/eXtremeRAID PCI RAID Controllers
5 * Copyright 2017 Hannes Reinecke, SUSE Linux GmbH <hare@suse.com>
7 * Based on the original DAC960 driver,
8 * Copyright 1998-2001 by Leonard N. Zubkoff <lnz@dandelion.com>
9 * Portions Copyright 2002 by Mylex (An IBM Business Unit)
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/pci.h>
18 #include <linux/raid_class.h>
19 #include <asm/unaligned.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_host.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/scsi_cmnd.h>
24 #include <scsi/scsi_tcq.h>
27 static struct raid_template *myrb_raid_template;
29 static void myrb_monitor(struct work_struct *work);
30 static inline void myrb_translate_devstate(void *DeviceState);
32 static inline int myrb_logical_channel(struct Scsi_Host *shost)
34 return shost->max_channel - 1;
37 static struct myrb_devstate_name_entry {
38 enum myrb_devstate state;
40 } myrb_devstate_name_list[] = {
41 { MYRB_DEVICE_DEAD, "Dead" },
42 { MYRB_DEVICE_WO, "WriteOnly" },
43 { MYRB_DEVICE_ONLINE, "Online" },
44 { MYRB_DEVICE_CRITICAL, "Critical" },
45 { MYRB_DEVICE_STANDBY, "Standby" },
46 { MYRB_DEVICE_OFFLINE, "Offline" },
49 static const char *myrb_devstate_name(enum myrb_devstate state)
51 struct myrb_devstate_name_entry *entry = myrb_devstate_name_list;
54 for (i = 0; i < ARRAY_SIZE(myrb_devstate_name_list); i++) {
55 if (entry[i].state == state)
61 static struct myrb_raidlevel_name_entry {
62 enum myrb_raidlevel level;
64 } myrb_raidlevel_name_list[] = {
65 { MYRB_RAID_LEVEL0, "RAID0" },
66 { MYRB_RAID_LEVEL1, "RAID1" },
67 { MYRB_RAID_LEVEL3, "RAID3" },
68 { MYRB_RAID_LEVEL5, "RAID5" },
69 { MYRB_RAID_LEVEL6, "RAID6" },
70 { MYRB_RAID_JBOD, "JBOD" },
73 static const char *myrb_raidlevel_name(enum myrb_raidlevel level)
75 struct myrb_raidlevel_name_entry *entry = myrb_raidlevel_name_list;
78 for (i = 0; i < ARRAY_SIZE(myrb_raidlevel_name_list); i++) {
79 if (entry[i].level == level)
86 * myrb_create_mempools - allocates auxiliary data structures
88 * Return: true on success, false otherwise.
90 static bool myrb_create_mempools(struct pci_dev *pdev, struct myrb_hba *cb)
92 size_t elem_size, elem_align;
94 elem_align = sizeof(struct myrb_sge);
95 elem_size = cb->host->sg_tablesize * elem_align;
96 cb->sg_pool = dma_pool_create("myrb_sg", &pdev->dev,
97 elem_size, elem_align, 0);
98 if (cb->sg_pool == NULL) {
99 shost_printk(KERN_ERR, cb->host,
100 "Failed to allocate SG pool\n");
104 cb->dcdb_pool = dma_pool_create("myrb_dcdb", &pdev->dev,
105 sizeof(struct myrb_dcdb),
106 sizeof(unsigned int), 0);
107 if (!cb->dcdb_pool) {
108 dma_pool_destroy(cb->sg_pool);
110 shost_printk(KERN_ERR, cb->host,
111 "Failed to allocate DCDB pool\n");
115 snprintf(cb->work_q_name, sizeof(cb->work_q_name),
116 "myrb_wq_%d", cb->host->host_no);
117 cb->work_q = create_singlethread_workqueue(cb->work_q_name);
119 dma_pool_destroy(cb->dcdb_pool);
120 cb->dcdb_pool = NULL;
121 dma_pool_destroy(cb->sg_pool);
123 shost_printk(KERN_ERR, cb->host,
124 "Failed to create workqueue\n");
129 * Initialize the Monitoring Timer.
131 INIT_DELAYED_WORK(&cb->monitor_work, myrb_monitor);
132 queue_delayed_work(cb->work_q, &cb->monitor_work, 1);
138 * myrb_destroy_mempools - tears down the memory pools for the controller
140 static void myrb_destroy_mempools(struct myrb_hba *cb)
142 cancel_delayed_work_sync(&cb->monitor_work);
143 destroy_workqueue(cb->work_q);
145 dma_pool_destroy(cb->sg_pool);
146 dma_pool_destroy(cb->dcdb_pool);
150 * myrb_reset_cmd - reset command block
152 static inline void myrb_reset_cmd(struct myrb_cmdblk *cmd_blk)
154 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
156 memset(mbox, 0, sizeof(union myrb_cmd_mbox));
161 * myrb_qcmd - queues command block for execution
163 static void myrb_qcmd(struct myrb_hba *cb, struct myrb_cmdblk *cmd_blk)
165 void __iomem *base = cb->io_base;
166 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
167 union myrb_cmd_mbox *next_mbox = cb->next_cmd_mbox;
169 cb->write_cmd_mbox(next_mbox, mbox);
170 if (cb->prev_cmd_mbox1->words[0] == 0 ||
171 cb->prev_cmd_mbox2->words[0] == 0)
172 cb->get_cmd_mbox(base);
173 cb->prev_cmd_mbox2 = cb->prev_cmd_mbox1;
174 cb->prev_cmd_mbox1 = next_mbox;
175 if (++next_mbox > cb->last_cmd_mbox)
176 next_mbox = cb->first_cmd_mbox;
177 cb->next_cmd_mbox = next_mbox;
181 * myrb_exec_cmd - executes command block and waits for completion.
183 * Return: command status
185 static unsigned short myrb_exec_cmd(struct myrb_hba *cb,
186 struct myrb_cmdblk *cmd_blk)
188 DECLARE_COMPLETION_ONSTACK(cmpl);
191 cmd_blk->completion = &cmpl;
193 spin_lock_irqsave(&cb->queue_lock, flags);
194 cb->qcmd(cb, cmd_blk);
195 spin_unlock_irqrestore(&cb->queue_lock, flags);
197 WARN_ON(in_interrupt());
198 wait_for_completion(&cmpl);
199 return cmd_blk->status;
203 * myrb_exec_type3 - executes a type 3 command and waits for completion.
205 * Return: command status
207 static unsigned short myrb_exec_type3(struct myrb_hba *cb,
208 enum myrb_cmd_opcode op, dma_addr_t addr)
210 struct myrb_cmdblk *cmd_blk = &cb->dcmd_blk;
211 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
212 unsigned short status;
214 mutex_lock(&cb->dcmd_mutex);
215 myrb_reset_cmd(cmd_blk);
216 mbox->type3.id = MYRB_DCMD_TAG;
217 mbox->type3.opcode = op;
218 mbox->type3.addr = addr;
219 status = myrb_exec_cmd(cb, cmd_blk);
220 mutex_unlock(&cb->dcmd_mutex);
225 * myrb_exec_type3D - executes a type 3D command and waits for completion.
227 * Return: command status
229 static unsigned short myrb_exec_type3D(struct myrb_hba *cb,
230 enum myrb_cmd_opcode op, struct scsi_device *sdev,
231 struct myrb_pdev_state *pdev_info)
233 struct myrb_cmdblk *cmd_blk = &cb->dcmd_blk;
234 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
235 unsigned short status;
236 dma_addr_t pdev_info_addr;
238 pdev_info_addr = dma_map_single(&cb->pdev->dev, pdev_info,
239 sizeof(struct myrb_pdev_state),
241 if (dma_mapping_error(&cb->pdev->dev, pdev_info_addr))
242 return MYRB_STATUS_SUBSYS_FAILED;
244 mutex_lock(&cb->dcmd_mutex);
245 myrb_reset_cmd(cmd_blk);
246 mbox->type3D.id = MYRB_DCMD_TAG;
247 mbox->type3D.opcode = op;
248 mbox->type3D.channel = sdev->channel;
249 mbox->type3D.target = sdev->id;
250 mbox->type3D.addr = pdev_info_addr;
251 status = myrb_exec_cmd(cb, cmd_blk);
252 mutex_unlock(&cb->dcmd_mutex);
253 dma_unmap_single(&cb->pdev->dev, pdev_info_addr,
254 sizeof(struct myrb_pdev_state), DMA_FROM_DEVICE);
255 if (status == MYRB_STATUS_SUCCESS &&
256 mbox->type3D.opcode == MYRB_CMD_GET_DEVICE_STATE_OLD)
257 myrb_translate_devstate(pdev_info);
262 static char *myrb_event_msg[] = {
263 "killed because write recovery failed",
264 "killed because of SCSI bus reset failure",
265 "killed because of double check condition",
266 "killed because it was removed",
267 "killed because of gross error on SCSI chip",
268 "killed because of bad tag returned from drive",
269 "killed because of timeout on SCSI command",
270 "killed because of reset SCSI command issued from system",
271 "killed because busy or parity error count exceeded limit",
272 "killed because of 'kill drive' command from system",
273 "killed because of selection timeout",
274 "killed due to SCSI phase sequence error",
275 "killed due to unknown status",
279 * myrb_get_event - get event log from HBA
280 * @cb: pointer to the hba structure
281 * @event: number of the event
283 * Execute a type 3E command and logs the event message
285 static void myrb_get_event(struct myrb_hba *cb, unsigned int event)
287 struct myrb_cmdblk *cmd_blk = &cb->mcmd_blk;
288 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
289 struct myrb_log_entry *ev_buf;
291 unsigned short status;
293 ev_buf = dma_alloc_coherent(&cb->pdev->dev,
294 sizeof(struct myrb_log_entry),
295 &ev_addr, GFP_KERNEL);
299 myrb_reset_cmd(cmd_blk);
300 mbox->type3E.id = MYRB_MCMD_TAG;
301 mbox->type3E.opcode = MYRB_CMD_EVENT_LOG_OPERATION;
302 mbox->type3E.optype = DAC960_V1_GetEventLogEntry;
303 mbox->type3E.opqual = 1;
304 mbox->type3E.ev_seq = event;
305 mbox->type3E.addr = ev_addr;
306 status = myrb_exec_cmd(cb, cmd_blk);
307 if (status != MYRB_STATUS_SUCCESS)
308 shost_printk(KERN_INFO, cb->host,
309 "Failed to get event log %d, status %04x\n",
312 else if (ev_buf->seq_num == event) {
313 struct scsi_sense_hdr sshdr;
315 memset(&sshdr, 0, sizeof(sshdr));
316 scsi_normalize_sense(ev_buf->sense, 32, &sshdr);
318 if (sshdr.sense_key == VENDOR_SPECIFIC &&
320 sshdr.ascq < ARRAY_SIZE(myrb_event_msg))
321 shost_printk(KERN_CRIT, cb->host,
322 "Physical drive %d:%d: %s\n",
323 ev_buf->channel, ev_buf->target,
324 myrb_event_msg[sshdr.ascq]);
326 shost_printk(KERN_CRIT, cb->host,
327 "Physical drive %d:%d: Sense: %X/%02X/%02X\n",
328 ev_buf->channel, ev_buf->target,
329 sshdr.sense_key, sshdr.asc, sshdr.ascq);
332 dma_free_coherent(&cb->pdev->dev, sizeof(struct myrb_log_entry),
337 * myrb_get_errtable - retrieves the error table from the controller
339 * Executes a type 3 command and logs the error table from the controller.
341 static void myrb_get_errtable(struct myrb_hba *cb)
343 struct myrb_cmdblk *cmd_blk = &cb->mcmd_blk;
344 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
345 unsigned short status;
346 struct myrb_error_entry old_table[MYRB_MAX_CHANNELS * MYRB_MAX_TARGETS];
348 memcpy(&old_table, cb->err_table, sizeof(old_table));
350 myrb_reset_cmd(cmd_blk);
351 mbox->type3.id = MYRB_MCMD_TAG;
352 mbox->type3.opcode = MYRB_CMD_GET_ERROR_TABLE;
353 mbox->type3.addr = cb->err_table_addr;
354 status = myrb_exec_cmd(cb, cmd_blk);
355 if (status == MYRB_STATUS_SUCCESS) {
356 struct myrb_error_entry *table = cb->err_table;
357 struct myrb_error_entry *new, *old;
358 size_t err_table_offset;
359 struct scsi_device *sdev;
361 shost_for_each_device(sdev, cb->host) {
362 if (sdev->channel >= myrb_logical_channel(cb->host))
364 err_table_offset = sdev->channel * MYRB_MAX_TARGETS
366 new = table + err_table_offset;
367 old = &old_table[err_table_offset];
368 if (new->parity_err == old->parity_err &&
369 new->soft_err == old->soft_err &&
370 new->hard_err == old->hard_err &&
371 new->misc_err == old->misc_err)
373 sdev_printk(KERN_CRIT, sdev,
374 "Errors: Parity = %d, Soft = %d, Hard = %d, Misc = %d\n",
375 new->parity_err, new->soft_err,
376 new->hard_err, new->misc_err);
382 * myrb_get_ldev_info - retrieves the logical device table from the controller
384 * Executes a type 3 command and updates the logical device table.
386 * Return: command status
388 static unsigned short myrb_get_ldev_info(struct myrb_hba *cb)
390 unsigned short status;
391 int ldev_num, ldev_cnt = cb->enquiry->ldev_count;
392 struct Scsi_Host *shost = cb->host;
394 status = myrb_exec_type3(cb, MYRB_CMD_GET_LDEV_INFO,
396 if (status != MYRB_STATUS_SUCCESS)
399 for (ldev_num = 0; ldev_num < ldev_cnt; ldev_num++) {
400 struct myrb_ldev_info *old = NULL;
401 struct myrb_ldev_info *new = cb->ldev_info_buf + ldev_num;
402 struct scsi_device *sdev;
404 sdev = scsi_device_lookup(shost, myrb_logical_channel(shost),
407 if (new->state == MYRB_DEVICE_OFFLINE)
409 shost_printk(KERN_INFO, shost,
410 "Adding Logical Drive %d in state %s\n",
411 ldev_num, myrb_devstate_name(new->state));
412 scsi_add_device(shost, myrb_logical_channel(shost),
416 old = sdev->hostdata;
417 if (new->state != old->state)
418 shost_printk(KERN_INFO, shost,
419 "Logical Drive %d is now %s\n",
420 ldev_num, myrb_devstate_name(new->state));
421 if (new->wb_enabled != old->wb_enabled)
422 sdev_printk(KERN_INFO, sdev,
423 "Logical Drive is now WRITE %s\n",
424 (new->wb_enabled ? "BACK" : "THRU"));
425 memcpy(old, new, sizeof(*new));
426 scsi_device_put(sdev);
432 * myrb_get_rbld_progress - get rebuild progress information
434 * Executes a type 3 command and returns the rebuild progress
437 * Return: command status
439 static unsigned short myrb_get_rbld_progress(struct myrb_hba *cb,
440 struct myrb_rbld_progress *rbld)
442 struct myrb_cmdblk *cmd_blk = &cb->mcmd_blk;
443 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
444 struct myrb_rbld_progress *rbld_buf;
445 dma_addr_t rbld_addr;
446 unsigned short status;
448 rbld_buf = dma_alloc_coherent(&cb->pdev->dev,
449 sizeof(struct myrb_rbld_progress),
450 &rbld_addr, GFP_KERNEL);
452 return MYRB_STATUS_RBLD_NOT_CHECKED;
454 myrb_reset_cmd(cmd_blk);
455 mbox->type3.id = MYRB_MCMD_TAG;
456 mbox->type3.opcode = MYRB_CMD_GET_REBUILD_PROGRESS;
457 mbox->type3.addr = rbld_addr;
458 status = myrb_exec_cmd(cb, cmd_blk);
460 memcpy(rbld, rbld_buf, sizeof(struct myrb_rbld_progress));
461 dma_free_coherent(&cb->pdev->dev, sizeof(struct myrb_rbld_progress),
462 rbld_buf, rbld_addr);
467 * myrb_update_rbld_progress - updates the rebuild status
469 * Updates the rebuild status for the attached logical devices.
472 static void myrb_update_rbld_progress(struct myrb_hba *cb)
474 struct myrb_rbld_progress rbld_buf;
475 unsigned short status;
477 status = myrb_get_rbld_progress(cb, &rbld_buf);
478 if (status == MYRB_NO_STDBY_RBLD_OR_CHECK_IN_PROGRESS &&
479 cb->last_rbld_status == MYRB_STATUS_SUCCESS)
480 status = MYRB_STATUS_RBLD_SUCCESS;
481 if (status != MYRB_NO_STDBY_RBLD_OR_CHECK_IN_PROGRESS) {
482 unsigned int blocks_done =
483 rbld_buf.ldev_size - rbld_buf.blocks_left;
484 struct scsi_device *sdev;
486 sdev = scsi_device_lookup(cb->host,
487 myrb_logical_channel(cb->host),
488 rbld_buf.ldev_num, 0);
493 case MYRB_STATUS_SUCCESS:
494 sdev_printk(KERN_INFO, sdev,
495 "Rebuild in Progress, %d%% completed\n",
496 (100 * (blocks_done >> 7))
497 / (rbld_buf.ldev_size >> 7));
499 case MYRB_STATUS_RBLD_FAILED_LDEV_FAILURE:
500 sdev_printk(KERN_INFO, sdev,
501 "Rebuild Failed due to Logical Drive Failure\n");
503 case MYRB_STATUS_RBLD_FAILED_BADBLOCKS:
504 sdev_printk(KERN_INFO, sdev,
505 "Rebuild Failed due to Bad Blocks on Other Drives\n");
507 case MYRB_STATUS_RBLD_FAILED_NEW_DRIVE_FAILED:
508 sdev_printk(KERN_INFO, sdev,
509 "Rebuild Failed due to Failure of Drive Being Rebuilt\n");
511 case MYRB_STATUS_RBLD_SUCCESS:
512 sdev_printk(KERN_INFO, sdev,
513 "Rebuild Completed Successfully\n");
515 case MYRB_STATUS_RBLD_SUCCESS_TERMINATED:
516 sdev_printk(KERN_INFO, sdev,
517 "Rebuild Successfully Terminated\n");
522 scsi_device_put(sdev);
524 cb->last_rbld_status = status;
528 * myrb_get_cc_progress - retrieve the rebuild status
530 * Execute a type 3 Command and fetch the rebuild / consistency check
533 static void myrb_get_cc_progress(struct myrb_hba *cb)
535 struct myrb_cmdblk *cmd_blk = &cb->mcmd_blk;
536 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
537 struct myrb_rbld_progress *rbld_buf;
538 dma_addr_t rbld_addr;
539 unsigned short status;
541 rbld_buf = dma_alloc_coherent(&cb->pdev->dev,
542 sizeof(struct myrb_rbld_progress),
543 &rbld_addr, GFP_KERNEL);
545 cb->need_cc_status = true;
548 myrb_reset_cmd(cmd_blk);
549 mbox->type3.id = MYRB_MCMD_TAG;
550 mbox->type3.opcode = MYRB_CMD_REBUILD_STAT;
551 mbox->type3.addr = rbld_addr;
552 status = myrb_exec_cmd(cb, cmd_blk);
553 if (status == MYRB_STATUS_SUCCESS) {
554 unsigned int ldev_num = rbld_buf->ldev_num;
555 unsigned int ldev_size = rbld_buf->ldev_size;
556 unsigned int blocks_done =
557 ldev_size - rbld_buf->blocks_left;
558 struct scsi_device *sdev;
560 sdev = scsi_device_lookup(cb->host,
561 myrb_logical_channel(cb->host),
564 sdev_printk(KERN_INFO, sdev,
565 "Consistency Check in Progress: %d%% completed\n",
566 (100 * (blocks_done >> 7))
568 scsi_device_put(sdev);
571 dma_free_coherent(&cb->pdev->dev, sizeof(struct myrb_rbld_progress),
572 rbld_buf, rbld_addr);
576 * myrb_bgi_control - updates background initialisation status
578 * Executes a type 3B command and updates the background initialisation status
580 static void myrb_bgi_control(struct myrb_hba *cb)
582 struct myrb_cmdblk *cmd_blk = &cb->mcmd_blk;
583 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
584 struct myrb_bgi_status *bgi, *last_bgi;
586 struct scsi_device *sdev = NULL;
587 unsigned short status;
589 bgi = dma_alloc_coherent(&cb->pdev->dev, sizeof(struct myrb_bgi_status),
590 &bgi_addr, GFP_KERNEL);
592 shost_printk(KERN_ERR, cb->host,
593 "Failed to allocate bgi memory\n");
596 myrb_reset_cmd(cmd_blk);
597 mbox->type3B.id = MYRB_DCMD_TAG;
598 mbox->type3B.opcode = MYRB_CMD_BGI_CONTROL;
599 mbox->type3B.optype = 0x20;
600 mbox->type3B.addr = bgi_addr;
601 status = myrb_exec_cmd(cb, cmd_blk);
602 last_bgi = &cb->bgi_status;
603 sdev = scsi_device_lookup(cb->host,
604 myrb_logical_channel(cb->host),
607 case MYRB_STATUS_SUCCESS:
608 switch (bgi->status) {
609 case MYRB_BGI_INVALID:
611 case MYRB_BGI_STARTED:
614 sdev_printk(KERN_INFO, sdev,
615 "Background Initialization Started\n");
617 case MYRB_BGI_INPROGRESS:
620 if (bgi->blocks_done == last_bgi->blocks_done &&
621 bgi->ldev_num == last_bgi->ldev_num)
623 sdev_printk(KERN_INFO, sdev,
624 "Background Initialization in Progress: %d%% completed\n",
625 (100 * (bgi->blocks_done >> 7))
626 / (bgi->ldev_size >> 7));
628 case MYRB_BGI_SUSPENDED:
631 sdev_printk(KERN_INFO, sdev,
632 "Background Initialization Suspended\n");
634 case MYRB_BGI_CANCELLED:
637 sdev_printk(KERN_INFO, sdev,
638 "Background Initialization Cancelled\n");
641 memcpy(&cb->bgi_status, bgi, sizeof(struct myrb_bgi_status));
643 case MYRB_STATUS_BGI_SUCCESS:
644 if (sdev && cb->bgi_status.status == MYRB_BGI_INPROGRESS)
645 sdev_printk(KERN_INFO, sdev,
646 "Background Initialization Completed Successfully\n");
647 cb->bgi_status.status = MYRB_BGI_INVALID;
649 case MYRB_STATUS_BGI_ABORTED:
650 if (sdev && cb->bgi_status.status == MYRB_BGI_INPROGRESS)
651 sdev_printk(KERN_INFO, sdev,
652 "Background Initialization Aborted\n");
654 case MYRB_STATUS_NO_BGI_INPROGRESS:
655 cb->bgi_status.status = MYRB_BGI_INVALID;
659 scsi_device_put(sdev);
660 dma_free_coherent(&cb->pdev->dev, sizeof(struct myrb_bgi_status),
665 * myrb_hba_enquiry - updates the controller status
667 * Executes a DAC_V1_Enquiry command and updates the controller status.
669 * Return: command status
671 static unsigned short myrb_hba_enquiry(struct myrb_hba *cb)
673 struct myrb_enquiry old, *new;
674 unsigned short status;
676 memcpy(&old, cb->enquiry, sizeof(struct myrb_enquiry));
678 status = myrb_exec_type3(cb, MYRB_CMD_ENQUIRY, cb->enquiry_addr);
679 if (status != MYRB_STATUS_SUCCESS)
683 if (new->ldev_count > old.ldev_count) {
684 int ldev_num = old.ldev_count - 1;
686 while (++ldev_num < new->ldev_count)
687 shost_printk(KERN_CRIT, cb->host,
688 "Logical Drive %d Now Exists\n",
691 if (new->ldev_count < old.ldev_count) {
692 int ldev_num = new->ldev_count - 1;
694 while (++ldev_num < old.ldev_count)
695 shost_printk(KERN_CRIT, cb->host,
696 "Logical Drive %d No Longer Exists\n",
699 if (new->status.deferred != old.status.deferred)
700 shost_printk(KERN_CRIT, cb->host,
701 "Deferred Write Error Flag is now %s\n",
702 (new->status.deferred ? "TRUE" : "FALSE"));
703 if (new->ev_seq != old.ev_seq) {
704 cb->new_ev_seq = new->ev_seq;
705 cb->need_err_info = true;
706 shost_printk(KERN_INFO, cb->host,
707 "Event log %d/%d (%d/%d) available\n",
708 cb->old_ev_seq, cb->new_ev_seq,
709 old.ev_seq, new->ev_seq);
711 if ((new->ldev_critical > 0 &&
712 new->ldev_critical != old.ldev_critical) ||
713 (new->ldev_offline > 0 &&
714 new->ldev_offline != old.ldev_offline) ||
715 (new->ldev_count != old.ldev_count)) {
716 shost_printk(KERN_INFO, cb->host,
717 "Logical drive count changed (%d/%d/%d)\n",
721 cb->need_ldev_info = true;
723 if (new->pdev_dead > 0 ||
724 new->pdev_dead != old.pdev_dead ||
725 time_after_eq(jiffies, cb->secondary_monitor_time
726 + MYRB_SECONDARY_MONITOR_INTERVAL)) {
727 cb->need_bgi_status = cb->bgi_status_supported;
728 cb->secondary_monitor_time = jiffies;
730 if (new->rbld == MYRB_STDBY_RBLD_IN_PROGRESS ||
731 new->rbld == MYRB_BG_RBLD_IN_PROGRESS ||
732 old.rbld == MYRB_STDBY_RBLD_IN_PROGRESS ||
733 old.rbld == MYRB_BG_RBLD_IN_PROGRESS) {
734 cb->need_rbld = true;
735 cb->rbld_first = (new->ldev_critical < old.ldev_critical);
737 if (old.rbld == MYRB_BG_CHECK_IN_PROGRESS)
739 case MYRB_NO_STDBY_RBLD_OR_CHECK_IN_PROGRESS:
740 shost_printk(KERN_INFO, cb->host,
741 "Consistency Check Completed Successfully\n");
743 case MYRB_STDBY_RBLD_IN_PROGRESS:
744 case MYRB_BG_RBLD_IN_PROGRESS:
746 case MYRB_BG_CHECK_IN_PROGRESS:
747 cb->need_cc_status = true;
749 case MYRB_STDBY_RBLD_COMPLETED_WITH_ERROR:
750 shost_printk(KERN_INFO, cb->host,
751 "Consistency Check Completed with Error\n");
753 case MYRB_BG_RBLD_OR_CHECK_FAILED_DRIVE_FAILED:
754 shost_printk(KERN_INFO, cb->host,
755 "Consistency Check Failed - Physical Device Failed\n");
757 case MYRB_BG_RBLD_OR_CHECK_FAILED_LDEV_FAILED:
758 shost_printk(KERN_INFO, cb->host,
759 "Consistency Check Failed - Logical Drive Failed\n");
761 case MYRB_BG_RBLD_OR_CHECK_FAILED_OTHER:
762 shost_printk(KERN_INFO, cb->host,
763 "Consistency Check Failed - Other Causes\n");
765 case MYRB_BG_RBLD_OR_CHECK_SUCCESS_TERMINATED:
766 shost_printk(KERN_INFO, cb->host,
767 "Consistency Check Successfully Terminated\n");
770 else if (new->rbld == MYRB_BG_CHECK_IN_PROGRESS)
771 cb->need_cc_status = true;
773 return MYRB_STATUS_SUCCESS;
777 * myrb_set_pdev_state - sets the device state for a physical device
779 * Return: command status
781 static unsigned short myrb_set_pdev_state(struct myrb_hba *cb,
782 struct scsi_device *sdev, enum myrb_devstate state)
784 struct myrb_cmdblk *cmd_blk = &cb->dcmd_blk;
785 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
786 unsigned short status;
788 mutex_lock(&cb->dcmd_mutex);
789 mbox->type3D.opcode = MYRB_CMD_START_DEVICE;
790 mbox->type3D.id = MYRB_DCMD_TAG;
791 mbox->type3D.channel = sdev->channel;
792 mbox->type3D.target = sdev->id;
793 mbox->type3D.state = state & 0x1F;
794 status = myrb_exec_cmd(cb, cmd_blk);
795 mutex_unlock(&cb->dcmd_mutex);
801 * myrb_enable_mmio - enables the Memory Mailbox Interface
803 * PD and P controller types have no memory mailbox, but still need the
804 * other dma mapped memory.
806 * Return: true on success, false otherwise.
808 static bool myrb_enable_mmio(struct myrb_hba *cb, mbox_mmio_init_t mmio_init_fn)
810 void __iomem *base = cb->io_base;
811 struct pci_dev *pdev = cb->pdev;
812 size_t err_table_size;
813 size_t ldev_info_size;
814 union myrb_cmd_mbox *cmd_mbox_mem;
815 struct myrb_stat_mbox *stat_mbox_mem;
816 union myrb_cmd_mbox mbox;
817 unsigned short status;
819 memset(&mbox, 0, sizeof(union myrb_cmd_mbox));
821 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
822 dev_err(&pdev->dev, "DMA mask out of range\n");
826 cb->enquiry = dma_alloc_coherent(&pdev->dev,
827 sizeof(struct myrb_enquiry),
828 &cb->enquiry_addr, GFP_KERNEL);
832 err_table_size = sizeof(struct myrb_error_entry) *
833 MYRB_MAX_CHANNELS * MYRB_MAX_TARGETS;
834 cb->err_table = dma_alloc_coherent(&pdev->dev, err_table_size,
835 &cb->err_table_addr, GFP_KERNEL);
839 ldev_info_size = sizeof(struct myrb_ldev_info) * MYRB_MAX_LDEVS;
840 cb->ldev_info_buf = dma_alloc_coherent(&pdev->dev, ldev_info_size,
841 &cb->ldev_info_addr, GFP_KERNEL);
842 if (!cb->ldev_info_buf)
846 * Skip mailbox initialisation for PD and P Controllers
851 /* These are the base addresses for the command memory mailbox array */
852 cb->cmd_mbox_size = MYRB_CMD_MBOX_COUNT * sizeof(union myrb_cmd_mbox);
853 cb->first_cmd_mbox = dma_alloc_coherent(&pdev->dev,
857 if (!cb->first_cmd_mbox)
860 cmd_mbox_mem = cb->first_cmd_mbox;
861 cmd_mbox_mem += MYRB_CMD_MBOX_COUNT - 1;
862 cb->last_cmd_mbox = cmd_mbox_mem;
863 cb->next_cmd_mbox = cb->first_cmd_mbox;
864 cb->prev_cmd_mbox1 = cb->last_cmd_mbox;
865 cb->prev_cmd_mbox2 = cb->last_cmd_mbox - 1;
867 /* These are the base addresses for the status memory mailbox array */
868 cb->stat_mbox_size = MYRB_STAT_MBOX_COUNT *
869 sizeof(struct myrb_stat_mbox);
870 cb->first_stat_mbox = dma_alloc_coherent(&pdev->dev,
874 if (!cb->first_stat_mbox)
877 stat_mbox_mem = cb->first_stat_mbox;
878 stat_mbox_mem += MYRB_STAT_MBOX_COUNT - 1;
879 cb->last_stat_mbox = stat_mbox_mem;
880 cb->next_stat_mbox = cb->first_stat_mbox;
882 /* Enable the Memory Mailbox Interface. */
883 cb->dual_mode_interface = true;
884 mbox.typeX.opcode = 0x2B;
886 mbox.typeX.opcode2 = 0x14;
887 mbox.typeX.cmd_mbox_addr = cb->cmd_mbox_addr;
888 mbox.typeX.stat_mbox_addr = cb->stat_mbox_addr;
890 status = mmio_init_fn(pdev, base, &mbox);
891 if (status != MYRB_STATUS_SUCCESS) {
892 cb->dual_mode_interface = false;
893 mbox.typeX.opcode2 = 0x10;
894 status = mmio_init_fn(pdev, base, &mbox);
895 if (status != MYRB_STATUS_SUCCESS) {
897 "Failed to enable mailbox, statux %02X\n",
906 * myrb_get_hba_config - reads the configuration information
908 * Reads the configuration information from the controller and
909 * initializes the controller structure.
911 * Return: 0 on success, errno otherwise
913 static int myrb_get_hba_config(struct myrb_hba *cb)
915 struct myrb_enquiry2 *enquiry2;
916 dma_addr_t enquiry2_addr;
917 struct myrb_config2 *config2;
918 dma_addr_t config2_addr;
919 struct Scsi_Host *shost = cb->host;
920 struct pci_dev *pdev = cb->pdev;
921 int pchan_max = 0, pchan_cur = 0;
922 unsigned short status;
923 int ret = -ENODEV, memsize = 0;
925 enquiry2 = dma_alloc_coherent(&pdev->dev, sizeof(struct myrb_enquiry2),
926 &enquiry2_addr, GFP_KERNEL);
928 shost_printk(KERN_ERR, cb->host,
929 "Failed to allocate V1 enquiry2 memory\n");
932 config2 = dma_alloc_coherent(&pdev->dev, sizeof(struct myrb_config2),
933 &config2_addr, GFP_KERNEL);
935 shost_printk(KERN_ERR, cb->host,
936 "Failed to allocate V1 config2 memory\n");
937 dma_free_coherent(&pdev->dev, sizeof(struct myrb_enquiry2),
938 enquiry2, enquiry2_addr);
941 mutex_lock(&cb->dma_mutex);
942 status = myrb_hba_enquiry(cb);
943 mutex_unlock(&cb->dma_mutex);
944 if (status != MYRB_STATUS_SUCCESS) {
945 shost_printk(KERN_WARNING, cb->host,
946 "Failed it issue V1 Enquiry\n");
950 status = myrb_exec_type3(cb, MYRB_CMD_ENQUIRY2, enquiry2_addr);
951 if (status != MYRB_STATUS_SUCCESS) {
952 shost_printk(KERN_WARNING, cb->host,
953 "Failed to issue V1 Enquiry2\n");
957 status = myrb_exec_type3(cb, MYRB_CMD_READ_CONFIG2, config2_addr);
958 if (status != MYRB_STATUS_SUCCESS) {
959 shost_printk(KERN_WARNING, cb->host,
960 "Failed to issue ReadConfig2\n");
964 status = myrb_get_ldev_info(cb);
965 if (status != MYRB_STATUS_SUCCESS) {
966 shost_printk(KERN_WARNING, cb->host,
967 "Failed to get logical drive information\n");
972 * Initialize the Controller Model Name and Full Model Name fields.
974 switch (enquiry2->hw.sub_model) {
975 case DAC960_V1_P_PD_PU:
976 if (enquiry2->scsi_cap.bus_speed == MYRB_SCSI_SPEED_ULTRA)
977 strcpy(cb->model_name, "DAC960PU");
979 strcpy(cb->model_name, "DAC960PD");
982 strcpy(cb->model_name, "DAC960PL");
985 strcpy(cb->model_name, "DAC960PG");
988 strcpy(cb->model_name, "DAC960PJ");
991 strcpy(cb->model_name, "DAC960PR");
994 strcpy(cb->model_name, "DAC960PT");
997 strcpy(cb->model_name, "DAC960PTL0");
1000 strcpy(cb->model_name, "DAC960PRL");
1002 case DAC960_V1_PTL1:
1003 strcpy(cb->model_name, "DAC960PTL1");
1005 case DAC960_V1_1164P:
1006 strcpy(cb->model_name, "eXtremeRAID 1100");
1009 shost_printk(KERN_WARNING, cb->host,
1010 "Unknown Model %X\n",
1011 enquiry2->hw.sub_model);
1015 * Initialize the Controller Firmware Version field and verify that it
1016 * is a supported firmware version.
1017 * The supported firmware versions are:
1019 * DAC1164P 5.06 and above
1020 * DAC960PTL/PRL/PJ/PG 4.06 and above
1021 * DAC960PU/PD/PL 3.51 and above
1022 * DAC960PU/PD/PL/P 2.73 and above
1024 #if defined(CONFIG_ALPHA)
1026 * DEC Alpha machines were often equipped with DAC960 cards that were
1027 * OEMed from Mylex, and had their own custom firmware. Version 2.70,
1028 * the last custom FW revision to be released by DEC for these older
1029 * controllers, appears to work quite well with this driver.
1031 * Cards tested successfully were several versions each of the PD and
1032 * PU, called by DEC the KZPSC and KZPAC, respectively, and having
1033 * the Manufacturer Numbers (from Mylex), usually on a sticker on the
1034 * back of the board, of:
1036 * KZPSC: D040347 (1-channel) or D040348 (2-channel)
1037 * or D040349 (3-channel)
1038 * KZPAC: D040395 (1-channel) or D040396 (2-channel)
1039 * or D040397 (3-channel)
1041 # define FIRMWARE_27X "2.70"
1043 # define FIRMWARE_27X "2.73"
1046 if (enquiry2->fw.major_version == 0) {
1047 enquiry2->fw.major_version = cb->enquiry->fw_major_version;
1048 enquiry2->fw.minor_version = cb->enquiry->fw_minor_version;
1049 enquiry2->fw.firmware_type = '0';
1050 enquiry2->fw.turn_id = 0;
1052 snprintf(cb->fw_version, sizeof(cb->fw_version),
1054 enquiry2->fw.major_version,
1055 enquiry2->fw.minor_version,
1056 enquiry2->fw.firmware_type,
1057 enquiry2->fw.turn_id);
1058 if (!((enquiry2->fw.major_version == 5 &&
1059 enquiry2->fw.minor_version >= 6) ||
1060 (enquiry2->fw.major_version == 4 &&
1061 enquiry2->fw.minor_version >= 6) ||
1062 (enquiry2->fw.major_version == 3 &&
1063 enquiry2->fw.minor_version >= 51) ||
1064 (enquiry2->fw.major_version == 2 &&
1065 strcmp(cb->fw_version, FIRMWARE_27X) >= 0))) {
1066 shost_printk(KERN_WARNING, cb->host,
1067 "Firmware Version '%s' unsupported\n",
1072 * Initialize the Channels, Targets, Memory Size, and SAF-TE
1073 * Enclosure Management Enabled fields.
1075 switch (enquiry2->hw.model) {
1076 case MYRB_5_CHANNEL_BOARD:
1079 case MYRB_3_CHANNEL_BOARD:
1080 case MYRB_3_CHANNEL_ASIC_DAC:
1083 case MYRB_2_CHANNEL_BOARD:
1087 pchan_max = enquiry2->cfg_chan;
1090 pchan_cur = enquiry2->cur_chan;
1091 if (enquiry2->scsi_cap.bus_width == MYRB_WIDTH_WIDE_32BIT)
1093 else if (enquiry2->scsi_cap.bus_width == MYRB_WIDTH_WIDE_16BIT)
1097 cb->ldev_block_size = enquiry2->ldev_block_size;
1098 shost->max_channel = pchan_cur;
1099 shost->max_id = enquiry2->max_targets;
1100 memsize = enquiry2->mem_size >> 20;
1101 cb->safte_enabled = (enquiry2->fault_mgmt == MYRB_FAULT_SAFTE);
1103 * Initialize the Controller Queue Depth, Driver Queue Depth,
1104 * Logical Drive Count, Maximum Blocks per Command, Controller
1105 * Scatter/Gather Limit, and Driver Scatter/Gather Limit.
1106 * The Driver Queue Depth must be at most one less than the
1107 * Controller Queue Depth to allow for an automatic drive
1108 * rebuild operation.
1110 shost->can_queue = cb->enquiry->max_tcq;
1111 if (shost->can_queue < 3)
1112 shost->can_queue = enquiry2->max_cmds;
1113 if (shost->can_queue < 3)
1114 /* Play safe and disable TCQ */
1115 shost->can_queue = 1;
1117 if (shost->can_queue > MYRB_CMD_MBOX_COUNT - 2)
1118 shost->can_queue = MYRB_CMD_MBOX_COUNT - 2;
1119 shost->max_sectors = enquiry2->max_sectors;
1120 shost->sg_tablesize = enquiry2->max_sge;
1121 if (shost->sg_tablesize > MYRB_SCATTER_GATHER_LIMIT)
1122 shost->sg_tablesize = MYRB_SCATTER_GATHER_LIMIT;
1124 * Initialize the Stripe Size, Segment Size, and Geometry Translation.
1126 cb->stripe_size = config2->blocks_per_stripe * config2->block_factor
1127 >> (10 - MYRB_BLKSIZE_BITS);
1128 cb->segment_size = config2->blocks_per_cacheline * config2->block_factor
1129 >> (10 - MYRB_BLKSIZE_BITS);
1130 /* Assume 255/63 translation */
1131 cb->ldev_geom_heads = 255;
1132 cb->ldev_geom_sectors = 63;
1133 if (config2->drive_geometry) {
1134 cb->ldev_geom_heads = 128;
1135 cb->ldev_geom_sectors = 32;
1139 * Initialize the Background Initialization Status.
1141 if ((cb->fw_version[0] == '4' &&
1142 strcmp(cb->fw_version, "4.08") >= 0) ||
1143 (cb->fw_version[0] == '5' &&
1144 strcmp(cb->fw_version, "5.08") >= 0)) {
1145 cb->bgi_status_supported = true;
1146 myrb_bgi_control(cb);
1148 cb->last_rbld_status = MYRB_NO_STDBY_RBLD_OR_CHECK_IN_PROGRESS;
1152 shost_printk(KERN_INFO, cb->host,
1153 "Configuring %s PCI RAID Controller\n", cb->model_name);
1154 shost_printk(KERN_INFO, cb->host,
1155 " Firmware Version: %s, Memory Size: %dMB\n",
1156 cb->fw_version, memsize);
1157 if (cb->io_addr == 0)
1158 shost_printk(KERN_INFO, cb->host,
1159 " I/O Address: n/a, PCI Address: 0x%lX, IRQ Channel: %d\n",
1160 (unsigned long)cb->pci_addr, cb->irq);
1162 shost_printk(KERN_INFO, cb->host,
1163 " I/O Address: 0x%lX, PCI Address: 0x%lX, IRQ Channel: %d\n",
1164 (unsigned long)cb->io_addr, (unsigned long)cb->pci_addr,
1166 shost_printk(KERN_INFO, cb->host,
1167 " Controller Queue Depth: %d, Maximum Blocks per Command: %d\n",
1168 cb->host->can_queue, cb->host->max_sectors);
1169 shost_printk(KERN_INFO, cb->host,
1170 " Driver Queue Depth: %d, Scatter/Gather Limit: %d of %d Segments\n",
1171 cb->host->can_queue, cb->host->sg_tablesize,
1172 MYRB_SCATTER_GATHER_LIMIT);
1173 shost_printk(KERN_INFO, cb->host,
1174 " Stripe Size: %dKB, Segment Size: %dKB, BIOS Geometry: %d/%d%s\n",
1175 cb->stripe_size, cb->segment_size,
1176 cb->ldev_geom_heads, cb->ldev_geom_sectors,
1178 " SAF-TE Enclosure Management Enabled" : "");
1179 shost_printk(KERN_INFO, cb->host,
1180 " Physical: %d/%d channels %d/%d/%d devices\n",
1181 pchan_cur, pchan_max, 0, cb->enquiry->pdev_dead,
1184 shost_printk(KERN_INFO, cb->host,
1185 " Logical: 1/1 channels, %d/%d disks\n",
1186 cb->enquiry->ldev_count, MYRB_MAX_LDEVS);
1189 dma_free_coherent(&pdev->dev, sizeof(struct myrb_enquiry2),
1190 enquiry2, enquiry2_addr);
1191 dma_free_coherent(&pdev->dev, sizeof(struct myrb_config2),
1192 config2, config2_addr);
1198 * myrb_unmap - unmaps controller structures
1200 static void myrb_unmap(struct myrb_hba *cb)
1202 if (cb->ldev_info_buf) {
1203 size_t ldev_info_size = sizeof(struct myrb_ldev_info) *
1205 dma_free_coherent(&cb->pdev->dev, ldev_info_size,
1206 cb->ldev_info_buf, cb->ldev_info_addr);
1207 cb->ldev_info_buf = NULL;
1209 if (cb->err_table) {
1210 size_t err_table_size = sizeof(struct myrb_error_entry) *
1211 MYRB_MAX_CHANNELS * MYRB_MAX_TARGETS;
1212 dma_free_coherent(&cb->pdev->dev, err_table_size,
1213 cb->err_table, cb->err_table_addr);
1214 cb->err_table = NULL;
1217 dma_free_coherent(&cb->pdev->dev, sizeof(struct myrb_enquiry),
1218 cb->enquiry, cb->enquiry_addr);
1221 if (cb->first_stat_mbox) {
1222 dma_free_coherent(&cb->pdev->dev, cb->stat_mbox_size,
1223 cb->first_stat_mbox, cb->stat_mbox_addr);
1224 cb->first_stat_mbox = NULL;
1226 if (cb->first_cmd_mbox) {
1227 dma_free_coherent(&cb->pdev->dev, cb->cmd_mbox_size,
1228 cb->first_cmd_mbox, cb->cmd_mbox_addr);
1229 cb->first_cmd_mbox = NULL;
1234 * myrb_cleanup - cleanup controller structures
1236 static void myrb_cleanup(struct myrb_hba *cb)
1238 struct pci_dev *pdev = cb->pdev;
1240 /* Free the memory mailbox, status, and related structures */
1243 if (cb->mmio_base) {
1244 if (cb->disable_intr)
1245 cb->disable_intr(cb->io_base);
1246 iounmap(cb->mmio_base);
1249 free_irq(cb->irq, cb);
1251 release_region(cb->io_addr, 0x80);
1252 pci_set_drvdata(pdev, NULL);
1253 pci_disable_device(pdev);
1254 scsi_host_put(cb->host);
1257 static int myrb_host_reset(struct scsi_cmnd *scmd)
1259 struct Scsi_Host *shost = scmd->device->host;
1260 struct myrb_hba *cb = shost_priv(shost);
1262 cb->reset(cb->io_base);
1266 static int myrb_pthru_queuecommand(struct Scsi_Host *shost,
1267 struct scsi_cmnd *scmd)
1269 struct myrb_hba *cb = shost_priv(shost);
1270 struct myrb_cmdblk *cmd_blk = scsi_cmd_priv(scmd);
1271 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
1272 struct myrb_dcdb *dcdb;
1273 dma_addr_t dcdb_addr;
1274 struct scsi_device *sdev = scmd->device;
1275 struct scatterlist *sgl;
1276 unsigned long flags;
1279 myrb_reset_cmd(cmd_blk);
1280 dcdb = dma_pool_alloc(cb->dcdb_pool, GFP_ATOMIC, &dcdb_addr);
1282 return SCSI_MLQUEUE_HOST_BUSY;
1283 nsge = scsi_dma_map(scmd);
1285 dma_pool_free(cb->dcdb_pool, dcdb, dcdb_addr);
1286 scmd->result = (DID_ERROR << 16);
1287 scmd->scsi_done(scmd);
1291 mbox->type3.opcode = MYRB_CMD_DCDB;
1292 mbox->type3.id = scmd->request->tag + 3;
1293 mbox->type3.addr = dcdb_addr;
1294 dcdb->channel = sdev->channel;
1295 dcdb->target = sdev->id;
1296 switch (scmd->sc_data_direction) {
1298 dcdb->data_xfer = MYRB_DCDB_XFER_NONE;
1301 dcdb->data_xfer = MYRB_DCDB_XFER_SYSTEM_TO_DEVICE;
1303 case DMA_FROM_DEVICE:
1304 dcdb->data_xfer = MYRB_DCDB_XFER_DEVICE_TO_SYSTEM;
1307 dcdb->data_xfer = MYRB_DCDB_XFER_ILLEGAL;
1310 dcdb->early_status = false;
1311 if (scmd->request->timeout <= 10)
1312 dcdb->timeout = MYRB_DCDB_TMO_10_SECS;
1313 else if (scmd->request->timeout <= 60)
1314 dcdb->timeout = MYRB_DCDB_TMO_60_SECS;
1315 else if (scmd->request->timeout <= 600)
1316 dcdb->timeout = MYRB_DCDB_TMO_10_MINS;
1318 dcdb->timeout = MYRB_DCDB_TMO_24_HRS;
1319 dcdb->no_autosense = false;
1320 dcdb->allow_disconnect = true;
1321 sgl = scsi_sglist(scmd);
1322 dcdb->dma_addr = sg_dma_address(sgl);
1323 if (sg_dma_len(sgl) > USHRT_MAX) {
1324 dcdb->xfer_len_lo = sg_dma_len(sgl) & 0xffff;
1325 dcdb->xfer_len_hi4 = sg_dma_len(sgl) >> 16;
1327 dcdb->xfer_len_lo = sg_dma_len(sgl);
1328 dcdb->xfer_len_hi4 = 0;
1330 dcdb->cdb_len = scmd->cmd_len;
1331 dcdb->sense_len = sizeof(dcdb->sense);
1332 memcpy(&dcdb->cdb, scmd->cmnd, scmd->cmd_len);
1334 spin_lock_irqsave(&cb->queue_lock, flags);
1335 cb->qcmd(cb, cmd_blk);
1336 spin_unlock_irqrestore(&cb->queue_lock, flags);
1340 static void myrb_inquiry(struct myrb_hba *cb,
1341 struct scsi_cmnd *scmd)
1343 unsigned char inq[36] = {
1344 0x00, 0x00, 0x03, 0x02, 0x20, 0x00, 0x01, 0x00,
1345 0x4d, 0x59, 0x4c, 0x45, 0x58, 0x20, 0x20, 0x20,
1346 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1347 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1348 0x20, 0x20, 0x20, 0x20,
1351 if (cb->bus_width > 16)
1353 if (cb->bus_width > 8)
1355 memcpy(&inq[16], cb->model_name, 16);
1356 memcpy(&inq[32], cb->fw_version, 1);
1357 memcpy(&inq[33], &cb->fw_version[2], 2);
1358 memcpy(&inq[35], &cb->fw_version[7], 1);
1360 scsi_sg_copy_from_buffer(scmd, (void *)inq, 36);
1364 myrb_mode_sense(struct myrb_hba *cb, struct scsi_cmnd *scmd,
1365 struct myrb_ldev_info *ldev_info)
1367 unsigned char modes[32], *mode_pg;
1371 dbd = (scmd->cmnd[1] & 0x08) == 0x08;
1374 mode_pg = &modes[4];
1377 mode_pg = &modes[12];
1379 memset(modes, 0, sizeof(modes));
1380 modes[0] = mode_len - 1;
1382 unsigned char *block_desc = &modes[4];
1385 put_unaligned_be32(ldev_info->size, &block_desc[0]);
1386 put_unaligned_be32(cb->ldev_block_size, &block_desc[5]);
1390 if (ldev_info->wb_enabled)
1392 if (cb->segment_size) {
1394 put_unaligned_be16(cb->segment_size, &mode_pg[14]);
1397 scsi_sg_copy_from_buffer(scmd, modes, mode_len);
1400 static void myrb_request_sense(struct myrb_hba *cb,
1401 struct scsi_cmnd *scmd)
1403 scsi_build_sense_buffer(0, scmd->sense_buffer,
1405 scsi_sg_copy_from_buffer(scmd, scmd->sense_buffer,
1406 SCSI_SENSE_BUFFERSIZE);
1409 static void myrb_read_capacity(struct myrb_hba *cb, struct scsi_cmnd *scmd,
1410 struct myrb_ldev_info *ldev_info)
1412 unsigned char data[8];
1414 dev_dbg(&scmd->device->sdev_gendev,
1415 "Capacity %u, blocksize %u\n",
1416 ldev_info->size, cb->ldev_block_size);
1417 put_unaligned_be32(ldev_info->size - 1, &data[0]);
1418 put_unaligned_be32(cb->ldev_block_size, &data[4]);
1419 scsi_sg_copy_from_buffer(scmd, data, 8);
1422 static int myrb_ldev_queuecommand(struct Scsi_Host *shost,
1423 struct scsi_cmnd *scmd)
1425 struct myrb_hba *cb = shost_priv(shost);
1426 struct myrb_cmdblk *cmd_blk = scsi_cmd_priv(scmd);
1427 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
1428 struct myrb_ldev_info *ldev_info;
1429 struct scsi_device *sdev = scmd->device;
1430 struct scatterlist *sgl;
1431 unsigned long flags;
1436 ldev_info = sdev->hostdata;
1437 if (ldev_info->state != MYRB_DEVICE_ONLINE &&
1438 ldev_info->state != MYRB_DEVICE_WO) {
1439 dev_dbg(&shost->shost_gendev, "ldev %u in state %x, skip\n",
1440 sdev->id, ldev_info ? ldev_info->state : 0xff);
1441 scmd->result = (DID_BAD_TARGET << 16);
1442 scmd->scsi_done(scmd);
1445 switch (scmd->cmnd[0]) {
1446 case TEST_UNIT_READY:
1447 scmd->result = (DID_OK << 16);
1448 scmd->scsi_done(scmd);
1451 if (scmd->cmnd[1] & 1) {
1452 /* Illegal request, invalid field in CDB */
1453 scsi_build_sense_buffer(0, scmd->sense_buffer,
1454 ILLEGAL_REQUEST, 0x24, 0);
1455 scmd->result = (DRIVER_SENSE << 24) |
1456 SAM_STAT_CHECK_CONDITION;
1458 myrb_inquiry(cb, scmd);
1459 scmd->result = (DID_OK << 16);
1461 scmd->scsi_done(scmd);
1463 case SYNCHRONIZE_CACHE:
1464 scmd->result = (DID_OK << 16);
1465 scmd->scsi_done(scmd);
1468 if ((scmd->cmnd[2] & 0x3F) != 0x3F &&
1469 (scmd->cmnd[2] & 0x3F) != 0x08) {
1470 /* Illegal request, invalid field in CDB */
1471 scsi_build_sense_buffer(0, scmd->sense_buffer,
1472 ILLEGAL_REQUEST, 0x24, 0);
1473 scmd->result = (DRIVER_SENSE << 24) |
1474 SAM_STAT_CHECK_CONDITION;
1476 myrb_mode_sense(cb, scmd, ldev_info);
1477 scmd->result = (DID_OK << 16);
1479 scmd->scsi_done(scmd);
1482 if ((scmd->cmnd[1] & 1) ||
1483 (scmd->cmnd[8] & 1)) {
1484 /* Illegal request, invalid field in CDB */
1485 scsi_build_sense_buffer(0, scmd->sense_buffer,
1486 ILLEGAL_REQUEST, 0x24, 0);
1487 scmd->result = (DRIVER_SENSE << 24) |
1488 SAM_STAT_CHECK_CONDITION;
1489 scmd->scsi_done(scmd);
1492 lba = get_unaligned_be32(&scmd->cmnd[2]);
1494 /* Illegal request, invalid field in CDB */
1495 scsi_build_sense_buffer(0, scmd->sense_buffer,
1496 ILLEGAL_REQUEST, 0x24, 0);
1497 scmd->result = (DRIVER_SENSE << 24) |
1498 SAM_STAT_CHECK_CONDITION;
1499 scmd->scsi_done(scmd);
1502 myrb_read_capacity(cb, scmd, ldev_info);
1503 scmd->scsi_done(scmd);
1506 myrb_request_sense(cb, scmd);
1507 scmd->result = (DID_OK << 16);
1509 case SEND_DIAGNOSTIC:
1510 if (scmd->cmnd[1] != 0x04) {
1511 /* Illegal request, invalid field in CDB */
1512 scsi_build_sense_buffer(0, scmd->sense_buffer,
1513 ILLEGAL_REQUEST, 0x24, 0);
1514 scmd->result = (DRIVER_SENSE << 24) |
1515 SAM_STAT_CHECK_CONDITION;
1517 /* Assume good status */
1518 scmd->result = (DID_OK << 16);
1520 scmd->scsi_done(scmd);
1523 if (ldev_info->state == MYRB_DEVICE_WO) {
1524 /* Data protect, attempt to read invalid data */
1525 scsi_build_sense_buffer(0, scmd->sense_buffer,
1526 DATA_PROTECT, 0x21, 0x06);
1527 scmd->result = (DRIVER_SENSE << 24) |
1528 SAM_STAT_CHECK_CONDITION;
1529 scmd->scsi_done(scmd);
1534 lba = (((scmd->cmnd[1] & 0x1F) << 16) |
1535 (scmd->cmnd[2] << 8) |
1537 block_cnt = scmd->cmnd[4];
1540 if (ldev_info->state == MYRB_DEVICE_WO) {
1541 /* Data protect, attempt to read invalid data */
1542 scsi_build_sense_buffer(0, scmd->sense_buffer,
1543 DATA_PROTECT, 0x21, 0x06);
1544 scmd->result = (DRIVER_SENSE << 24) |
1545 SAM_STAT_CHECK_CONDITION;
1546 scmd->scsi_done(scmd);
1551 case VERIFY: /* 0x2F */
1552 case WRITE_VERIFY: /* 0x2E */
1553 lba = get_unaligned_be32(&scmd->cmnd[2]);
1554 block_cnt = get_unaligned_be16(&scmd->cmnd[7]);
1557 if (ldev_info->state == MYRB_DEVICE_WO) {
1558 /* Data protect, attempt to read invalid data */
1559 scsi_build_sense_buffer(0, scmd->sense_buffer,
1560 DATA_PROTECT, 0x21, 0x06);
1561 scmd->result = (DRIVER_SENSE << 24) |
1562 SAM_STAT_CHECK_CONDITION;
1563 scmd->scsi_done(scmd);
1568 case VERIFY_12: /* 0xAF */
1569 case WRITE_VERIFY_12: /* 0xAE */
1570 lba = get_unaligned_be32(&scmd->cmnd[2]);
1571 block_cnt = get_unaligned_be32(&scmd->cmnd[6]);
1574 /* Illegal request, invalid opcode */
1575 scsi_build_sense_buffer(0, scmd->sense_buffer,
1576 ILLEGAL_REQUEST, 0x20, 0);
1577 scmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1578 scmd->scsi_done(scmd);
1582 myrb_reset_cmd(cmd_blk);
1583 mbox->type5.id = scmd->request->tag + 3;
1584 if (scmd->sc_data_direction == DMA_NONE)
1586 nsge = scsi_dma_map(scmd);
1588 sgl = scsi_sglist(scmd);
1589 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
1590 mbox->type5.opcode = MYRB_CMD_READ;
1592 mbox->type5.opcode = MYRB_CMD_WRITE;
1594 mbox->type5.ld.xfer_len = block_cnt;
1595 mbox->type5.ld.ldev_num = sdev->id;
1596 mbox->type5.lba = lba;
1597 mbox->type5.addr = (u32)sg_dma_address(sgl);
1599 struct myrb_sge *hw_sgl;
1600 dma_addr_t hw_sgl_addr;
1603 hw_sgl = dma_pool_alloc(cb->sg_pool, GFP_ATOMIC, &hw_sgl_addr);
1605 return SCSI_MLQUEUE_HOST_BUSY;
1607 cmd_blk->sgl = hw_sgl;
1608 cmd_blk->sgl_addr = hw_sgl_addr;
1610 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
1611 mbox->type5.opcode = MYRB_CMD_READ_SG;
1613 mbox->type5.opcode = MYRB_CMD_WRITE_SG;
1615 mbox->type5.ld.xfer_len = block_cnt;
1616 mbox->type5.ld.ldev_num = sdev->id;
1617 mbox->type5.lba = lba;
1618 mbox->type5.addr = hw_sgl_addr;
1619 mbox->type5.sg_count = nsge;
1621 scsi_for_each_sg(scmd, sgl, nsge, i) {
1622 hw_sgl->sge_addr = (u32)sg_dma_address(sgl);
1623 hw_sgl->sge_count = (u32)sg_dma_len(sgl);
1628 spin_lock_irqsave(&cb->queue_lock, flags);
1629 cb->qcmd(cb, cmd_blk);
1630 spin_unlock_irqrestore(&cb->queue_lock, flags);
1635 static int myrb_queuecommand(struct Scsi_Host *shost,
1636 struct scsi_cmnd *scmd)
1638 struct scsi_device *sdev = scmd->device;
1640 if (sdev->channel > myrb_logical_channel(shost)) {
1641 scmd->result = (DID_BAD_TARGET << 16);
1642 scmd->scsi_done(scmd);
1645 if (sdev->channel == myrb_logical_channel(shost))
1646 return myrb_ldev_queuecommand(shost, scmd);
1648 return myrb_pthru_queuecommand(shost, scmd);
1651 static int myrb_ldev_slave_alloc(struct scsi_device *sdev)
1653 struct myrb_hba *cb = shost_priv(sdev->host);
1654 struct myrb_ldev_info *ldev_info;
1655 unsigned short ldev_num = sdev->id;
1656 enum raid_level level;
1658 ldev_info = cb->ldev_info_buf + ldev_num;
1662 sdev->hostdata = kzalloc(sizeof(*ldev_info), GFP_KERNEL);
1663 if (!sdev->hostdata)
1665 dev_dbg(&sdev->sdev_gendev,
1666 "slave alloc ldev %d state %x\n",
1667 ldev_num, ldev_info->state);
1668 memcpy(sdev->hostdata, ldev_info,
1669 sizeof(*ldev_info));
1670 switch (ldev_info->raid_level) {
1671 case MYRB_RAID_LEVEL0:
1672 level = RAID_LEVEL_LINEAR;
1674 case MYRB_RAID_LEVEL1:
1675 level = RAID_LEVEL_1;
1677 case MYRB_RAID_LEVEL3:
1678 level = RAID_LEVEL_3;
1680 case MYRB_RAID_LEVEL5:
1681 level = RAID_LEVEL_5;
1683 case MYRB_RAID_LEVEL6:
1684 level = RAID_LEVEL_6;
1686 case MYRB_RAID_JBOD:
1687 level = RAID_LEVEL_JBOD;
1690 level = RAID_LEVEL_UNKNOWN;
1693 raid_set_level(myrb_raid_template, &sdev->sdev_gendev, level);
1697 static int myrb_pdev_slave_alloc(struct scsi_device *sdev)
1699 struct myrb_hba *cb = shost_priv(sdev->host);
1700 struct myrb_pdev_state *pdev_info;
1701 unsigned short status;
1703 if (sdev->id > MYRB_MAX_TARGETS)
1706 pdev_info = kzalloc(sizeof(*pdev_info), GFP_KERNEL|GFP_DMA);
1710 status = myrb_exec_type3D(cb, MYRB_CMD_GET_DEVICE_STATE,
1712 if (status != MYRB_STATUS_SUCCESS) {
1713 dev_dbg(&sdev->sdev_gendev,
1714 "Failed to get device state, status %x\n",
1719 if (!pdev_info->present) {
1720 dev_dbg(&sdev->sdev_gendev,
1721 "device not present, skip\n");
1725 dev_dbg(&sdev->sdev_gendev,
1726 "slave alloc pdev %d:%d state %x\n",
1727 sdev->channel, sdev->id, pdev_info->state);
1728 sdev->hostdata = pdev_info;
1733 static int myrb_slave_alloc(struct scsi_device *sdev)
1735 if (sdev->channel > myrb_logical_channel(sdev->host))
1741 if (sdev->channel == myrb_logical_channel(sdev->host))
1742 return myrb_ldev_slave_alloc(sdev);
1744 return myrb_pdev_slave_alloc(sdev);
1747 static int myrb_slave_configure(struct scsi_device *sdev)
1749 struct myrb_ldev_info *ldev_info;
1751 if (sdev->channel > myrb_logical_channel(sdev->host))
1754 if (sdev->channel < myrb_logical_channel(sdev->host)) {
1755 sdev->no_uld_attach = 1;
1761 ldev_info = sdev->hostdata;
1764 if (ldev_info->state != MYRB_DEVICE_ONLINE)
1765 sdev_printk(KERN_INFO, sdev,
1766 "Logical drive is %s\n",
1767 myrb_devstate_name(ldev_info->state));
1769 sdev->tagged_supported = 1;
1773 static void myrb_slave_destroy(struct scsi_device *sdev)
1775 kfree(sdev->hostdata);
1778 static int myrb_biosparam(struct scsi_device *sdev, struct block_device *bdev,
1779 sector_t capacity, int geom[])
1781 struct myrb_hba *cb = shost_priv(sdev->host);
1783 geom[0] = cb->ldev_geom_heads;
1784 geom[1] = cb->ldev_geom_sectors;
1785 geom[2] = sector_div(capacity, geom[0] * geom[1]);
1790 static ssize_t raid_state_show(struct device *dev,
1791 struct device_attribute *attr, char *buf)
1793 struct scsi_device *sdev = to_scsi_device(dev);
1794 struct myrb_hba *cb = shost_priv(sdev->host);
1797 if (!sdev->hostdata)
1798 return snprintf(buf, 16, "Unknown\n");
1800 if (sdev->channel == myrb_logical_channel(sdev->host)) {
1801 struct myrb_ldev_info *ldev_info = sdev->hostdata;
1804 name = myrb_devstate_name(ldev_info->state);
1806 ret = snprintf(buf, 64, "%s\n", name);
1808 ret = snprintf(buf, 64, "Invalid (%02X)\n",
1811 struct myrb_pdev_state *pdev_info = sdev->hostdata;
1812 unsigned short status;
1815 status = myrb_exec_type3D(cb, MYRB_CMD_GET_DEVICE_STATE,
1817 if (status != MYRB_STATUS_SUCCESS)
1818 sdev_printk(KERN_INFO, sdev,
1819 "Failed to get device state, status %x\n",
1822 if (!pdev_info->present)
1825 name = myrb_devstate_name(pdev_info->state);
1827 ret = snprintf(buf, 64, "%s\n", name);
1829 ret = snprintf(buf, 64, "Invalid (%02X)\n",
1835 static ssize_t raid_state_store(struct device *dev,
1836 struct device_attribute *attr, const char *buf, size_t count)
1838 struct scsi_device *sdev = to_scsi_device(dev);
1839 struct myrb_hba *cb = shost_priv(sdev->host);
1840 struct myrb_pdev_state *pdev_info;
1841 enum myrb_devstate new_state;
1842 unsigned short status;
1844 if (!strncmp(buf, "kill", 4) ||
1845 !strncmp(buf, "offline", 7))
1846 new_state = MYRB_DEVICE_DEAD;
1847 else if (!strncmp(buf, "online", 6))
1848 new_state = MYRB_DEVICE_ONLINE;
1849 else if (!strncmp(buf, "standby", 7))
1850 new_state = MYRB_DEVICE_STANDBY;
1854 pdev_info = sdev->hostdata;
1856 sdev_printk(KERN_INFO, sdev,
1857 "Failed - no physical device information\n");
1860 if (!pdev_info->present) {
1861 sdev_printk(KERN_INFO, sdev,
1862 "Failed - device not present\n");
1866 if (pdev_info->state == new_state)
1869 status = myrb_set_pdev_state(cb, sdev, new_state);
1871 case MYRB_STATUS_SUCCESS:
1873 case MYRB_STATUS_START_DEVICE_FAILED:
1874 sdev_printk(KERN_INFO, sdev,
1875 "Failed - Unable to Start Device\n");
1878 case MYRB_STATUS_NO_DEVICE:
1879 sdev_printk(KERN_INFO, sdev,
1880 "Failed - No Device at Address\n");
1883 case MYRB_STATUS_INVALID_CHANNEL_OR_TARGET:
1884 sdev_printk(KERN_INFO, sdev,
1885 "Failed - Invalid Channel or Target or Modifier\n");
1888 case MYRB_STATUS_CHANNEL_BUSY:
1889 sdev_printk(KERN_INFO, sdev,
1890 "Failed - Channel Busy\n");
1894 sdev_printk(KERN_INFO, sdev,
1895 "Failed - Unexpected Status %04X\n", status);
1901 static DEVICE_ATTR_RW(raid_state);
1903 static ssize_t raid_level_show(struct device *dev,
1904 struct device_attribute *attr, char *buf)
1906 struct scsi_device *sdev = to_scsi_device(dev);
1908 if (sdev->channel == myrb_logical_channel(sdev->host)) {
1909 struct myrb_ldev_info *ldev_info = sdev->hostdata;
1915 name = myrb_raidlevel_name(ldev_info->raid_level);
1917 return snprintf(buf, 64, "Invalid (%02X)\n",
1919 return snprintf(buf, 64, "%s\n", name);
1921 return snprintf(buf, 64, "Physical Drive\n");
1923 static DEVICE_ATTR_RO(raid_level);
1925 static ssize_t rebuild_show(struct device *dev,
1926 struct device_attribute *attr, char *buf)
1928 struct scsi_device *sdev = to_scsi_device(dev);
1929 struct myrb_hba *cb = shost_priv(sdev->host);
1930 struct myrb_rbld_progress rbld_buf;
1931 unsigned char status;
1933 if (sdev->channel < myrb_logical_channel(sdev->host))
1934 return snprintf(buf, 64, "physical device - not rebuilding\n");
1936 status = myrb_get_rbld_progress(cb, &rbld_buf);
1938 if (rbld_buf.ldev_num != sdev->id ||
1939 status != MYRB_STATUS_SUCCESS)
1940 return snprintf(buf, 64, "not rebuilding\n");
1942 return snprintf(buf, 64, "rebuilding block %u of %u\n",
1943 rbld_buf.ldev_size - rbld_buf.blocks_left,
1944 rbld_buf.ldev_size);
1947 static ssize_t rebuild_store(struct device *dev,
1948 struct device_attribute *attr, const char *buf, size_t count)
1950 struct scsi_device *sdev = to_scsi_device(dev);
1951 struct myrb_hba *cb = shost_priv(sdev->host);
1952 struct myrb_cmdblk *cmd_blk;
1953 union myrb_cmd_mbox *mbox;
1954 unsigned short status;
1958 rc = kstrtoint(buf, 0, &start);
1962 if (sdev->channel >= myrb_logical_channel(sdev->host))
1965 status = myrb_get_rbld_progress(cb, NULL);
1967 if (status == MYRB_STATUS_SUCCESS) {
1968 sdev_printk(KERN_INFO, sdev,
1969 "Rebuild Not Initiated; already in progress\n");
1972 mutex_lock(&cb->dcmd_mutex);
1973 cmd_blk = &cb->dcmd_blk;
1974 myrb_reset_cmd(cmd_blk);
1975 mbox = &cmd_blk->mbox;
1976 mbox->type3D.opcode = MYRB_CMD_REBUILD_ASYNC;
1977 mbox->type3D.id = MYRB_DCMD_TAG;
1978 mbox->type3D.channel = sdev->channel;
1979 mbox->type3D.target = sdev->id;
1980 status = myrb_exec_cmd(cb, cmd_blk);
1981 mutex_unlock(&cb->dcmd_mutex);
1983 struct pci_dev *pdev = cb->pdev;
1984 unsigned char *rate;
1985 dma_addr_t rate_addr;
1987 if (status != MYRB_STATUS_SUCCESS) {
1988 sdev_printk(KERN_INFO, sdev,
1989 "Rebuild Not Cancelled; not in progress\n");
1993 rate = dma_alloc_coherent(&pdev->dev, sizeof(char),
1994 &rate_addr, GFP_KERNEL);
1996 sdev_printk(KERN_INFO, sdev,
1997 "Cancellation of Rebuild Failed - Out of Memory\n");
2000 mutex_lock(&cb->dcmd_mutex);
2001 cmd_blk = &cb->dcmd_blk;
2002 myrb_reset_cmd(cmd_blk);
2003 mbox = &cmd_blk->mbox;
2004 mbox->type3R.opcode = MYRB_CMD_REBUILD_CONTROL;
2005 mbox->type3R.id = MYRB_DCMD_TAG;
2006 mbox->type3R.rbld_rate = 0xFF;
2007 mbox->type3R.addr = rate_addr;
2008 status = myrb_exec_cmd(cb, cmd_blk);
2009 dma_free_coherent(&pdev->dev, sizeof(char), rate, rate_addr);
2010 mutex_unlock(&cb->dcmd_mutex);
2012 if (status == MYRB_STATUS_SUCCESS) {
2013 sdev_printk(KERN_INFO, sdev, "Rebuild %s\n",
2014 start ? "Initiated" : "Cancelled");
2018 sdev_printk(KERN_INFO, sdev,
2019 "Rebuild Not Cancelled, status 0x%x\n",
2025 case MYRB_STATUS_ATTEMPT_TO_RBLD_ONLINE_DRIVE:
2026 msg = "Attempt to Rebuild Online or Unresponsive Drive";
2028 case MYRB_STATUS_RBLD_NEW_DISK_FAILED:
2029 msg = "New Disk Failed During Rebuild";
2031 case MYRB_STATUS_INVALID_ADDRESS:
2032 msg = "Invalid Device Address";
2034 case MYRB_STATUS_RBLD_OR_CHECK_INPROGRESS:
2035 msg = "Already in Progress";
2042 sdev_printk(KERN_INFO, sdev,
2043 "Rebuild Failed - %s\n", msg);
2045 sdev_printk(KERN_INFO, sdev,
2046 "Rebuild Failed, status 0x%x\n", status);
2050 static DEVICE_ATTR_RW(rebuild);
2052 static ssize_t consistency_check_store(struct device *dev,
2053 struct device_attribute *attr, const char *buf, size_t count)
2055 struct scsi_device *sdev = to_scsi_device(dev);
2056 struct myrb_hba *cb = shost_priv(sdev->host);
2057 struct myrb_rbld_progress rbld_buf;
2058 struct myrb_cmdblk *cmd_blk;
2059 union myrb_cmd_mbox *mbox;
2060 unsigned short ldev_num = 0xFFFF;
2061 unsigned short status;
2065 rc = kstrtoint(buf, 0, &start);
2069 if (sdev->channel < myrb_logical_channel(sdev->host))
2072 status = myrb_get_rbld_progress(cb, &rbld_buf);
2074 if (status == MYRB_STATUS_SUCCESS) {
2075 sdev_printk(KERN_INFO, sdev,
2076 "Check Consistency Not Initiated; already in progress\n");
2079 mutex_lock(&cb->dcmd_mutex);
2080 cmd_blk = &cb->dcmd_blk;
2081 myrb_reset_cmd(cmd_blk);
2082 mbox = &cmd_blk->mbox;
2083 mbox->type3C.opcode = MYRB_CMD_CHECK_CONSISTENCY_ASYNC;
2084 mbox->type3C.id = MYRB_DCMD_TAG;
2085 mbox->type3C.ldev_num = sdev->id;
2086 mbox->type3C.auto_restore = true;
2088 status = myrb_exec_cmd(cb, cmd_blk);
2089 mutex_unlock(&cb->dcmd_mutex);
2091 struct pci_dev *pdev = cb->pdev;
2092 unsigned char *rate;
2093 dma_addr_t rate_addr;
2095 if (ldev_num != sdev->id) {
2096 sdev_printk(KERN_INFO, sdev,
2097 "Check Consistency Not Cancelled; not in progress\n");
2100 rate = dma_alloc_coherent(&pdev->dev, sizeof(char),
2101 &rate_addr, GFP_KERNEL);
2103 sdev_printk(KERN_INFO, sdev,
2104 "Cancellation of Check Consistency Failed - Out of Memory\n");
2107 mutex_lock(&cb->dcmd_mutex);
2108 cmd_blk = &cb->dcmd_blk;
2109 myrb_reset_cmd(cmd_blk);
2110 mbox = &cmd_blk->mbox;
2111 mbox->type3R.opcode = MYRB_CMD_REBUILD_CONTROL;
2112 mbox->type3R.id = MYRB_DCMD_TAG;
2113 mbox->type3R.rbld_rate = 0xFF;
2114 mbox->type3R.addr = rate_addr;
2115 status = myrb_exec_cmd(cb, cmd_blk);
2116 dma_free_coherent(&pdev->dev, sizeof(char), rate, rate_addr);
2117 mutex_unlock(&cb->dcmd_mutex);
2119 if (status == MYRB_STATUS_SUCCESS) {
2120 sdev_printk(KERN_INFO, sdev, "Check Consistency %s\n",
2121 start ? "Initiated" : "Cancelled");
2125 sdev_printk(KERN_INFO, sdev,
2126 "Check Consistency Not Cancelled, status 0x%x\n",
2132 case MYRB_STATUS_ATTEMPT_TO_RBLD_ONLINE_DRIVE:
2133 msg = "Dependent Physical Device is DEAD";
2135 case MYRB_STATUS_RBLD_NEW_DISK_FAILED:
2136 msg = "New Disk Failed During Rebuild";
2138 case MYRB_STATUS_INVALID_ADDRESS:
2139 msg = "Invalid or Nonredundant Logical Drive";
2141 case MYRB_STATUS_RBLD_OR_CHECK_INPROGRESS:
2142 msg = "Already in Progress";
2149 sdev_printk(KERN_INFO, sdev,
2150 "Check Consistency Failed - %s\n", msg);
2152 sdev_printk(KERN_INFO, sdev,
2153 "Check Consistency Failed, status 0x%x\n", status);
2158 static ssize_t consistency_check_show(struct device *dev,
2159 struct device_attribute *attr, char *buf)
2161 return rebuild_show(dev, attr, buf);
2163 static DEVICE_ATTR_RW(consistency_check);
2165 static ssize_t ctlr_num_show(struct device *dev,
2166 struct device_attribute *attr, char *buf)
2168 struct Scsi_Host *shost = class_to_shost(dev);
2169 struct myrb_hba *cb = shost_priv(shost);
2171 return snprintf(buf, 20, "%u\n", cb->ctlr_num);
2173 static DEVICE_ATTR_RO(ctlr_num);
2175 static ssize_t firmware_show(struct device *dev,
2176 struct device_attribute *attr, char *buf)
2178 struct Scsi_Host *shost = class_to_shost(dev);
2179 struct myrb_hba *cb = shost_priv(shost);
2181 return snprintf(buf, 16, "%s\n", cb->fw_version);
2183 static DEVICE_ATTR_RO(firmware);
2185 static ssize_t model_show(struct device *dev,
2186 struct device_attribute *attr, char *buf)
2188 struct Scsi_Host *shost = class_to_shost(dev);
2189 struct myrb_hba *cb = shost_priv(shost);
2191 return snprintf(buf, 16, "%s\n", cb->model_name);
2193 static DEVICE_ATTR_RO(model);
2195 static ssize_t flush_cache_store(struct device *dev,
2196 struct device_attribute *attr, const char *buf, size_t count)
2198 struct Scsi_Host *shost = class_to_shost(dev);
2199 struct myrb_hba *cb = shost_priv(shost);
2200 unsigned short status;
2202 status = myrb_exec_type3(cb, MYRB_CMD_FLUSH, 0);
2203 if (status == MYRB_STATUS_SUCCESS) {
2204 shost_printk(KERN_INFO, shost,
2205 "Cache Flush Completed\n");
2208 shost_printk(KERN_INFO, shost,
2209 "Cache Flush Failed, status %x\n", status);
2212 static DEVICE_ATTR_WO(flush_cache);
2214 static struct device_attribute *myrb_sdev_attrs[] = {
2216 &dev_attr_consistency_check,
2217 &dev_attr_raid_state,
2218 &dev_attr_raid_level,
2222 static struct device_attribute *myrb_shost_attrs[] = {
2226 &dev_attr_flush_cache,
2230 static struct scsi_host_template myrb_template = {
2231 .module = THIS_MODULE,
2233 .proc_name = "myrb",
2234 .queuecommand = myrb_queuecommand,
2235 .eh_host_reset_handler = myrb_host_reset,
2236 .slave_alloc = myrb_slave_alloc,
2237 .slave_configure = myrb_slave_configure,
2238 .slave_destroy = myrb_slave_destroy,
2239 .bios_param = myrb_biosparam,
2240 .cmd_size = sizeof(struct myrb_cmdblk),
2241 .shost_attrs = myrb_shost_attrs,
2242 .sdev_attrs = myrb_sdev_attrs,
2247 * myrb_is_raid - return boolean indicating device is raid volume
2248 * @dev the device struct object
2250 static int myrb_is_raid(struct device *dev)
2252 struct scsi_device *sdev = to_scsi_device(dev);
2254 return sdev->channel == myrb_logical_channel(sdev->host);
2258 * myrb_get_resync - get raid volume resync percent complete
2259 * @dev the device struct object
2261 static void myrb_get_resync(struct device *dev)
2263 struct scsi_device *sdev = to_scsi_device(dev);
2264 struct myrb_hba *cb = shost_priv(sdev->host);
2265 struct myrb_rbld_progress rbld_buf;
2266 unsigned int percent_complete = 0;
2267 unsigned short status;
2268 unsigned int ldev_size = 0, remaining = 0;
2270 if (sdev->channel < myrb_logical_channel(sdev->host))
2272 status = myrb_get_rbld_progress(cb, &rbld_buf);
2273 if (status == MYRB_STATUS_SUCCESS) {
2274 if (rbld_buf.ldev_num == sdev->id) {
2275 ldev_size = rbld_buf.ldev_size;
2276 remaining = rbld_buf.blocks_left;
2279 if (remaining && ldev_size)
2280 percent_complete = (ldev_size - remaining) * 100 / ldev_size;
2281 raid_set_resync(myrb_raid_template, dev, percent_complete);
2285 * myrb_get_state - get raid volume status
2286 * @dev the device struct object
2288 static void myrb_get_state(struct device *dev)
2290 struct scsi_device *sdev = to_scsi_device(dev);
2291 struct myrb_hba *cb = shost_priv(sdev->host);
2292 struct myrb_ldev_info *ldev_info = sdev->hostdata;
2293 enum raid_state state = RAID_STATE_UNKNOWN;
2294 unsigned short status;
2296 if (sdev->channel < myrb_logical_channel(sdev->host) || !ldev_info)
2297 state = RAID_STATE_UNKNOWN;
2299 status = myrb_get_rbld_progress(cb, NULL);
2300 if (status == MYRB_STATUS_SUCCESS)
2301 state = RAID_STATE_RESYNCING;
2303 switch (ldev_info->state) {
2304 case MYRB_DEVICE_ONLINE:
2305 state = RAID_STATE_ACTIVE;
2307 case MYRB_DEVICE_WO:
2308 case MYRB_DEVICE_CRITICAL:
2309 state = RAID_STATE_DEGRADED;
2312 state = RAID_STATE_OFFLINE;
2316 raid_set_state(myrb_raid_template, dev, state);
2319 static struct raid_function_template myrb_raid_functions = {
2320 .cookie = &myrb_template,
2321 .is_raid = myrb_is_raid,
2322 .get_resync = myrb_get_resync,
2323 .get_state = myrb_get_state,
2326 static void myrb_handle_scsi(struct myrb_hba *cb, struct myrb_cmdblk *cmd_blk,
2327 struct scsi_cmnd *scmd)
2329 unsigned short status;
2334 scsi_dma_unmap(scmd);
2336 if (cmd_blk->dcdb) {
2337 memcpy(scmd->sense_buffer, &cmd_blk->dcdb->sense, 64);
2338 dma_pool_free(cb->dcdb_pool, cmd_blk->dcdb,
2339 cmd_blk->dcdb_addr);
2340 cmd_blk->dcdb = NULL;
2343 dma_pool_free(cb->sg_pool, cmd_blk->sgl, cmd_blk->sgl_addr);
2344 cmd_blk->sgl = NULL;
2345 cmd_blk->sgl_addr = 0;
2347 status = cmd_blk->status;
2349 case MYRB_STATUS_SUCCESS:
2350 case MYRB_STATUS_DEVICE_BUSY:
2351 scmd->result = (DID_OK << 16) | status;
2353 case MYRB_STATUS_BAD_DATA:
2354 dev_dbg(&scmd->device->sdev_gendev,
2355 "Bad Data Encountered\n");
2356 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2357 /* Unrecovered read error */
2358 scsi_build_sense_buffer(0, scmd->sense_buffer,
2359 MEDIUM_ERROR, 0x11, 0);
2362 scsi_build_sense_buffer(0, scmd->sense_buffer,
2363 MEDIUM_ERROR, 0x0C, 0);
2364 scmd->result = (DID_OK << 16) | SAM_STAT_CHECK_CONDITION;
2366 case MYRB_STATUS_IRRECOVERABLE_DATA_ERROR:
2367 scmd_printk(KERN_ERR, scmd, "Irrecoverable Data Error\n");
2368 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
2369 /* Unrecovered read error, auto-reallocation failed */
2370 scsi_build_sense_buffer(0, scmd->sense_buffer,
2371 MEDIUM_ERROR, 0x11, 0x04);
2373 /* Write error, auto-reallocation failed */
2374 scsi_build_sense_buffer(0, scmd->sense_buffer,
2375 MEDIUM_ERROR, 0x0C, 0x02);
2376 scmd->result = (DID_OK << 16) | SAM_STAT_CHECK_CONDITION;
2378 case MYRB_STATUS_LDRV_NONEXISTENT_OR_OFFLINE:
2379 dev_dbg(&scmd->device->sdev_gendev,
2380 "Logical Drive Nonexistent or Offline");
2381 scmd->result = (DID_BAD_TARGET << 16);
2383 case MYRB_STATUS_ACCESS_BEYOND_END_OF_LDRV:
2384 dev_dbg(&scmd->device->sdev_gendev,
2385 "Attempt to Access Beyond End of Logical Drive");
2386 /* Logical block address out of range */
2387 scsi_build_sense_buffer(0, scmd->sense_buffer,
2388 NOT_READY, 0x21, 0);
2390 case MYRB_STATUS_DEVICE_NONRESPONSIVE:
2391 dev_dbg(&scmd->device->sdev_gendev, "Device nonresponsive\n");
2392 scmd->result = (DID_BAD_TARGET << 16);
2395 scmd_printk(KERN_ERR, scmd,
2396 "Unexpected Error Status %04X", status);
2397 scmd->result = (DID_ERROR << 16);
2400 scmd->scsi_done(scmd);
2403 static void myrb_handle_cmdblk(struct myrb_hba *cb, struct myrb_cmdblk *cmd_blk)
2408 if (cmd_blk->completion) {
2409 complete(cmd_blk->completion);
2410 cmd_blk->completion = NULL;
2414 static void myrb_monitor(struct work_struct *work)
2416 struct myrb_hba *cb = container_of(work,
2417 struct myrb_hba, monitor_work.work);
2418 struct Scsi_Host *shost = cb->host;
2419 unsigned long interval = MYRB_PRIMARY_MONITOR_INTERVAL;
2421 dev_dbg(&shost->shost_gendev, "monitor tick\n");
2423 if (cb->new_ev_seq > cb->old_ev_seq) {
2424 int event = cb->old_ev_seq;
2426 dev_dbg(&shost->shost_gendev,
2427 "get event log no %d/%d\n",
2428 cb->new_ev_seq, event);
2429 myrb_get_event(cb, event);
2430 cb->old_ev_seq = event + 1;
2432 } else if (cb->need_err_info) {
2433 cb->need_err_info = false;
2434 dev_dbg(&shost->shost_gendev, "get error table\n");
2435 myrb_get_errtable(cb);
2437 } else if (cb->need_rbld && cb->rbld_first) {
2438 cb->need_rbld = false;
2439 dev_dbg(&shost->shost_gendev,
2440 "get rebuild progress\n");
2441 myrb_update_rbld_progress(cb);
2443 } else if (cb->need_ldev_info) {
2444 cb->need_ldev_info = false;
2445 dev_dbg(&shost->shost_gendev,
2446 "get logical drive info\n");
2447 myrb_get_ldev_info(cb);
2449 } else if (cb->need_rbld) {
2450 cb->need_rbld = false;
2451 dev_dbg(&shost->shost_gendev,
2452 "get rebuild progress\n");
2453 myrb_update_rbld_progress(cb);
2455 } else if (cb->need_cc_status) {
2456 cb->need_cc_status = false;
2457 dev_dbg(&shost->shost_gendev,
2458 "get consistency check progress\n");
2459 myrb_get_cc_progress(cb);
2461 } else if (cb->need_bgi_status) {
2462 cb->need_bgi_status = false;
2463 dev_dbg(&shost->shost_gendev, "get background init status\n");
2464 myrb_bgi_control(cb);
2467 dev_dbg(&shost->shost_gendev, "new enquiry\n");
2468 mutex_lock(&cb->dma_mutex);
2469 myrb_hba_enquiry(cb);
2470 mutex_unlock(&cb->dma_mutex);
2471 if ((cb->new_ev_seq - cb->old_ev_seq > 0) ||
2472 cb->need_err_info || cb->need_rbld ||
2473 cb->need_ldev_info || cb->need_cc_status ||
2474 cb->need_bgi_status) {
2475 dev_dbg(&shost->shost_gendev,
2476 "reschedule monitor\n");
2481 cb->primary_monitor_time = jiffies;
2482 queue_delayed_work(cb->work_q, &cb->monitor_work, interval);
2486 * myrb_err_status - reports controller BIOS messages
2488 * Controller BIOS messages are passed through the Error Status Register
2489 * when the driver performs the BIOS handshaking.
2491 * Return: true for fatal errors and false otherwise.
2493 static bool myrb_err_status(struct myrb_hba *cb, unsigned char error,
2494 unsigned char parm0, unsigned char parm1)
2496 struct pci_dev *pdev = cb->pdev;
2500 dev_info(&pdev->dev,
2501 "Physical Device %d:%d Not Responding\n",
2505 dev_notice(&pdev->dev, "Spinning Up Drives\n");
2508 dev_notice(&pdev->dev, "Configuration Checksum Error\n");
2511 dev_notice(&pdev->dev, "Mirror Race Recovery Failed\n");
2514 dev_notice(&pdev->dev, "Mirror Race Recovery In Progress\n");
2517 dev_notice(&pdev->dev, "Physical Device %d:%d COD Mismatch\n",
2521 dev_notice(&pdev->dev, "Logical Drive Installation Aborted\n");
2524 dev_notice(&pdev->dev, "Mirror Race On A Critical Logical Drive\n");
2527 dev_notice(&pdev->dev, "New Controller Configuration Found\n");
2530 dev_err(&pdev->dev, "Fatal Memory Parity Error\n");
2533 dev_err(&pdev->dev, "Unknown Initialization Error %02X\n",
2541 * Hardware-specific functions
2545 * DAC960 LA Series Controllers
2548 static inline void DAC960_LA_hw_mbox_new_cmd(void __iomem *base)
2550 writeb(DAC960_LA_IDB_HWMBOX_NEW_CMD, base + DAC960_LA_IDB_OFFSET);
2553 static inline void DAC960_LA_ack_hw_mbox_status(void __iomem *base)
2555 writeb(DAC960_LA_IDB_HWMBOX_ACK_STS, base + DAC960_LA_IDB_OFFSET);
2558 static inline void DAC960_LA_gen_intr(void __iomem *base)
2560 writeb(DAC960_LA_IDB_GEN_IRQ, base + DAC960_LA_IDB_OFFSET);
2563 static inline void DAC960_LA_reset_ctrl(void __iomem *base)
2565 writeb(DAC960_LA_IDB_CTRL_RESET, base + DAC960_LA_IDB_OFFSET);
2568 static inline void DAC960_LA_mem_mbox_new_cmd(void __iomem *base)
2570 writeb(DAC960_LA_IDB_MMBOX_NEW_CMD, base + DAC960_LA_IDB_OFFSET);
2573 static inline bool DAC960_LA_hw_mbox_is_full(void __iomem *base)
2575 unsigned char idb = readb(base + DAC960_LA_IDB_OFFSET);
2577 return !(idb & DAC960_LA_IDB_HWMBOX_EMPTY);
2580 static inline bool DAC960_LA_init_in_progress(void __iomem *base)
2582 unsigned char idb = readb(base + DAC960_LA_IDB_OFFSET);
2584 return !(idb & DAC960_LA_IDB_INIT_DONE);
2587 static inline void DAC960_LA_ack_hw_mbox_intr(void __iomem *base)
2589 writeb(DAC960_LA_ODB_HWMBOX_ACK_IRQ, base + DAC960_LA_ODB_OFFSET);
2592 static inline void DAC960_LA_ack_mem_mbox_intr(void __iomem *base)
2594 writeb(DAC960_LA_ODB_MMBOX_ACK_IRQ, base + DAC960_LA_ODB_OFFSET);
2597 static inline void DAC960_LA_ack_intr(void __iomem *base)
2599 writeb(DAC960_LA_ODB_HWMBOX_ACK_IRQ | DAC960_LA_ODB_MMBOX_ACK_IRQ,
2600 base + DAC960_LA_ODB_OFFSET);
2603 static inline bool DAC960_LA_hw_mbox_status_available(void __iomem *base)
2605 unsigned char odb = readb(base + DAC960_LA_ODB_OFFSET);
2607 return odb & DAC960_LA_ODB_HWMBOX_STS_AVAIL;
2610 static inline bool DAC960_LA_mem_mbox_status_available(void __iomem *base)
2612 unsigned char odb = readb(base + DAC960_LA_ODB_OFFSET);
2614 return odb & DAC960_LA_ODB_MMBOX_STS_AVAIL;
2617 static inline void DAC960_LA_enable_intr(void __iomem *base)
2619 unsigned char odb = 0xFF;
2621 odb &= ~DAC960_LA_IRQMASK_DISABLE_IRQ;
2622 writeb(odb, base + DAC960_LA_IRQMASK_OFFSET);
2625 static inline void DAC960_LA_disable_intr(void __iomem *base)
2627 unsigned char odb = 0xFF;
2629 odb |= DAC960_LA_IRQMASK_DISABLE_IRQ;
2630 writeb(odb, base + DAC960_LA_IRQMASK_OFFSET);
2633 static inline bool DAC960_LA_intr_enabled(void __iomem *base)
2635 unsigned char imask = readb(base + DAC960_LA_IRQMASK_OFFSET);
2637 return !(imask & DAC960_LA_IRQMASK_DISABLE_IRQ);
2640 static inline void DAC960_LA_write_cmd_mbox(union myrb_cmd_mbox *mem_mbox,
2641 union myrb_cmd_mbox *mbox)
2643 mem_mbox->words[1] = mbox->words[1];
2644 mem_mbox->words[2] = mbox->words[2];
2645 mem_mbox->words[3] = mbox->words[3];
2646 /* Memory barrier to prevent reordering */
2648 mem_mbox->words[0] = mbox->words[0];
2649 /* Memory barrier to force PCI access */
2653 static inline void DAC960_LA_write_hw_mbox(void __iomem *base,
2654 union myrb_cmd_mbox *mbox)
2656 writel(mbox->words[0], base + DAC960_LA_CMDOP_OFFSET);
2657 writel(mbox->words[1], base + DAC960_LA_MBOX4_OFFSET);
2658 writel(mbox->words[2], base + DAC960_LA_MBOX8_OFFSET);
2659 writeb(mbox->bytes[12], base + DAC960_LA_MBOX12_OFFSET);
2662 static inline unsigned char DAC960_LA_read_status_cmd_ident(void __iomem *base)
2664 return readb(base + DAC960_LA_STSID_OFFSET);
2667 static inline unsigned short DAC960_LA_read_status(void __iomem *base)
2669 return readw(base + DAC960_LA_STS_OFFSET);
2673 DAC960_LA_read_error_status(void __iomem *base, unsigned char *error,
2674 unsigned char *param0, unsigned char *param1)
2676 unsigned char errsts = readb(base + DAC960_LA_ERRSTS_OFFSET);
2678 if (!(errsts & DAC960_LA_ERRSTS_PENDING))
2680 errsts &= ~DAC960_LA_ERRSTS_PENDING;
2683 *param0 = readb(base + DAC960_LA_CMDOP_OFFSET);
2684 *param1 = readb(base + DAC960_LA_CMDID_OFFSET);
2685 writeb(0xFF, base + DAC960_LA_ERRSTS_OFFSET);
2689 static inline unsigned short
2690 DAC960_LA_mbox_init(struct pci_dev *pdev, void __iomem *base,
2691 union myrb_cmd_mbox *mbox)
2693 unsigned short status;
2696 while (timeout < MYRB_MAILBOX_TIMEOUT) {
2697 if (!DAC960_LA_hw_mbox_is_full(base))
2702 if (DAC960_LA_hw_mbox_is_full(base)) {
2704 "Timeout waiting for empty mailbox\n");
2705 return MYRB_STATUS_SUBSYS_TIMEOUT;
2707 DAC960_LA_write_hw_mbox(base, mbox);
2708 DAC960_LA_hw_mbox_new_cmd(base);
2710 while (timeout < MYRB_MAILBOX_TIMEOUT) {
2711 if (DAC960_LA_hw_mbox_status_available(base))
2716 if (!DAC960_LA_hw_mbox_status_available(base)) {
2717 dev_err(&pdev->dev, "Timeout waiting for mailbox status\n");
2718 return MYRB_STATUS_SUBSYS_TIMEOUT;
2720 status = DAC960_LA_read_status(base);
2721 DAC960_LA_ack_hw_mbox_intr(base);
2722 DAC960_LA_ack_hw_mbox_status(base);
2727 static int DAC960_LA_hw_init(struct pci_dev *pdev,
2728 struct myrb_hba *cb, void __iomem *base)
2731 unsigned char error, parm0, parm1;
2733 DAC960_LA_disable_intr(base);
2734 DAC960_LA_ack_hw_mbox_status(base);
2736 while (DAC960_LA_init_in_progress(base) &&
2737 timeout < MYRB_MAILBOX_TIMEOUT) {
2738 if (DAC960_LA_read_error_status(base, &error,
2740 myrb_err_status(cb, error, parm0, parm1))
2745 if (timeout == MYRB_MAILBOX_TIMEOUT) {
2747 "Timeout waiting for Controller Initialisation\n");
2750 if (!myrb_enable_mmio(cb, DAC960_LA_mbox_init)) {
2752 "Unable to Enable Memory Mailbox Interface\n");
2753 DAC960_LA_reset_ctrl(base);
2756 DAC960_LA_enable_intr(base);
2757 cb->qcmd = myrb_qcmd;
2758 cb->write_cmd_mbox = DAC960_LA_write_cmd_mbox;
2759 if (cb->dual_mode_interface)
2760 cb->get_cmd_mbox = DAC960_LA_mem_mbox_new_cmd;
2762 cb->get_cmd_mbox = DAC960_LA_hw_mbox_new_cmd;
2763 cb->disable_intr = DAC960_LA_disable_intr;
2764 cb->reset = DAC960_LA_reset_ctrl;
2769 static irqreturn_t DAC960_LA_intr_handler(int irq, void *arg)
2771 struct myrb_hba *cb = arg;
2772 void __iomem *base = cb->io_base;
2773 struct myrb_stat_mbox *next_stat_mbox;
2774 unsigned long flags;
2776 spin_lock_irqsave(&cb->queue_lock, flags);
2777 DAC960_LA_ack_intr(base);
2778 next_stat_mbox = cb->next_stat_mbox;
2779 while (next_stat_mbox->valid) {
2780 unsigned char id = next_stat_mbox->id;
2781 struct scsi_cmnd *scmd = NULL;
2782 struct myrb_cmdblk *cmd_blk = NULL;
2784 if (id == MYRB_DCMD_TAG)
2785 cmd_blk = &cb->dcmd_blk;
2786 else if (id == MYRB_MCMD_TAG)
2787 cmd_blk = &cb->mcmd_blk;
2789 scmd = scsi_host_find_tag(cb->host, id - 3);
2791 cmd_blk = scsi_cmd_priv(scmd);
2794 cmd_blk->status = next_stat_mbox->status;
2796 dev_err(&cb->pdev->dev,
2797 "Unhandled command completion %d\n", id);
2799 memset(next_stat_mbox, 0, sizeof(struct myrb_stat_mbox));
2800 if (++next_stat_mbox > cb->last_stat_mbox)
2801 next_stat_mbox = cb->first_stat_mbox;
2805 myrb_handle_cmdblk(cb, cmd_blk);
2807 myrb_handle_scsi(cb, cmd_blk, scmd);
2810 cb->next_stat_mbox = next_stat_mbox;
2811 spin_unlock_irqrestore(&cb->queue_lock, flags);
2815 struct myrb_privdata DAC960_LA_privdata = {
2816 .hw_init = DAC960_LA_hw_init,
2817 .irq_handler = DAC960_LA_intr_handler,
2818 .mmio_size = DAC960_LA_mmio_size,
2822 * DAC960 PG Series Controllers
2824 static inline void DAC960_PG_hw_mbox_new_cmd(void __iomem *base)
2826 writel(DAC960_PG_IDB_HWMBOX_NEW_CMD, base + DAC960_PG_IDB_OFFSET);
2829 static inline void DAC960_PG_ack_hw_mbox_status(void __iomem *base)
2831 writel(DAC960_PG_IDB_HWMBOX_ACK_STS, base + DAC960_PG_IDB_OFFSET);
2834 static inline void DAC960_PG_gen_intr(void __iomem *base)
2836 writel(DAC960_PG_IDB_GEN_IRQ, base + DAC960_PG_IDB_OFFSET);
2839 static inline void DAC960_PG_reset_ctrl(void __iomem *base)
2841 writel(DAC960_PG_IDB_CTRL_RESET, base + DAC960_PG_IDB_OFFSET);
2844 static inline void DAC960_PG_mem_mbox_new_cmd(void __iomem *base)
2846 writel(DAC960_PG_IDB_MMBOX_NEW_CMD, base + DAC960_PG_IDB_OFFSET);
2849 static inline bool DAC960_PG_hw_mbox_is_full(void __iomem *base)
2851 unsigned char idb = readl(base + DAC960_PG_IDB_OFFSET);
2853 return idb & DAC960_PG_IDB_HWMBOX_FULL;
2856 static inline bool DAC960_PG_init_in_progress(void __iomem *base)
2858 unsigned char idb = readl(base + DAC960_PG_IDB_OFFSET);
2860 return idb & DAC960_PG_IDB_INIT_IN_PROGRESS;
2863 static inline void DAC960_PG_ack_hw_mbox_intr(void __iomem *base)
2865 writel(DAC960_PG_ODB_HWMBOX_ACK_IRQ, base + DAC960_PG_ODB_OFFSET);
2868 static inline void DAC960_PG_ack_mem_mbox_intr(void __iomem *base)
2870 writel(DAC960_PG_ODB_MMBOX_ACK_IRQ, base + DAC960_PG_ODB_OFFSET);
2873 static inline void DAC960_PG_ack_intr(void __iomem *base)
2875 writel(DAC960_PG_ODB_HWMBOX_ACK_IRQ | DAC960_PG_ODB_MMBOX_ACK_IRQ,
2876 base + DAC960_PG_ODB_OFFSET);
2879 static inline bool DAC960_PG_hw_mbox_status_available(void __iomem *base)
2881 unsigned char odb = readl(base + DAC960_PG_ODB_OFFSET);
2883 return odb & DAC960_PG_ODB_HWMBOX_STS_AVAIL;
2886 static inline bool DAC960_PG_mem_mbox_status_available(void __iomem *base)
2888 unsigned char odb = readl(base + DAC960_PG_ODB_OFFSET);
2890 return odb & DAC960_PG_ODB_MMBOX_STS_AVAIL;
2893 static inline void DAC960_PG_enable_intr(void __iomem *base)
2895 unsigned int imask = (unsigned int)-1;
2897 imask &= ~DAC960_PG_IRQMASK_DISABLE_IRQ;
2898 writel(imask, base + DAC960_PG_IRQMASK_OFFSET);
2901 static inline void DAC960_PG_disable_intr(void __iomem *base)
2903 unsigned int imask = (unsigned int)-1;
2905 writel(imask, base + DAC960_PG_IRQMASK_OFFSET);
2908 static inline bool DAC960_PG_intr_enabled(void __iomem *base)
2910 unsigned int imask = readl(base + DAC960_PG_IRQMASK_OFFSET);
2912 return !(imask & DAC960_PG_IRQMASK_DISABLE_IRQ);
2915 static inline void DAC960_PG_write_cmd_mbox(union myrb_cmd_mbox *mem_mbox,
2916 union myrb_cmd_mbox *mbox)
2918 mem_mbox->words[1] = mbox->words[1];
2919 mem_mbox->words[2] = mbox->words[2];
2920 mem_mbox->words[3] = mbox->words[3];
2921 /* Memory barrier to prevent reordering */
2923 mem_mbox->words[0] = mbox->words[0];
2924 /* Memory barrier to force PCI access */
2928 static inline void DAC960_PG_write_hw_mbox(void __iomem *base,
2929 union myrb_cmd_mbox *mbox)
2931 writel(mbox->words[0], base + DAC960_PG_CMDOP_OFFSET);
2932 writel(mbox->words[1], base + DAC960_PG_MBOX4_OFFSET);
2933 writel(mbox->words[2], base + DAC960_PG_MBOX8_OFFSET);
2934 writeb(mbox->bytes[12], base + DAC960_PG_MBOX12_OFFSET);
2937 static inline unsigned char
2938 DAC960_PG_read_status_cmd_ident(void __iomem *base)
2940 return readb(base + DAC960_PG_STSID_OFFSET);
2943 static inline unsigned short
2944 DAC960_PG_read_status(void __iomem *base)
2946 return readw(base + DAC960_PG_STS_OFFSET);
2950 DAC960_PG_read_error_status(void __iomem *base, unsigned char *error,
2951 unsigned char *param0, unsigned char *param1)
2953 unsigned char errsts = readb(base + DAC960_PG_ERRSTS_OFFSET);
2955 if (!(errsts & DAC960_PG_ERRSTS_PENDING))
2957 errsts &= ~DAC960_PG_ERRSTS_PENDING;
2959 *param0 = readb(base + DAC960_PG_CMDOP_OFFSET);
2960 *param1 = readb(base + DAC960_PG_CMDID_OFFSET);
2961 writeb(0, base + DAC960_PG_ERRSTS_OFFSET);
2965 static inline unsigned short
2966 DAC960_PG_mbox_init(struct pci_dev *pdev, void __iomem *base,
2967 union myrb_cmd_mbox *mbox)
2969 unsigned short status;
2972 while (timeout < MYRB_MAILBOX_TIMEOUT) {
2973 if (!DAC960_PG_hw_mbox_is_full(base))
2978 if (DAC960_PG_hw_mbox_is_full(base)) {
2980 "Timeout waiting for empty mailbox\n");
2981 return MYRB_STATUS_SUBSYS_TIMEOUT;
2983 DAC960_PG_write_hw_mbox(base, mbox);
2984 DAC960_PG_hw_mbox_new_cmd(base);
2987 while (timeout < MYRB_MAILBOX_TIMEOUT) {
2988 if (DAC960_PG_hw_mbox_status_available(base))
2993 if (!DAC960_PG_hw_mbox_status_available(base)) {
2995 "Timeout waiting for mailbox status\n");
2996 return MYRB_STATUS_SUBSYS_TIMEOUT;
2998 status = DAC960_PG_read_status(base);
2999 DAC960_PG_ack_hw_mbox_intr(base);
3000 DAC960_PG_ack_hw_mbox_status(base);
3005 static int DAC960_PG_hw_init(struct pci_dev *pdev,
3006 struct myrb_hba *cb, void __iomem *base)
3009 unsigned char error, parm0, parm1;
3011 DAC960_PG_disable_intr(base);
3012 DAC960_PG_ack_hw_mbox_status(base);
3014 while (DAC960_PG_init_in_progress(base) &&
3015 timeout < MYRB_MAILBOX_TIMEOUT) {
3016 if (DAC960_PG_read_error_status(base, &error,
3018 myrb_err_status(cb, error, parm0, parm1))
3023 if (timeout == MYRB_MAILBOX_TIMEOUT) {
3025 "Timeout waiting for Controller Initialisation\n");
3028 if (!myrb_enable_mmio(cb, DAC960_PG_mbox_init)) {
3030 "Unable to Enable Memory Mailbox Interface\n");
3031 DAC960_PG_reset_ctrl(base);
3034 DAC960_PG_enable_intr(base);
3035 cb->qcmd = myrb_qcmd;
3036 cb->write_cmd_mbox = DAC960_PG_write_cmd_mbox;
3037 if (cb->dual_mode_interface)
3038 cb->get_cmd_mbox = DAC960_PG_mem_mbox_new_cmd;
3040 cb->get_cmd_mbox = DAC960_PG_hw_mbox_new_cmd;
3041 cb->disable_intr = DAC960_PG_disable_intr;
3042 cb->reset = DAC960_PG_reset_ctrl;
3047 static irqreturn_t DAC960_PG_intr_handler(int irq, void *arg)
3049 struct myrb_hba *cb = arg;
3050 void __iomem *base = cb->io_base;
3051 struct myrb_stat_mbox *next_stat_mbox;
3052 unsigned long flags;
3054 spin_lock_irqsave(&cb->queue_lock, flags);
3055 DAC960_PG_ack_intr(base);
3056 next_stat_mbox = cb->next_stat_mbox;
3057 while (next_stat_mbox->valid) {
3058 unsigned char id = next_stat_mbox->id;
3059 struct scsi_cmnd *scmd = NULL;
3060 struct myrb_cmdblk *cmd_blk = NULL;
3062 if (id == MYRB_DCMD_TAG)
3063 cmd_blk = &cb->dcmd_blk;
3064 else if (id == MYRB_MCMD_TAG)
3065 cmd_blk = &cb->mcmd_blk;
3067 scmd = scsi_host_find_tag(cb->host, id - 3);
3069 cmd_blk = scsi_cmd_priv(scmd);
3072 cmd_blk->status = next_stat_mbox->status;
3074 dev_err(&cb->pdev->dev,
3075 "Unhandled command completion %d\n", id);
3077 memset(next_stat_mbox, 0, sizeof(struct myrb_stat_mbox));
3078 if (++next_stat_mbox > cb->last_stat_mbox)
3079 next_stat_mbox = cb->first_stat_mbox;
3082 myrb_handle_cmdblk(cb, cmd_blk);
3084 myrb_handle_scsi(cb, cmd_blk, scmd);
3086 cb->next_stat_mbox = next_stat_mbox;
3087 spin_unlock_irqrestore(&cb->queue_lock, flags);
3091 struct myrb_privdata DAC960_PG_privdata = {
3092 .hw_init = DAC960_PG_hw_init,
3093 .irq_handler = DAC960_PG_intr_handler,
3094 .mmio_size = DAC960_PG_mmio_size,
3099 * DAC960 PD Series Controllers
3102 static inline void DAC960_PD_hw_mbox_new_cmd(void __iomem *base)
3104 writeb(DAC960_PD_IDB_HWMBOX_NEW_CMD, base + DAC960_PD_IDB_OFFSET);
3107 static inline void DAC960_PD_ack_hw_mbox_status(void __iomem *base)
3109 writeb(DAC960_PD_IDB_HWMBOX_ACK_STS, base + DAC960_PD_IDB_OFFSET);
3112 static inline void DAC960_PD_gen_intr(void __iomem *base)
3114 writeb(DAC960_PD_IDB_GEN_IRQ, base + DAC960_PD_IDB_OFFSET);
3117 static inline void DAC960_PD_reset_ctrl(void __iomem *base)
3119 writeb(DAC960_PD_IDB_CTRL_RESET, base + DAC960_PD_IDB_OFFSET);
3122 static inline bool DAC960_PD_hw_mbox_is_full(void __iomem *base)
3124 unsigned char idb = readb(base + DAC960_PD_IDB_OFFSET);
3126 return idb & DAC960_PD_IDB_HWMBOX_FULL;
3129 static inline bool DAC960_PD_init_in_progress(void __iomem *base)
3131 unsigned char idb = readb(base + DAC960_PD_IDB_OFFSET);
3133 return idb & DAC960_PD_IDB_INIT_IN_PROGRESS;
3136 static inline void DAC960_PD_ack_intr(void __iomem *base)
3138 writeb(DAC960_PD_ODB_HWMBOX_ACK_IRQ, base + DAC960_PD_ODB_OFFSET);
3141 static inline bool DAC960_PD_hw_mbox_status_available(void __iomem *base)
3143 unsigned char odb = readb(base + DAC960_PD_ODB_OFFSET);
3145 return odb & DAC960_PD_ODB_HWMBOX_STS_AVAIL;
3148 static inline void DAC960_PD_enable_intr(void __iomem *base)
3150 writeb(DAC960_PD_IRQMASK_ENABLE_IRQ, base + DAC960_PD_IRQEN_OFFSET);
3153 static inline void DAC960_PD_disable_intr(void __iomem *base)
3155 writeb(0, base + DAC960_PD_IRQEN_OFFSET);
3158 static inline bool DAC960_PD_intr_enabled(void __iomem *base)
3160 unsigned char imask = readb(base + DAC960_PD_IRQEN_OFFSET);
3162 return imask & DAC960_PD_IRQMASK_ENABLE_IRQ;
3165 static inline void DAC960_PD_write_cmd_mbox(void __iomem *base,
3166 union myrb_cmd_mbox *mbox)
3168 writel(mbox->words[0], base + DAC960_PD_CMDOP_OFFSET);
3169 writel(mbox->words[1], base + DAC960_PD_MBOX4_OFFSET);
3170 writel(mbox->words[2], base + DAC960_PD_MBOX8_OFFSET);
3171 writeb(mbox->bytes[12], base + DAC960_PD_MBOX12_OFFSET);
3174 static inline unsigned char
3175 DAC960_PD_read_status_cmd_ident(void __iomem *base)
3177 return readb(base + DAC960_PD_STSID_OFFSET);
3180 static inline unsigned short
3181 DAC960_PD_read_status(void __iomem *base)
3183 return readw(base + DAC960_PD_STS_OFFSET);
3187 DAC960_PD_read_error_status(void __iomem *base, unsigned char *error,
3188 unsigned char *param0, unsigned char *param1)
3190 unsigned char errsts = readb(base + DAC960_PD_ERRSTS_OFFSET);
3192 if (!(errsts & DAC960_PD_ERRSTS_PENDING))
3194 errsts &= ~DAC960_PD_ERRSTS_PENDING;
3196 *param0 = readb(base + DAC960_PD_CMDOP_OFFSET);
3197 *param1 = readb(base + DAC960_PD_CMDID_OFFSET);
3198 writeb(0, base + DAC960_PD_ERRSTS_OFFSET);
3202 static void DAC960_PD_qcmd(struct myrb_hba *cb, struct myrb_cmdblk *cmd_blk)
3204 void __iomem *base = cb->io_base;
3205 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
3207 while (DAC960_PD_hw_mbox_is_full(base))
3209 DAC960_PD_write_cmd_mbox(base, mbox);
3210 DAC960_PD_hw_mbox_new_cmd(base);
3213 static int DAC960_PD_hw_init(struct pci_dev *pdev,
3214 struct myrb_hba *cb, void __iomem *base)
3217 unsigned char error, parm0, parm1;
3219 if (!request_region(cb->io_addr, 0x80, "myrb")) {
3220 dev_err(&pdev->dev, "IO port 0x%lx busy\n",
3221 (unsigned long)cb->io_addr);
3224 DAC960_PD_disable_intr(base);
3225 DAC960_PD_ack_hw_mbox_status(base);
3227 while (DAC960_PD_init_in_progress(base) &&
3228 timeout < MYRB_MAILBOX_TIMEOUT) {
3229 if (DAC960_PD_read_error_status(base, &error,
3231 myrb_err_status(cb, error, parm0, parm1))
3236 if (timeout == MYRB_MAILBOX_TIMEOUT) {
3238 "Timeout waiting for Controller Initialisation\n");
3241 if (!myrb_enable_mmio(cb, NULL)) {
3243 "Unable to Enable Memory Mailbox Interface\n");
3244 DAC960_PD_reset_ctrl(base);
3247 DAC960_PD_enable_intr(base);
3248 cb->qcmd = DAC960_PD_qcmd;
3249 cb->disable_intr = DAC960_PD_disable_intr;
3250 cb->reset = DAC960_PD_reset_ctrl;
3255 static irqreturn_t DAC960_PD_intr_handler(int irq, void *arg)
3257 struct myrb_hba *cb = arg;
3258 void __iomem *base = cb->io_base;
3259 unsigned long flags;
3261 spin_lock_irqsave(&cb->queue_lock, flags);
3262 while (DAC960_PD_hw_mbox_status_available(base)) {
3263 unsigned char id = DAC960_PD_read_status_cmd_ident(base);
3264 struct scsi_cmnd *scmd = NULL;
3265 struct myrb_cmdblk *cmd_blk = NULL;
3267 if (id == MYRB_DCMD_TAG)
3268 cmd_blk = &cb->dcmd_blk;
3269 else if (id == MYRB_MCMD_TAG)
3270 cmd_blk = &cb->mcmd_blk;
3272 scmd = scsi_host_find_tag(cb->host, id - 3);
3274 cmd_blk = scsi_cmd_priv(scmd);
3277 cmd_blk->status = DAC960_PD_read_status(base);
3279 dev_err(&cb->pdev->dev,
3280 "Unhandled command completion %d\n", id);
3282 DAC960_PD_ack_intr(base);
3283 DAC960_PD_ack_hw_mbox_status(base);
3286 myrb_handle_cmdblk(cb, cmd_blk);
3288 myrb_handle_scsi(cb, cmd_blk, scmd);
3290 spin_unlock_irqrestore(&cb->queue_lock, flags);
3294 struct myrb_privdata DAC960_PD_privdata = {
3295 .hw_init = DAC960_PD_hw_init,
3296 .irq_handler = DAC960_PD_intr_handler,
3297 .mmio_size = DAC960_PD_mmio_size,
3302 * DAC960 P Series Controllers
3304 * Similar to the DAC960 PD Series Controllers, but some commands have
3308 static inline void myrb_translate_enquiry(void *enq)
3310 memcpy(enq + 132, enq + 36, 64);
3311 memset(enq + 36, 0, 96);
3314 static inline void myrb_translate_devstate(void *state)
3316 memcpy(state + 2, state + 3, 1);
3317 memmove(state + 4, state + 5, 2);
3318 memmove(state + 6, state + 8, 4);
3321 static inline void myrb_translate_to_rw_command(struct myrb_cmdblk *cmd_blk)
3323 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
3324 int ldev_num = mbox->type5.ld.ldev_num;
3326 mbox->bytes[3] &= 0x7;
3327 mbox->bytes[3] |= mbox->bytes[7] << 6;
3328 mbox->bytes[7] = ldev_num;
3331 static inline void myrb_translate_from_rw_command(struct myrb_cmdblk *cmd_blk)
3333 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
3334 int ldev_num = mbox->bytes[7];
3336 mbox->bytes[7] = mbox->bytes[3] >> 6;
3337 mbox->bytes[3] &= 0x7;
3338 mbox->bytes[3] |= ldev_num << 3;
3341 static void DAC960_P_qcmd(struct myrb_hba *cb, struct myrb_cmdblk *cmd_blk)
3343 void __iomem *base = cb->io_base;
3344 union myrb_cmd_mbox *mbox = &cmd_blk->mbox;
3346 switch (mbox->common.opcode) {
3347 case MYRB_CMD_ENQUIRY:
3348 mbox->common.opcode = MYRB_CMD_ENQUIRY_OLD;
3350 case MYRB_CMD_GET_DEVICE_STATE:
3351 mbox->common.opcode = MYRB_CMD_GET_DEVICE_STATE_OLD;
3354 mbox->common.opcode = MYRB_CMD_READ_OLD;
3355 myrb_translate_to_rw_command(cmd_blk);
3357 case MYRB_CMD_WRITE:
3358 mbox->common.opcode = MYRB_CMD_WRITE_OLD;
3359 myrb_translate_to_rw_command(cmd_blk);
3361 case MYRB_CMD_READ_SG:
3362 mbox->common.opcode = MYRB_CMD_READ_SG_OLD;
3363 myrb_translate_to_rw_command(cmd_blk);
3365 case MYRB_CMD_WRITE_SG:
3366 mbox->common.opcode = MYRB_CMD_WRITE_SG_OLD;
3367 myrb_translate_to_rw_command(cmd_blk);
3372 while (DAC960_PD_hw_mbox_is_full(base))
3374 DAC960_PD_write_cmd_mbox(base, mbox);
3375 DAC960_PD_hw_mbox_new_cmd(base);
3379 static int DAC960_P_hw_init(struct pci_dev *pdev,
3380 struct myrb_hba *cb, void __iomem *base)
3383 unsigned char error, parm0, parm1;
3385 if (!request_region(cb->io_addr, 0x80, "myrb")) {
3386 dev_err(&pdev->dev, "IO port 0x%lx busy\n",
3387 (unsigned long)cb->io_addr);
3390 DAC960_PD_disable_intr(base);
3391 DAC960_PD_ack_hw_mbox_status(base);
3393 while (DAC960_PD_init_in_progress(base) &&
3394 timeout < MYRB_MAILBOX_TIMEOUT) {
3395 if (DAC960_PD_read_error_status(base, &error,
3397 myrb_err_status(cb, error, parm0, parm1))
3402 if (timeout == MYRB_MAILBOX_TIMEOUT) {
3404 "Timeout waiting for Controller Initialisation\n");
3407 if (!myrb_enable_mmio(cb, NULL)) {
3409 "Unable to allocate DMA mapped memory\n");
3410 DAC960_PD_reset_ctrl(base);
3413 DAC960_PD_enable_intr(base);
3414 cb->qcmd = DAC960_P_qcmd;
3415 cb->disable_intr = DAC960_PD_disable_intr;
3416 cb->reset = DAC960_PD_reset_ctrl;
3421 static irqreturn_t DAC960_P_intr_handler(int irq, void *arg)
3423 struct myrb_hba *cb = arg;
3424 void __iomem *base = cb->io_base;
3425 unsigned long flags;
3427 spin_lock_irqsave(&cb->queue_lock, flags);
3428 while (DAC960_PD_hw_mbox_status_available(base)) {
3429 unsigned char id = DAC960_PD_read_status_cmd_ident(base);
3430 struct scsi_cmnd *scmd = NULL;
3431 struct myrb_cmdblk *cmd_blk = NULL;
3432 union myrb_cmd_mbox *mbox;
3433 enum myrb_cmd_opcode op;
3436 if (id == MYRB_DCMD_TAG)
3437 cmd_blk = &cb->dcmd_blk;
3438 else if (id == MYRB_MCMD_TAG)
3439 cmd_blk = &cb->mcmd_blk;
3441 scmd = scsi_host_find_tag(cb->host, id - 3);
3443 cmd_blk = scsi_cmd_priv(scmd);
3446 cmd_blk->status = DAC960_PD_read_status(base);
3448 dev_err(&cb->pdev->dev,
3449 "Unhandled command completion %d\n", id);
3451 DAC960_PD_ack_intr(base);
3452 DAC960_PD_ack_hw_mbox_status(base);
3457 mbox = &cmd_blk->mbox;
3458 op = mbox->common.opcode;
3460 case MYRB_CMD_ENQUIRY_OLD:
3461 mbox->common.opcode = MYRB_CMD_ENQUIRY;
3462 myrb_translate_enquiry(cb->enquiry);
3464 case MYRB_CMD_READ_OLD:
3465 mbox->common.opcode = MYRB_CMD_READ;
3466 myrb_translate_from_rw_command(cmd_blk);
3468 case MYRB_CMD_WRITE_OLD:
3469 mbox->common.opcode = MYRB_CMD_WRITE;
3470 myrb_translate_from_rw_command(cmd_blk);
3472 case MYRB_CMD_READ_SG_OLD:
3473 mbox->common.opcode = MYRB_CMD_READ_SG;
3474 myrb_translate_from_rw_command(cmd_blk);
3476 case MYRB_CMD_WRITE_SG_OLD:
3477 mbox->common.opcode = MYRB_CMD_WRITE_SG;
3478 myrb_translate_from_rw_command(cmd_blk);
3484 myrb_handle_cmdblk(cb, cmd_blk);
3486 myrb_handle_scsi(cb, cmd_blk, scmd);
3488 spin_unlock_irqrestore(&cb->queue_lock, flags);
3492 struct myrb_privdata DAC960_P_privdata = {
3493 .hw_init = DAC960_P_hw_init,
3494 .irq_handler = DAC960_P_intr_handler,
3495 .mmio_size = DAC960_PD_mmio_size,
3498 static struct myrb_hba *myrb_detect(struct pci_dev *pdev,
3499 const struct pci_device_id *entry)
3501 struct myrb_privdata *privdata =
3502 (struct myrb_privdata *)entry->driver_data;
3503 irq_handler_t irq_handler = privdata->irq_handler;
3504 unsigned int mmio_size = privdata->mmio_size;
3505 struct Scsi_Host *shost;
3506 struct myrb_hba *cb = NULL;
3508 shost = scsi_host_alloc(&myrb_template, sizeof(struct myrb_hba));
3510 dev_err(&pdev->dev, "Unable to allocate Controller\n");
3513 shost->max_cmd_len = 12;
3514 shost->max_lun = 256;
3515 cb = shost_priv(shost);
3516 mutex_init(&cb->dcmd_mutex);
3517 mutex_init(&cb->dma_mutex);
3521 if (pci_enable_device(pdev)) {
3522 dev_err(&pdev->dev, "Failed to enable PCI device\n");
3523 scsi_host_put(shost);
3527 if (privdata->hw_init == DAC960_PD_hw_init ||
3528 privdata->hw_init == DAC960_P_hw_init) {
3529 cb->io_addr = pci_resource_start(pdev, 0);
3530 cb->pci_addr = pci_resource_start(pdev, 1);
3532 cb->pci_addr = pci_resource_start(pdev, 0);
3534 pci_set_drvdata(pdev, cb);
3535 spin_lock_init(&cb->queue_lock);
3536 if (mmio_size < PAGE_SIZE)
3537 mmio_size = PAGE_SIZE;
3538 cb->mmio_base = ioremap(cb->pci_addr & PAGE_MASK, mmio_size);
3539 if (cb->mmio_base == NULL) {
3541 "Unable to map Controller Register Window\n");
3545 cb->io_base = cb->mmio_base + (cb->pci_addr & ~PAGE_MASK);
3546 if (privdata->hw_init(pdev, cb, cb->io_base))
3549 if (request_irq(pdev->irq, irq_handler, IRQF_SHARED, "myrb", cb) < 0) {
3551 "Unable to acquire IRQ Channel %d\n", pdev->irq);
3554 cb->irq = pdev->irq;
3559 "Failed to initialize Controller\n");
3564 static int myrb_probe(struct pci_dev *dev, const struct pci_device_id *entry)
3566 struct myrb_hba *cb;
3569 cb = myrb_detect(dev, entry);
3573 ret = myrb_get_hba_config(cb);
3579 if (!myrb_create_mempools(dev, cb)) {
3584 ret = scsi_add_host(cb->host, &dev->dev);
3586 dev_err(&dev->dev, "scsi_add_host failed with %d\n", ret);
3587 myrb_destroy_mempools(cb);
3590 scsi_scan_host(cb->host);
3598 static void myrb_remove(struct pci_dev *pdev)
3600 struct myrb_hba *cb = pci_get_drvdata(pdev);
3602 shost_printk(KERN_NOTICE, cb->host, "Flushing Cache...");
3603 myrb_exec_type3(cb, MYRB_CMD_FLUSH, 0);
3605 myrb_destroy_mempools(cb);
3609 static const struct pci_device_id myrb_id_table[] = {
3611 PCI_DEVICE_SUB(PCI_VENDOR_ID_DEC,
3612 PCI_DEVICE_ID_DEC_21285,
3613 PCI_VENDOR_ID_MYLEX,
3614 PCI_DEVICE_ID_MYLEX_DAC960_LA),
3615 .driver_data = (unsigned long) &DAC960_LA_privdata,
3618 PCI_DEVICE_DATA(MYLEX, DAC960_PG, &DAC960_PG_privdata),
3621 PCI_DEVICE_DATA(MYLEX, DAC960_PD, &DAC960_PD_privdata),
3624 PCI_DEVICE_DATA(MYLEX, DAC960_P, &DAC960_P_privdata),
3629 MODULE_DEVICE_TABLE(pci, myrb_id_table);
3631 static struct pci_driver myrb_pci_driver = {
3633 .id_table = myrb_id_table,
3634 .probe = myrb_probe,
3635 .remove = myrb_remove,
3638 static int __init myrb_init_module(void)
3642 myrb_raid_template = raid_class_attach(&myrb_raid_functions);
3643 if (!myrb_raid_template)
3646 ret = pci_register_driver(&myrb_pci_driver);
3648 raid_class_release(myrb_raid_template);
3653 static void __exit myrb_cleanup_module(void)
3655 pci_unregister_driver(&myrb_pci_driver);
3656 raid_class_release(myrb_raid_template);
3659 module_init(myrb_init_module);
3660 module_exit(myrb_cleanup_module);
3662 MODULE_DESCRIPTION("Mylex DAC960/AcceleRAID/eXtremeRAID driver (Block interface)");
3663 MODULE_AUTHOR("Hannes Reinecke <hare@suse.com>");
3664 MODULE_LICENSE("GPL");