fcal.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* fcal.c: Fibre Channel Arbitrated Loop SCSI host adapter driver.
  2. *
  3. * Copyright (C) 1998,1999 Jakub Jelinek (jj@ultra.linux.cz)
  4. *
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/delay.h>
  8. #include <linux/types.h>
  9. #include <linux/string.h>
  10. #include <linux/slab.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/stat.h>
  14. #include <linux/init.h>
  15. #include <linux/config.h>
  16. #ifdef CONFIG_KMOD
  17. #include <linux/kmod.h>
  18. #endif
  19. #include <asm/irq.h>
  20. #include "scsi.h"
  21. #include <scsi/scsi_host.h>
  22. #include "../fc4/fcp_impl.h"
  23. #include "fcal.h"
  24. #include <linux/module.h>
  25. /* #define FCAL_DEBUG */
  26. #define fcal_printk printk ("FCAL %s: ", fc->name); printk
  27. #ifdef FCAL_DEBUG
  28. #define FCALD(x) fcal_printk x;
  29. #define FCALND(x) printk ("FCAL: "); printk x;
  30. #else
  31. #define FCALD(x)
  32. #define FCALND(x)
  33. #endif
  34. static unsigned char alpa2target[] = {
  35. 0x7e, 0x7d, 0x7c, 0xff, 0x7b, 0xff, 0xff, 0xff, 0x7a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x79,
  36. 0x78, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x77, 0x76, 0xff, 0xff, 0x75, 0xff, 0x74, 0x73, 0x72,
  37. 0xff, 0xff, 0xff, 0x71, 0xff, 0x70, 0x6f, 0x6e, 0xff, 0x6d, 0x6c, 0x6b, 0x6a, 0x69, 0x68, 0xff,
  38. 0xff, 0x67, 0x66, 0x65, 0x64, 0x63, 0x62, 0xff, 0xff, 0x61, 0x60, 0xff, 0x5f, 0xff, 0xff, 0xff,
  39. 0xff, 0xff, 0xff, 0x5e, 0xff, 0x5d, 0x5c, 0x5b, 0xff, 0x5a, 0x59, 0x58, 0x57, 0x56, 0x55, 0xff,
  40. 0xff, 0x54, 0x53, 0x52, 0x51, 0x50, 0x4f, 0xff, 0xff, 0x4e, 0x4d, 0xff, 0x4c, 0xff, 0xff, 0xff,
  41. 0xff, 0xff, 0xff, 0x4b, 0xff, 0x4a, 0x49, 0x48, 0xff, 0x47, 0x46, 0x45, 0x44, 0x43, 0x42, 0xff,
  42. 0xff, 0x41, 0x40, 0x3f, 0x3e, 0x3d, 0x3c, 0xff, 0xff, 0x3b, 0x3a, 0xff, 0x39, 0xff, 0xff, 0xff,
  43. 0x38, 0x37, 0x36, 0xff, 0x35, 0xff, 0xff, 0xff, 0x34, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x33,
  44. 0x32, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x31, 0x30, 0xff, 0xff, 0x2f, 0xff, 0x2e, 0x2d, 0x2c,
  45. 0xff, 0xff, 0xff, 0x2b, 0xff, 0x2a, 0x29, 0x28, 0xff, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0xff,
  46. 0xff, 0x21, 0x20, 0x1f, 0x1e, 0x1d, 0x1c, 0xff, 0xff, 0x1b, 0x1a, 0xff, 0x19, 0xff, 0xff, 0xff,
  47. 0xff, 0xff, 0xff, 0x18, 0xff, 0x17, 0x16, 0x15, 0xff, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0xff,
  48. 0xff, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0xff, 0xff, 0x08, 0x07, 0xff, 0x06, 0xff, 0xff, 0xff,
  49. 0x05, 0x04, 0x03, 0xff, 0x02, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00
  50. };
  51. static unsigned char target2alpa[] = {
  52. 0xef, 0xe8, 0xe4, 0xe2, 0xe1, 0xe0, 0xdc, 0xda, 0xd9, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xce,
  53. 0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc7, 0xc6, 0xc5, 0xc3, 0xbc, 0xba, 0xb9, 0xb6, 0xb5, 0xb4, 0xb3,
  54. 0xb2, 0xb1, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9, 0xa7, 0xa6, 0xa5, 0xa3, 0x9f, 0x9e, 0x9d, 0x9b,
  55. 0x98, 0x97, 0x90, 0x8f, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7c, 0x7a, 0x79, 0x76, 0x75, 0x74, 0x73,
  56. 0x72, 0x71, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5c, 0x5a, 0x59, 0x56,
  57. 0x55, 0x54, 0x53, 0x52, 0x51, 0x4e, 0x4d, 0x4c, 0x4b, 0x4a, 0x49, 0x47, 0x46, 0x45, 0x43, 0x3c,
  58. 0x3a, 0x39, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x27, 0x26,
  59. 0x25, 0x23, 0x1f, 0x1e, 0x1d, 0x1b, 0x18, 0x17, 0x10, 0x0f, 0x08, 0x04, 0x02, 0x01, 0x00
  60. };
  61. static int fcal_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd);
  62. int fcal_slave_configure(struct scsi_device *device)
  63. {
  64. int depth_to_use;
  65. if (device->tagged_supported)
  66. depth_to_use = /* 254 */ 8;
  67. else
  68. depth_to_use = 2;
  69. scsi_adjust_queue_depth(device,
  70. (device->tagged_supported ?
  71. MSG_SIMPLE_TAG : 0),
  72. depth_to_use);
  73. return 0;
  74. }
  75. /* Detect all FC Arbitrated Loops attached to the machine.
  76. fc4 module has done all the work for us... */
  77. int __init fcal_detect(struct scsi_host_template *tpnt)
  78. {
  79. int nfcals = 0;
  80. fc_channel *fc;
  81. int fcalcount;
  82. int i;
  83. tpnt->proc_name = "fcal";
  84. fcalcount = 0;
  85. for_each_online_fc_channel(fc)
  86. if (fc->posmap)
  87. fcalcount++;
  88. FCALND(("%d channels online\n", fcalcount))
  89. if (!fcalcount) {
  90. #if defined(MODULE) && defined(CONFIG_FC4_SOCAL_MODULE) && defined(CONFIG_KMOD)
  91. request_module("socal");
  92. for_each_online_fc_channel(fc)
  93. if (fc->posmap)
  94. fcalcount++;
  95. if (!fcalcount)
  96. #endif
  97. return 0;
  98. }
  99. for_each_online_fc_channel(fc) {
  100. struct Scsi_Host *host;
  101. long *ages;
  102. struct fcal *fcal;
  103. if (!fc->posmap) continue;
  104. /* Strange, this is already registered to some other SCSI host, then it cannot be fcal */
  105. if (fc->scsi_name[0]) continue;
  106. memcpy (fc->scsi_name, "FCAL", 4);
  107. fc->can_queue = FCAL_CAN_QUEUE;
  108. fc->rsp_size = 64;
  109. fc->encode_addr = fcal_encode_addr;
  110. ages = kmalloc (128 * sizeof(long), GFP_KERNEL);
  111. if (!ages) continue;
  112. host = scsi_register (tpnt, sizeof (struct fcal));
  113. if (!host)
  114. {
  115. kfree(ages);
  116. continue;
  117. }
  118. if (!try_module_get(fc->module)) {
  119. kfree(ages);
  120. scsi_unregister(host);
  121. continue;
  122. }
  123. nfcals++;
  124. fcal = (struct fcal *)host->hostdata;
  125. fc->fcp_register(fc, TYPE_SCSI_FCP, 0);
  126. for (i = 0; i < fc->posmap->len; i++) {
  127. int status, target, alpa;
  128. alpa = fc->posmap->list[i];
  129. FCALD(("Sending PLOGI to %02x\n", alpa))
  130. target = alpa2target[alpa];
  131. status = fc_do_plogi(fc, alpa, fcal->node_wwn + target,
  132. fcal->nport_wwn + target);
  133. FCALD(("PLOGI returned with status %d\n", status))
  134. if (status != FC_STATUS_OK)
  135. continue;
  136. FCALD(("Sending PRLI to %02x\n", alpa))
  137. status = fc_do_prli(fc, alpa);
  138. FCALD(("PRLI returned with status %d\n", status))
  139. if (status == FC_STATUS_OK)
  140. fcal->map[target] = 1;
  141. }
  142. host->max_id = 127;
  143. host->irq = fc->irq;
  144. #ifdef __sparc_v9__
  145. host->unchecked_isa_dma = 1;
  146. #endif
  147. fc->channels = 1;
  148. fc->targets = 127;
  149. fc->ages = ages;
  150. memset (ages, 0, 128 * sizeof(long));
  151. fcal->fc = fc;
  152. FCALD(("Found FCAL\n"))
  153. }
  154. if (nfcals)
  155. #ifdef __sparc__
  156. printk ("FCAL: Total of %d Sun Enterprise Network Array (A5000 or EX500) channels found\n", nfcals);
  157. #else
  158. printk ("FCAL: Total of %d Fibre Channel Arbitrated Loops found\n", nfcals);
  159. #endif
  160. return nfcals;
  161. }
  162. int fcal_release(struct Scsi_Host *host)
  163. {
  164. struct fcal *fcal = (struct fcal *)host->hostdata;
  165. fc_channel *fc = fcal->fc;
  166. module_put(fc->module);
  167. fc->fcp_register(fc, TYPE_SCSI_FCP, 1);
  168. FCALND((" releasing fcal.\n"));
  169. kfree (fc->ages);
  170. FCALND(("released fcal!\n"));
  171. return 0;
  172. }
  173. #undef SPRINTF
  174. #define SPRINTF(args...) { if (pos < (buffer + length)) pos += sprintf (pos, ## args); }
  175. int fcal_proc_info (struct Scsi_Host *host, char *buffer, char **start, off_t offset, int length, int inout)
  176. {
  177. struct fcal *fcal;
  178. fc_channel *fc;
  179. char *pos = buffer;
  180. int i, j;
  181. if (inout) return length;
  182. fcal = (struct fcal *)host->hostdata;
  183. fc = fcal->fc;
  184. #ifdef __sparc__
  185. SPRINTF ("Sun Enterprise Network Array (A5000 or E?500) on %s PROM node %x\n", fc->name, fc->dev->prom_node);
  186. #else
  187. SPRINTF ("Fibre Channel Arbitrated Loop on %s\n", fc->name);
  188. #endif
  189. SPRINTF ("Initiator AL-PA: %02x\n", fc->sid);
  190. SPRINTF ("\nAttached devices:\n");
  191. for (i = 0; i < fc->posmap->len; i++) {
  192. unsigned char alpa = fc->posmap->list[i];
  193. unsigned char target;
  194. u32 *u1, *u2;
  195. target = alpa2target[alpa];
  196. u1 = (u32 *)&fcal->nport_wwn[target];
  197. u2 = (u32 *)&fcal->node_wwn[target];
  198. if (!u1[0] && !u1[1]) {
  199. SPRINTF (" [AL-PA: %02x] Not responded to PLOGI\n", alpa);
  200. } else if (!fcal->map[target]) {
  201. SPRINTF (" [AL-PA: %02x, Port WWN: %08x%08x, Node WWN: %08x%08x] Not responded to PRLI\n",
  202. alpa, u1[0], u1[1], u2[0], u2[1]);
  203. } else {
  204. struct scsi_device *scd;
  205. shost_for_each_device(scd, host)
  206. if (scd->id == target) {
  207. SPRINTF (" [AL-PA: %02x, Id: %02d, Port WWN: %08x%08x, Node WWN: %08x%08x] ",
  208. alpa, target, u1[0], u1[1], u2[0], u2[1]);
  209. SPRINTF ("%s ", (scd->type < MAX_SCSI_DEVICE_CODE) ?
  210. scsi_device_types[(short) scd->type] : "Unknown device");
  211. for (j = 0; (j < 8) && (scd->vendor[j] >= 0x20); j++)
  212. SPRINTF ("%c", scd->vendor[j]);
  213. SPRINTF (" ");
  214. for (j = 0; (j < 16) && (scd->model[j] >= 0x20); j++)
  215. SPRINTF ("%c", scd->model[j]);
  216. SPRINTF ("\n");
  217. }
  218. }
  219. }
  220. SPRINTF ("\n");
  221. *start = buffer + offset;
  222. if ((pos - buffer) < offset)
  223. return 0;
  224. else if (pos - buffer - offset < length)
  225. return pos - buffer - offset;
  226. else
  227. return length;
  228. }
  229. /*
  230. For FC-AL, we use a simple addressing: we have just one channel 0,
  231. and all AL-PAs are mapped to targets 0..0x7e
  232. */
  233. static int fcal_encode_addr(Scsi_Cmnd *SCpnt, u16 *addr, fc_channel *fc, fcp_cmnd *fcmd)
  234. {
  235. struct fcal *f;
  236. /* We don't support LUNs yet - I'm not sure if LUN should be in SCSI fcp_cdb, or in second byte of addr[0] */
  237. if (SCpnt->cmnd[1] & 0xe0) return -EINVAL;
  238. /* FC-PLDA tells us... */
  239. memset(addr, 0, 8);
  240. f = (struct fcal *)SCpnt->device->host->hostdata;
  241. if (!f->map[SCpnt->device->id])
  242. return -EINVAL;
  243. /* Now, determine DID: It will be Native Identifier, so we zero upper
  244. 2 bytes of the 3 byte DID, lowest byte will be AL-PA */
  245. fcmd->did = target2alpa[SCpnt->device->id];
  246. FCALD(("trying DID %06x\n", fcmd->did))
  247. return 0;
  248. }
  249. static struct scsi_host_template driver_template = {
  250. .name = "Fibre Channel Arbitrated Loop",
  251. .detect = fcal_detect,
  252. .release = fcal_release,
  253. .proc_info = fcal_proc_info,
  254. .queuecommand = fcp_scsi_queuecommand,
  255. .slave_configure = fcal_slave_configure,
  256. .can_queue = FCAL_CAN_QUEUE,
  257. .this_id = -1,
  258. .sg_tablesize = 1,
  259. .cmd_per_lun = 1,
  260. .use_clustering = ENABLE_CLUSTERING,
  261. .eh_abort_handler = fcp_scsi_abort,
  262. .eh_device_reset_handler = fcp_scsi_dev_reset,
  263. .eh_host_reset_handler = fcp_scsi_host_reset,
  264. };
  265. #include "scsi_module.c"
  266. MODULE_LICENSE("GPL");