fc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /* fc.c: Generic Fibre Channel and FC4 SCSI driver.
  2. *
  3. * Copyright (C) 1997,1998,1999 Jakub Jelinek (jj@ultra.linux.cz)
  4. * Copyright (C) 1997,1998 Jirka Hanika (geo@ff.cuni.cz)
  5. *
  6. * There are two kinds of Fibre Channel adapters used in Linux. Either
  7. * the adapter is "smart" and does all FC bookkeeping by itself and
  8. * just presents a standard SCSI interface to the operating system
  9. * (that's e.g. the case with Qlogic FC cards), or leaves most of the FC
  10. * bookkeeping to the OS (e.g. soc, socal). Drivers for the former adapters
  11. * will look like normal SCSI drivers (with the exception of max_id will be
  12. * usually 127), the latter on the other side allows SCSI, IP over FC and other
  13. * protocols. This driver tree is for the latter adapters.
  14. *
  15. * This file should support both Point-to-Point and Arbitrated Loop topologies.
  16. *
  17. * Sources:
  18. * Fibre Channel Physical & Signaling Interface (FC-PH), dpANS, 1994
  19. * dpANS Fibre Channel Protocol for SCSI (X3.269-199X), Rev. 012, 1995
  20. * Fibre Channel Arbitrated Loop (FC-AL), Rev. 4.5, 1995
  21. * Fibre Channel Private Loop SCSI Direct Attach (FC-PLDA), Rev. 2.1, 1997
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/types.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/ioport.h>
  31. #include <linux/in.h>
  32. #include <linux/slab.h>
  33. #include <linux/string.h>
  34. #include <linux/init.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/irq.h>
  37. #include <asm/semaphore.h>
  38. #include "fcp_impl.h"
  39. #include <scsi/scsi_host.h>
  40. /* #define FCDEBUG */
  41. #define fc_printk printk ("%s: ", fc->name); printk
  42. #ifdef FCDEBUG
  43. #define FCD(x) fc_printk x;
  44. #define FCND(x) printk ("FC: "); printk x;
  45. #else
  46. #define FCD(x)
  47. #define FCND(x)
  48. #endif
  49. #ifdef __sparc__
  50. #define dma_alloc_consistent(d,s,p) sbus_alloc_consistent(d,s,p)
  51. #define dma_free_consistent(d,s,v,h) sbus_free_consistent(d,s,v,h)
  52. #define dma_map_single(d,v,s,dir) sbus_map_single(d,v,s,dir)
  53. #define dma_unmap_single(d,h,s,dir) sbus_unmap_single(d,h,s,dir)
  54. #define dma_map_sg(d,s,n,dir) sbus_map_sg(d,s,n,dir)
  55. #define dma_unmap_sg(d,s,n,dir) sbus_unmap_sg(d,s,n,dir)
  56. #else
  57. #define dma_alloc_consistent(d,s,p) pci_alloc_consistent(d,s,p)
  58. #define dma_free_consistent(d,s,v,h) pci_free_consistent(d,s,v,h)
  59. #define dma_map_single(d,v,s,dir) pci_map_single(d,v,s,dir)
  60. #define dma_unmap_single(d,h,s,dir) pci_unmap_single(d,h,s,dir)
  61. #define dma_map_sg(d,s,n,dir) pci_map_sg(d,s,n,dir)
  62. #define dma_unmap_sg(d,s,n,dir) pci_unmap_sg(d,s,n,dir)
  63. #endif
  64. #define FCP_CMND(SCpnt) ((fcp_cmnd *)&(SCpnt->SCp))
  65. #define FC_SCMND(SCpnt) ((fc_channel *)(SCpnt->device->host->hostdata[0]))
  66. #define SC_FCMND(fcmnd) ((Scsi_Cmnd *)((long)fcmnd - (long)&(((Scsi_Cmnd *)0)->SCp)))
  67. static int fcp_scsi_queue_it(fc_channel *, Scsi_Cmnd *, fcp_cmnd *, int);
  68. void fcp_queue_empty(fc_channel *);
  69. static void fcp_scsi_insert_queue (fc_channel *fc, fcp_cmnd *fcmd)
  70. {
  71. if (!fc->scsi_que) {
  72. fc->scsi_que = fcmd;
  73. fcmd->next = fcmd;
  74. fcmd->prev = fcmd;
  75. } else {
  76. fc->scsi_que->prev->next = fcmd;
  77. fcmd->prev = fc->scsi_que->prev;
  78. fc->scsi_que->prev = fcmd;
  79. fcmd->next = fc->scsi_que;
  80. }
  81. }
  82. static void fcp_scsi_remove_queue (fc_channel *fc, fcp_cmnd *fcmd)
  83. {
  84. if (fcmd == fcmd->next) {
  85. fc->scsi_que = NULL;
  86. return;
  87. }
  88. if (fcmd == fc->scsi_que)
  89. fc->scsi_que = fcmd->next;
  90. fcmd->prev->next = fcmd->next;
  91. fcmd->next->prev = fcmd->prev;
  92. }
  93. fc_channel *fc_channels = NULL;
  94. #define LSMAGIC 620829043
  95. typedef struct {
  96. /* Must be first */
  97. struct semaphore sem;
  98. int magic;
  99. int count;
  100. logi *logi;
  101. fcp_cmnd *fcmds;
  102. atomic_t todo;
  103. struct timer_list timer;
  104. unsigned char grace[0];
  105. } ls;
  106. #define LSOMAGIC 654907799
  107. typedef struct {
  108. /* Must be first */
  109. struct semaphore sem;
  110. int magic;
  111. int count;
  112. fcp_cmnd *fcmds;
  113. atomic_t todo;
  114. struct timer_list timer;
  115. } lso;
  116. #define LSEMAGIC 84482456
  117. typedef struct {
  118. /* Must be first */
  119. struct semaphore sem;
  120. int magic;
  121. int status;
  122. struct timer_list timer;
  123. } lse;
  124. static void fcp_login_timeout(unsigned long data)
  125. {
  126. ls *l = (ls *)data;
  127. FCND(("Login timeout\n"))
  128. up(&l->sem);
  129. }
  130. static void fcp_login_done(fc_channel *fc, int i, int status)
  131. {
  132. fcp_cmnd *fcmd;
  133. logi *plogi;
  134. fc_hdr *fch;
  135. ls *l = (ls *)fc->ls;
  136. FCD(("Login done %d %d\n", i, status))
  137. if (i < l->count) {
  138. if (fc->state == FC_STATE_FPORT_OK) {
  139. FCD(("Additional FPORT_OK received with status %d\n", status))
  140. return;
  141. }
  142. switch (status) {
  143. case FC_STATUS_OK: /* Oh, we found a fabric */
  144. case FC_STATUS_P_RJT: /* Oh, we haven't found any */
  145. fc->state = FC_STATE_FPORT_OK;
  146. fcmd = l->fcmds + i;
  147. plogi = l->logi + 3 * i;
  148. dma_unmap_single (fc->dev, fcmd->cmd, 3 * sizeof(logi),
  149. DMA_BIDIRECTIONAL);
  150. plogi->code = LS_PLOGI;
  151. memcpy (&plogi->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
  152. memcpy (&plogi->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
  153. memcpy (&plogi->common, fc->common_svc, sizeof(common_svc_parm));
  154. memcpy (&plogi->class1, fc->class_svcs, 3*sizeof(svc_parm));
  155. fch = &fcmd->fch;
  156. fcmd->token += l->count;
  157. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, fc->did);
  158. FILL_FCHDR_SID(fch, fc->sid);
  159. #ifdef FCDEBUG
  160. {
  161. int i;
  162. unsigned *x = (unsigned *)plogi;
  163. printk ("logi: ");
  164. for (i = 0; i < 21; i++)
  165. printk ("%08x ", x[i]);
  166. printk ("\n");
  167. }
  168. #endif
  169. fcmd->cmd = dma_map_single (fc->dev, plogi, 3 * sizeof(logi),
  170. DMA_BIDIRECTIONAL);
  171. fcmd->rsp = fcmd->cmd + 2 * sizeof(logi);
  172. if (fc->hw_enque (fc, fcmd))
  173. printk ("FC: Cannot enque PLOGI packet on %s\n", fc->name);
  174. break;
  175. case FC_STATUS_ERR_OFFLINE:
  176. fc->state = FC_STATE_MAYBEOFFLINE;
  177. FCD (("FC is offline %d\n", l->grace[i]))
  178. break;
  179. default:
  180. printk ("FLOGI failed for %s with status %d\n", fc->name, status);
  181. /* Do some sort of error recovery here */
  182. break;
  183. }
  184. } else {
  185. i -= l->count;
  186. if (fc->state != FC_STATE_FPORT_OK) {
  187. FCD(("Unexpected N-PORT rsp received"))
  188. return;
  189. }
  190. switch (status) {
  191. case FC_STATUS_OK:
  192. plogi = l->logi + 3 * i;
  193. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
  194. DMA_BIDIRECTIONAL);
  195. if (!fc->wwn_dest.lo && !fc->wwn_dest.hi) {
  196. memcpy (&fc->wwn_dest, &plogi[1].node_wwn, sizeof(fc_wwn));
  197. FCD(("Dest WWN %08x%08x\n", *(u32 *)&fc->wwn_dest, fc->wwn_dest.lo))
  198. } else if (fc->wwn_dest.lo != plogi[1].node_wwn.lo ||
  199. fc->wwn_dest.hi != plogi[1].node_wwn.hi) {
  200. printk ("%s: mismatch in wwns. Got %08x%08x, expected %08x%08x\n",
  201. fc->name,
  202. *(u32 *)&plogi[1].node_wwn, plogi[1].node_wwn.lo,
  203. *(u32 *)&fc->wwn_dest, fc->wwn_dest.lo);
  204. }
  205. fc->state = FC_STATE_ONLINE;
  206. printk ("%s: ONLINE\n", fc->name);
  207. if (atomic_dec_and_test (&l->todo))
  208. up(&l->sem);
  209. break;
  210. case FC_STATUS_ERR_OFFLINE:
  211. fc->state = FC_STATE_OFFLINE;
  212. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
  213. DMA_BIDIRECTIONAL);
  214. printk ("%s: FC is offline\n", fc->name);
  215. if (atomic_dec_and_test (&l->todo))
  216. up(&l->sem);
  217. break;
  218. default:
  219. printk ("PLOGI failed for %s with status %d\n", fc->name, status);
  220. /* Do some sort of error recovery here */
  221. break;
  222. }
  223. }
  224. }
  225. static void fcp_report_map_done(fc_channel *fc, int i, int status)
  226. {
  227. fcp_cmnd *fcmd;
  228. fc_hdr *fch;
  229. unsigned char j;
  230. ls *l = (ls *)fc->ls;
  231. fc_al_posmap *p;
  232. FCD(("Report map done %d %d\n", i, status))
  233. switch (status) {
  234. case FC_STATUS_OK: /* Ok, let's have a fun on a loop */
  235. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi),
  236. DMA_BIDIRECTIONAL);
  237. p = (fc_al_posmap *)(l->logi + 3 * i);
  238. #ifdef FCDEBUG
  239. {
  240. u32 *u = (u32 *)p;
  241. FCD(("%08x\n", u[0]))
  242. u ++;
  243. FCD(("%08x.%08x.%08x.%08x.%08x.%08x.%08x.%08x\n", u[0],u[1],u[2],u[3],u[4],u[5],u[6],u[7]))
  244. }
  245. #endif
  246. if ((p->magic & 0xffff0000) != FC_AL_LILP || !p->len) {
  247. printk ("FC: Bad magic from REPORT_AL_MAP on %s - %08x\n", fc->name, p->magic);
  248. fc->state = FC_STATE_OFFLINE;
  249. } else {
  250. fc->posmap = (fcp_posmap *)kmalloc(sizeof(fcp_posmap)+p->len, GFP_KERNEL);
  251. if (!fc->posmap) {
  252. printk("FC: Not enough memory, offlining channel\n");
  253. fc->state = FC_STATE_OFFLINE;
  254. } else {
  255. int k;
  256. memset(fc->posmap, 0, sizeof(fcp_posmap)+p->len);
  257. /* FIXME: This is where SOCAL transfers our AL-PA.
  258. Keep it here till we found out what other cards do... */
  259. fc->sid = (p->magic & 0xff);
  260. for (i = 0; i < p->len; i++)
  261. if (p->alpa[i] == fc->sid)
  262. break;
  263. k = p->len;
  264. if (i == p->len)
  265. i = 0;
  266. else {
  267. p->len--;
  268. i++;
  269. }
  270. fc->posmap->len = p->len;
  271. for (j = 0; j < p->len; j++) {
  272. if (i == k) i = 0;
  273. fc->posmap->list[j] = p->alpa[i++];
  274. }
  275. fc->state = FC_STATE_ONLINE;
  276. }
  277. }
  278. printk ("%s: ONLINE\n", fc->name);
  279. if (atomic_dec_and_test (&l->todo))
  280. up(&l->sem);
  281. break;
  282. case FC_STATUS_POINTTOPOINT: /* We're Point-to-Point, no AL... */
  283. FCD(("SID %d DID %d\n", fc->sid, fc->did))
  284. fcmd = l->fcmds + i;
  285. dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi),
  286. DMA_BIDIRECTIONAL);
  287. fch = &fcmd->fch;
  288. memset(l->logi + 3 * i, 0, 3 * sizeof(logi));
  289. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
  290. FILL_FCHDR_SID(fch, 0);
  291. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  292. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  293. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  294. fch->param = 0;
  295. l->logi [3 * i].code = LS_FLOGI;
  296. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
  297. DMA_BIDIRECTIONAL);
  298. fcmd->rsp = fcmd->cmd + sizeof(logi);
  299. fcmd->cmdlen = sizeof(logi);
  300. fcmd->rsplen = sizeof(logi);
  301. fcmd->data = (dma_addr_t)NULL;
  302. fcmd->class = FC_CLASS_SIMPLE;
  303. fcmd->proto = TYPE_EXTENDED_LS;
  304. if (fc->hw_enque (fc, fcmd))
  305. printk ("FC: Cannot enque FLOGI packet on %s\n", fc->name);
  306. break;
  307. case FC_STATUS_ERR_OFFLINE:
  308. fc->state = FC_STATE_MAYBEOFFLINE;
  309. FCD (("FC is offline %d\n", l->grace[i]))
  310. break;
  311. default:
  312. printk ("FLOGI failed for %s with status %d\n", fc->name, status);
  313. /* Do some sort of error recovery here */
  314. break;
  315. }
  316. }
  317. void fcp_register(fc_channel *fc, u8 type, int unregister)
  318. {
  319. int size, i;
  320. int slots = (fc->can_queue * 3) >> 1;
  321. FCND(("Going to %sregister\n", unregister ? "un" : ""))
  322. if (type == TYPE_SCSI_FCP) {
  323. if (!unregister) {
  324. fc->scsi_cmd_pool = (fcp_cmd *)
  325. dma_alloc_consistent (fc->dev,
  326. slots * (sizeof (fcp_cmd) + fc->rsp_size),
  327. &fc->dma_scsi_cmd);
  328. fc->scsi_rsp_pool = (char *)(fc->scsi_cmd_pool + slots);
  329. fc->dma_scsi_rsp = fc->dma_scsi_cmd + slots * sizeof (fcp_cmd);
  330. fc->scsi_bitmap_end = (slots + 63) & ~63;
  331. size = fc->scsi_bitmap_end / 8;
  332. fc->scsi_bitmap = kmalloc (size, GFP_KERNEL);
  333. memset (fc->scsi_bitmap, 0, size);
  334. set_bit (0, fc->scsi_bitmap);
  335. for (i = fc->can_queue; i < fc->scsi_bitmap_end; i++)
  336. set_bit (i, fc->scsi_bitmap);
  337. fc->scsi_free = fc->can_queue;
  338. fc->cmd_slots = (fcp_cmnd **)kmalloc(slots * sizeof(fcp_cmnd*), GFP_KERNEL);
  339. memset(fc->cmd_slots, 0, slots * sizeof(fcp_cmnd*));
  340. fc->abort_count = 0;
  341. } else {
  342. fc->scsi_name[0] = 0;
  343. kfree (fc->scsi_bitmap);
  344. kfree (fc->cmd_slots);
  345. FCND(("Unregistering\n"));
  346. if (fc->rst_pkt) {
  347. if (fc->rst_pkt->eh_state == SCSI_STATE_UNUSED)
  348. kfree(fc->rst_pkt);
  349. else {
  350. /* Can't happen. Some memory would be lost. */
  351. printk("FC: Reset in progress. Now?!");
  352. }
  353. }
  354. FCND(("Unregistered\n"));
  355. }
  356. } else
  357. printk ("FC: %segistering unknown type %02x\n", unregister ? "Unr" : "R", type);
  358. }
  359. static void fcp_scsi_done(Scsi_Cmnd *SCpnt);
  360. static inline void fcp_scsi_receive(fc_channel *fc, int token, int status, fc_hdr *fch)
  361. {
  362. fcp_cmnd *fcmd;
  363. fcp_rsp *rsp;
  364. int host_status;
  365. Scsi_Cmnd *SCpnt;
  366. int sense_len;
  367. int rsp_status;
  368. fcmd = fc->cmd_slots[token];
  369. if (!fcmd) return;
  370. rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
  371. SCpnt = SC_FCMND(fcmd);
  372. if (SCpnt->done != fcp_scsi_done)
  373. return;
  374. rsp_status = rsp->fcp_status;
  375. FCD(("rsp_status %08x status %08x\n", rsp_status, status))
  376. switch (status) {
  377. case FC_STATUS_OK:
  378. host_status=DID_OK;
  379. if (rsp_status & FCP_STATUS_RESID) {
  380. #ifdef FCDEBUG
  381. FCD(("Resid %d\n", rsp->fcp_resid))
  382. {
  383. fcp_cmd *cmd = fc->scsi_cmd_pool + token;
  384. int i;
  385. printk ("Command ");
  386. for (i = 0; i < sizeof(fcp_cmd); i+=4)
  387. printk ("%08x ", *(u32 *)(((char *)cmd)+i));
  388. printk ("\nResponse ");
  389. for (i = 0; i < fc->rsp_size; i+=4)
  390. printk ("%08x ", *(u32 *)(((char *)rsp)+i));
  391. printk ("\n");
  392. }
  393. #endif
  394. }
  395. if (rsp_status & FCP_STATUS_SENSE_LEN) {
  396. sense_len = rsp->fcp_sense_len;
  397. if (sense_len > sizeof(SCpnt->sense_buffer)) sense_len = sizeof(SCpnt->sense_buffer);
  398. memcpy(SCpnt->sense_buffer, ((char *)(rsp+1)), sense_len);
  399. }
  400. if (fcmd->data) {
  401. if (SCpnt->use_sg)
  402. dma_unmap_sg(fc->dev, (struct scatterlist *)SCpnt->buffer,
  403. SCpnt->use_sg,
  404. SCpnt->sc_data_direction);
  405. else
  406. dma_unmap_single(fc->dev, fcmd->data, SCpnt->request_bufflen,
  407. SCpnt->sc_data_direction);
  408. }
  409. break;
  410. default:
  411. host_status=DID_ERROR; /* FIXME */
  412. FCD(("Wrong FC status %d for token %d\n", status, token))
  413. break;
  414. }
  415. if (status_byte(rsp_status) == QUEUE_FULL) {
  416. printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc->name, SCpnt->device->channel, SCpnt->device->id, rsp_status);
  417. }
  418. SCpnt->result = (host_status << 16) | (rsp_status & 0xff);
  419. #ifdef FCDEBUG
  420. if (host_status || SCpnt->result || rsp_status) printk("FC: host_status %d, packet status %d\n",
  421. host_status, SCpnt->result);
  422. #endif
  423. SCpnt->done = fcmd->done;
  424. fcmd->done=NULL;
  425. clear_bit(token, fc->scsi_bitmap);
  426. fc->scsi_free++;
  427. FCD(("Calling scsi_done with %08x\n", SCpnt->result))
  428. SCpnt->scsi_done(SCpnt);
  429. }
  430. void fcp_receive_solicited(fc_channel *fc, int proto, int token, int status, fc_hdr *fch)
  431. {
  432. int magic;
  433. FCD(("receive_solicited %d %d %d\n", proto, token, status))
  434. switch (proto) {
  435. case TYPE_SCSI_FCP:
  436. fcp_scsi_receive(fc, token, status, fch); break;
  437. case TYPE_EXTENDED_LS:
  438. case PROTO_REPORT_AL_MAP:
  439. magic = 0;
  440. if (fc->ls)
  441. magic = ((ls *)(fc->ls))->magic;
  442. if (magic == LSMAGIC) {
  443. ls *l = (ls *)fc->ls;
  444. int i = (token >= l->count) ? token - l->count : token;
  445. /* Let's be sure */
  446. if ((unsigned)i < l->count && l->fcmds[i].fc == fc) {
  447. if (proto == TYPE_EXTENDED_LS)
  448. fcp_login_done(fc, token, status);
  449. else
  450. fcp_report_map_done(fc, token, status);
  451. break;
  452. }
  453. }
  454. FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc, fc->ls, fc->cmd_slots))
  455. if (proto == TYPE_EXTENDED_LS && !fc->ls && fc->cmd_slots) {
  456. fcp_cmnd *fcmd;
  457. fcmd = fc->cmd_slots[token];
  458. if (fcmd && fcmd->ls && ((ls *)(fcmd->ls))->magic == LSEMAGIC) {
  459. lse *l = (lse *)fcmd->ls;
  460. l->status = status;
  461. up (&l->sem);
  462. }
  463. }
  464. break;
  465. case PROTO_OFFLINE:
  466. if (fc->ls && ((lso *)(fc->ls))->magic == LSOMAGIC) {
  467. lso *l = (lso *)fc->ls;
  468. if ((unsigned)token < l->count && l->fcmds[token].fc == fc) {
  469. /* Wow, OFFLINE response arrived :) */
  470. FCD(("OFFLINE Response arrived\n"))
  471. fc->state = FC_STATE_OFFLINE;
  472. if (atomic_dec_and_test (&l->todo))
  473. up(&l->sem);
  474. }
  475. }
  476. break;
  477. default:
  478. break;
  479. }
  480. }
  481. void fcp_state_change(fc_channel *fc, int state)
  482. {
  483. FCD(("state_change %d %d\n", state, fc->state))
  484. if (state == FC_STATE_ONLINE && fc->state == FC_STATE_MAYBEOFFLINE)
  485. fc->state = FC_STATE_UNINITED;
  486. else if (state == FC_STATE_ONLINE)
  487. printk (KERN_WARNING "%s: state change to ONLINE\n", fc->name);
  488. else
  489. printk (KERN_ERR "%s: state change to OFFLINE\n", fc->name);
  490. }
  491. int fcp_initialize(fc_channel *fcchain, int count)
  492. {
  493. fc_channel *fc;
  494. fcp_cmnd *fcmd;
  495. int i, retry, ret;
  496. ls *l;
  497. FCND(("fcp_inititialize %08lx\n", (long)fcp_init))
  498. FCND(("fc_channels %08lx\n", (long)fc_channels))
  499. FCND((" SID %d DID %d\n", fcchain->sid, fcchain->did))
  500. l = kmalloc(sizeof (ls) + count, GFP_KERNEL);
  501. if (!l) {
  502. printk ("FC: Cannot allocate memory for initialization\n");
  503. return -ENOMEM;
  504. }
  505. memset (l, 0, sizeof(ls) + count);
  506. l->magic = LSMAGIC;
  507. l->count = count;
  508. FCND(("FCP Init for %d channels\n", count))
  509. init_MUTEX_LOCKED(&l->sem);
  510. init_timer(&l->timer);
  511. l->timer.function = fcp_login_timeout;
  512. l->timer.data = (unsigned long)l;
  513. atomic_set (&l->todo, count);
  514. l->logi = kmalloc (count * 3 * sizeof(logi), GFP_KERNEL);
  515. l->fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  516. if (!l->logi || !l->fcmds) {
  517. if (l->logi) kfree (l->logi);
  518. if (l->fcmds) kfree (l->fcmds);
  519. kfree (l);
  520. printk ("FC: Cannot allocate DMA memory for initialization\n");
  521. return -ENOMEM;
  522. }
  523. memset (l->logi, 0, count * 3 * sizeof(logi));
  524. memset (l->fcmds, 0, count * sizeof(fcp_cmnd));
  525. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  526. fc->state = FC_STATE_UNINITED;
  527. fc->rst_pkt = NULL; /* kmalloc when first used */
  528. }
  529. /* First try if we are in a AL topology */
  530. FCND(("Initializing REPORT_MAP packets\n"))
  531. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  532. fcmd = l->fcmds + i;
  533. fc->login = fcmd;
  534. fc->ls = (void *)l;
  535. /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
  536. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
  537. DMA_BIDIRECTIONAL);
  538. fcmd->proto = PROTO_REPORT_AL_MAP;
  539. fcmd->token = i;
  540. fcmd->fc = fc;
  541. }
  542. for (retry = 0; retry < 8; retry++) {
  543. int nqueued = 0;
  544. FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
  545. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  546. if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
  547. continue;
  548. disable_irq(fc->irq);
  549. if (fc->state == FC_STATE_MAYBEOFFLINE) {
  550. if (!l->grace[i]) {
  551. l->grace[i]++;
  552. FCD(("Grace\n"))
  553. } else {
  554. fc->state = FC_STATE_OFFLINE;
  555. enable_irq(fc->irq);
  556. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  557. if (atomic_dec_and_test (&l->todo))
  558. goto all_done;
  559. }
  560. }
  561. ret = fc->hw_enque (fc, fc->login);
  562. enable_irq(fc->irq);
  563. if (!ret) {
  564. nqueued++;
  565. continue;
  566. }
  567. if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
  568. /* Oh yes, this card handles Point-to-Point only, so let's try that. */
  569. fc_hdr *fch;
  570. FCD(("SID %d DID %d\n", fc->sid, fc->did))
  571. fcmd = l->fcmds + i;
  572. dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  573. fch = &fcmd->fch;
  574. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
  575. FILL_FCHDR_SID(fch, 0);
  576. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  577. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  578. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  579. fch->param = 0;
  580. l->logi [3 * i].code = LS_FLOGI;
  581. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  582. fcmd->rsp = fcmd->cmd + sizeof(logi);
  583. fcmd->cmdlen = sizeof(logi);
  584. fcmd->rsplen = sizeof(logi);
  585. fcmd->data = (dma_addr_t)NULL;
  586. fcmd->class = FC_CLASS_SIMPLE;
  587. fcmd->proto = TYPE_EXTENDED_LS;
  588. } else
  589. printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
  590. }
  591. if (nqueued) {
  592. l->timer.expires = jiffies + 5 * HZ;
  593. add_timer(&l->timer);
  594. down(&l->sem);
  595. if (!atomic_read(&l->todo)) {
  596. FCND(("All channels answered in time\n"))
  597. break; /* All fc channels have answered us */
  598. }
  599. }
  600. }
  601. all_done:
  602. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  603. fc->ls = NULL;
  604. switch (fc->state) {
  605. case FC_STATE_ONLINE: break;
  606. case FC_STATE_OFFLINE: break;
  607. default: dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  608. break;
  609. }
  610. }
  611. del_timer(&l->timer);
  612. kfree (l->logi);
  613. kfree (l->fcmds);
  614. kfree (l);
  615. return 0;
  616. }
  617. int fcp_forceoffline(fc_channel *fcchain, int count)
  618. {
  619. fc_channel *fc;
  620. fcp_cmnd *fcmd;
  621. int i, ret;
  622. lso l;
  623. memset (&l, 0, sizeof(lso));
  624. l.count = count;
  625. l.magic = LSOMAGIC;
  626. FCND(("FCP Force Offline for %d channels\n", count))
  627. init_MUTEX_LOCKED(&l.sem);
  628. init_timer(&l.timer);
  629. l.timer.function = fcp_login_timeout;
  630. l.timer.data = (unsigned long)&l;
  631. atomic_set (&l.todo, count);
  632. l.fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  633. if (!l.fcmds) {
  634. kfree (l.fcmds);
  635. printk ("FC: Cannot allocate memory for forcing offline\n");
  636. return -ENOMEM;
  637. }
  638. memset (l.fcmds, 0, count * sizeof(fcp_cmnd));
  639. FCND(("Initializing OFFLINE packets\n"))
  640. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  641. fc->state = FC_STATE_UNINITED;
  642. fcmd = l.fcmds + i;
  643. fc->login = fcmd;
  644. fc->ls = (void *)&l;
  645. fcmd->did = fc->did;
  646. fcmd->class = FC_CLASS_OFFLINE;
  647. fcmd->proto = PROTO_OFFLINE;
  648. fcmd->token = i;
  649. fcmd->fc = fc;
  650. disable_irq(fc->irq);
  651. ret = fc->hw_enque (fc, fc->login);
  652. enable_irq(fc->irq);
  653. if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
  654. }
  655. l.timer.expires = jiffies + 5 * HZ;
  656. add_timer(&l.timer);
  657. down(&l.sem);
  658. del_timer(&l.timer);
  659. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
  660. fc->ls = NULL;
  661. kfree (l.fcmds);
  662. return 0;
  663. }
  664. int fcp_init(fc_channel *fcchain)
  665. {
  666. fc_channel *fc;
  667. int count=0;
  668. int ret;
  669. for (fc = fcchain; fc; fc = fc->next) {
  670. fc->fcp_register = fcp_register;
  671. count++;
  672. }
  673. ret = fcp_initialize (fcchain, count);
  674. if (ret)
  675. return ret;
  676. if (!fc_channels)
  677. fc_channels = fcchain;
  678. else {
  679. for (fc = fc_channels; fc->next; fc = fc->next);
  680. fc->next = fcchain;
  681. }
  682. return ret;
  683. }
  684. void fcp_release(fc_channel *fcchain, int count) /* count must > 0 */
  685. {
  686. fc_channel *fc;
  687. fc_channel *fcx;
  688. for (fc = fcchain; --count && fc->next; fc = fc->next);
  689. if (count) {
  690. printk("FC: nothing to release\n");
  691. return;
  692. }
  693. if (fc_channels == fcchain)
  694. fc_channels = fc->next;
  695. else {
  696. for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
  697. fcx->next = fc->next;
  698. }
  699. fc->next = NULL;
  700. /*
  701. * We've just grabbed fcchain out of the fc_channel list
  702. * and zero-terminated it, while destroying the count.
  703. *
  704. * Freeing the fc's is the low level driver's responsibility.
  705. */
  706. }
  707. static void fcp_scsi_done (Scsi_Cmnd *SCpnt)
  708. {
  709. if (FCP_CMND(SCpnt)->done)
  710. FCP_CMND(SCpnt)->done(SCpnt);
  711. }
  712. static int fcp_scsi_queue_it(fc_channel *fc, Scsi_Cmnd *SCpnt, fcp_cmnd *fcmd, int prepare)
  713. {
  714. long i;
  715. fcp_cmd *cmd;
  716. u32 fcp_cntl;
  717. if (prepare) {
  718. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  719. set_bit (i, fc->scsi_bitmap);
  720. fcmd->token = i;
  721. cmd = fc->scsi_cmd_pool + i;
  722. if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
  723. /* Invalid channel/id/lun and couldn't map it into fcp_addr */
  724. clear_bit (i, fc->scsi_bitmap);
  725. SCpnt->result = (DID_BAD_TARGET << 16);
  726. SCpnt->scsi_done(SCpnt);
  727. return 0;
  728. }
  729. fc->scsi_free--;
  730. fc->cmd_slots[fcmd->token] = fcmd;
  731. if (SCpnt->device->tagged_supported) {
  732. if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
  733. fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
  734. fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
  735. } else
  736. fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
  737. } else
  738. fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
  739. if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
  740. cmd->fcp_cntl = fcp_cntl;
  741. fcmd->data = (dma_addr_t)NULL;
  742. } else {
  743. switch (SCpnt->cmnd[0]) {
  744. case WRITE_6:
  745. case WRITE_10:
  746. case WRITE_12:
  747. cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
  748. default:
  749. cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
  750. }
  751. if (!SCpnt->use_sg) {
  752. cmd->fcp_data_len = SCpnt->request_bufflen;
  753. fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
  754. SCpnt->request_bufflen,
  755. SCpnt->sc_data_direction);
  756. } else {
  757. struct scatterlist *sg = (struct scatterlist *)SCpnt->buffer;
  758. int nents;
  759. FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
  760. nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
  761. SCpnt->sc_data_direction);
  762. if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
  763. fcmd->data = sg_dma_address(sg);
  764. cmd->fcp_data_len = sg_dma_len(sg);
  765. }
  766. }
  767. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  768. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  769. FCD(("XXX: %04x.%04x.%04x.%04x - %08x%08x%08x\n", cmd->fcp_addr[0], cmd->fcp_addr[1], cmd->fcp_addr[2], cmd->fcp_addr[3], *(u32 *)SCpnt->cmnd, *(u32 *)(SCpnt->cmnd+4), *(u32 *)(SCpnt->cmnd+8)))
  770. }
  771. FCD(("Trying to enque %p\n", fcmd))
  772. if (!fc->scsi_que) {
  773. if (!fc->hw_enque (fc, fcmd)) {
  774. FCD(("hw_enque succeeded for %p\n", fcmd))
  775. return 0;
  776. }
  777. }
  778. FCD(("Putting into que1 %p\n", fcmd))
  779. fcp_scsi_insert_queue (fc, fcmd);
  780. return 0;
  781. }
  782. int fcp_scsi_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *))
  783. {
  784. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  785. fc_channel *fc = FC_SCMND(SCpnt);
  786. FCD(("Entering SCSI queuecommand %p\n", fcmd))
  787. if (SCpnt->done != fcp_scsi_done) {
  788. fcmd->done = SCpnt->done;
  789. SCpnt->done = fcp_scsi_done;
  790. SCpnt->scsi_done = done;
  791. fcmd->proto = TYPE_SCSI_FCP;
  792. if (!fc->scsi_free) {
  793. FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
  794. #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
  795. printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
  796. #endif
  797. return 1;
  798. }
  799. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
  800. }
  801. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
  802. }
  803. void fcp_queue_empty(fc_channel *fc)
  804. {
  805. fcp_cmnd *fcmd;
  806. FCD(("Queue empty\n"))
  807. while ((fcmd = fc->scsi_que)) {
  808. /* The hw told us we can try again queue some packet */
  809. if (fc->hw_enque (fc, fcmd))
  810. break;
  811. fcp_scsi_remove_queue (fc, fcmd);
  812. }
  813. }
  814. int fcp_scsi_abort(Scsi_Cmnd *SCpnt)
  815. {
  816. /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
  817. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  818. fc_channel *fc = FC_SCMND(SCpnt);
  819. /*
  820. * We react to abort requests by simply forgetting
  821. * about the command and pretending everything's sweet.
  822. * This may or may not be silly. We can't, however,
  823. * immediately reuse the command's cmd_slots slot,
  824. * as its result may arrive later and we cannot
  825. * check whether it is the aborted one, can't we?
  826. *
  827. * Therefore, after the first few aborts are done,
  828. * we tell the scsi error handler to do something clever.
  829. * It will eventually call host reset, refreshing
  830. * cmd_slots for us.
  831. *
  832. * There is a theoretical chance that we sometimes allow
  833. * more than can_queue packets to the jungle this way,
  834. * but the worst outcome possible is a series of
  835. * more aborts and eventually the dev_reset catharsis.
  836. */
  837. if (++fc->abort_count < (fc->can_queue >> 1)) {
  838. SCpnt->result = DID_ABORT;
  839. fcmd->done(SCpnt);
  840. printk("FC: soft abort\n");
  841. return SUCCESS;
  842. } else {
  843. printk("FC: hard abort refused\n");
  844. return FAILED;
  845. }
  846. }
  847. void fcp_scsi_reset_done(Scsi_Cmnd *SCpnt)
  848. {
  849. fc_channel *fc = FC_SCMND(SCpnt);
  850. fc->rst_pkt->eh_state = SCSI_STATE_FINISHED;
  851. up(fc->rst_pkt->device->host->eh_action);
  852. }
  853. #define FCP_RESET_TIMEOUT (2*HZ)
  854. int fcp_scsi_dev_reset(Scsi_Cmnd *SCpnt)
  855. {
  856. unsigned long flags;
  857. fcp_cmd *cmd;
  858. fcp_cmnd *fcmd;
  859. fc_channel *fc = FC_SCMND(SCpnt);
  860. DECLARE_MUTEX_LOCKED(sem);
  861. if (!fc->rst_pkt) {
  862. fc->rst_pkt = (Scsi_Cmnd *) kmalloc(sizeof(SCpnt), GFP_KERNEL);
  863. if (!fc->rst_pkt) return FAILED;
  864. fcmd = FCP_CMND(fc->rst_pkt);
  865. fcmd->token = 0;
  866. cmd = fc->scsi_cmd_pool + 0;
  867. FCD(("Preparing rst packet\n"))
  868. fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd);
  869. fc->rst_pkt->device = SCpnt->device;
  870. fc->rst_pkt->cmd_len = 0;
  871. fc->cmd_slots[0] = fcmd;
  872. cmd->fcp_cntl = FCP_CNTL_QTYPE_ORDERED | FCP_CNTL_RESET;
  873. fcmd->data = (dma_addr_t)NULL;
  874. fcmd->proto = TYPE_SCSI_FCP;
  875. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  876. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  877. FCD(("XXX: %04x.%04x.%04x.%04x - %08x%08x%08x\n", cmd->fcp_addr[0], cmd->fcp_addr[1], cmd->fcp_addr[2], cmd->fcp_addr[3], *(u32 *)SCpnt->cmnd, *(u32 *)(SCpnt->cmnd+4), *(u32 *)(SCpnt->cmnd+8)))
  878. } else {
  879. fcmd = FCP_CMND(fc->rst_pkt);
  880. if (fc->rst_pkt->eh_state == SCSI_STATE_QUEUED)
  881. return FAILED; /* or SUCCESS. Only these */
  882. }
  883. fc->rst_pkt->done = NULL;
  884. fc->rst_pkt->eh_state = SCSI_STATE_QUEUED;
  885. init_timer(&fc->rst_pkt->eh_timeout);
  886. fc->rst_pkt->eh_timeout.data = (unsigned long) fc->rst_pkt;
  887. fc->rst_pkt->eh_timeout.expires = jiffies + FCP_RESET_TIMEOUT;
  888. fc->rst_pkt->eh_timeout.function = (void (*)(unsigned long))fcp_scsi_reset_done;
  889. add_timer(&fc->rst_pkt->eh_timeout);
  890. /*
  891. * Set up the semaphore so we wait for the command to complete.
  892. */
  893. fc->rst_pkt->device->host->eh_action = &sem;
  894. fc->rst_pkt->request->rq_status = RQ_SCSI_BUSY;
  895. fc->rst_pkt->done = fcp_scsi_reset_done;
  896. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  897. fcp_scsi_queue_it(fc, fc->rst_pkt, fcmd, 0);
  898. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  899. down(&sem);
  900. fc->rst_pkt->device->host->eh_action = NULL;
  901. del_timer(&fc->rst_pkt->eh_timeout);
  902. /*
  903. * See if timeout. If so, tell the host to forget about it.
  904. * In other words, we don't want a callback any more.
  905. */
  906. if (fc->rst_pkt->eh_state == SCSI_STATE_TIMEOUT ) {
  907. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  908. return FAILED;
  909. }
  910. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  911. return SUCCESS;
  912. }
  913. static int __fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  914. {
  915. fc_channel *fc = FC_SCMND(SCpnt);
  916. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  917. int i;
  918. printk ("FC: host reset\n");
  919. for (i=0; i < fc->can_queue; i++) {
  920. if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
  921. SCpnt->result = DID_RESET;
  922. fcmd->done(SCpnt);
  923. fc->cmd_slots[i] = NULL;
  924. }
  925. }
  926. fc->reset(fc);
  927. fc->abort_count = 0;
  928. if (fcp_initialize(fc, 1)) return SUCCESS;
  929. else return FAILED;
  930. }
  931. int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  932. {
  933. unsigned long flags;
  934. int rc;
  935. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  936. rc = __fcp_scsi_host_reset(SCpnt);
  937. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  938. return rc;
  939. }
  940. static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
  941. {
  942. long i;
  943. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  944. set_bit (i, fc->scsi_bitmap);
  945. fcmd->token = i;
  946. fc->scsi_free--;
  947. fc->cmd_slots[fcmd->token] = fcmd;
  948. return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  949. }
  950. static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
  951. {
  952. fcp_cmnd _fcmd, *fcmd;
  953. fc_hdr *fch;
  954. lse l;
  955. int i;
  956. fcmd = &_fcmd;
  957. memset(fcmd, 0, sizeof(fcmd));
  958. FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
  959. fch = &fcmd->fch;
  960. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
  961. FILL_FCHDR_SID(fch, fc->sid);
  962. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  963. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  964. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  965. fch->param = 0;
  966. fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
  967. fcmd->rsp = fcmd->cmd + len;
  968. fcmd->cmdlen = len;
  969. fcmd->rsplen = len;
  970. fcmd->data = (dma_addr_t)NULL;
  971. fcmd->fc = fc;
  972. fcmd->class = FC_CLASS_SIMPLE;
  973. fcmd->proto = TYPE_EXTENDED_LS;
  974. memset (&l, 0, sizeof(lse));
  975. l.magic = LSEMAGIC;
  976. init_MUTEX_LOCKED(&l.sem);
  977. l.timer.function = fcp_login_timeout;
  978. l.timer.data = (unsigned long)&l;
  979. l.status = FC_STATUS_TIMED_OUT;
  980. fcmd->ls = (void *)&l;
  981. disable_irq(fc->irq);
  982. fcp_els_queue_it(fc, fcmd);
  983. enable_irq(fc->irq);
  984. for (i = 0;;) {
  985. l.timer.expires = jiffies + 5 * HZ;
  986. add_timer(&l.timer);
  987. down(&l.sem);
  988. del_timer(&l.timer);
  989. if (l.status != FC_STATUS_TIMED_OUT) break;
  990. if (++i == 3) break;
  991. disable_irq(fc->irq);
  992. fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  993. enable_irq(fc->irq);
  994. }
  995. clear_bit(fcmd->token, fc->scsi_bitmap);
  996. fc->scsi_free++;
  997. dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
  998. return l.status;
  999. }
  1000. int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
  1001. {
  1002. logi *l;
  1003. int status;
  1004. l = (logi *)kmalloc(2 * sizeof(logi), GFP_KERNEL);
  1005. if (!l) return -ENOMEM;
  1006. memset(l, 0, 2 * sizeof(logi));
  1007. l->code = LS_PLOGI;
  1008. memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
  1009. memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
  1010. memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
  1011. memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
  1012. status = fc_do_els(fc, alpa, l, sizeof(logi));
  1013. if (status == FC_STATUS_OK) {
  1014. if (l[1].code == LS_ACC) {
  1015. #ifdef FCDEBUG
  1016. u32 *u = (u32 *)&l[1].nport_wwn;
  1017. FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa,
  1018. u[0], u[1], u[2], u[3]))
  1019. #endif
  1020. memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
  1021. memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
  1022. } else
  1023. status = FC_STATUS_BAD_RSP;
  1024. }
  1025. kfree(l);
  1026. return status;
  1027. }
  1028. typedef struct {
  1029. unsigned int code;
  1030. unsigned params[4];
  1031. } prli;
  1032. int fc_do_prli(fc_channel *fc, unsigned char alpa)
  1033. {
  1034. prli *p;
  1035. int status;
  1036. p = (prli *)kmalloc(2 * sizeof(prli), GFP_KERNEL);
  1037. if (!p) return -ENOMEM;
  1038. memset(p, 0, 2 * sizeof(prli));
  1039. p->code = LS_PRLI;
  1040. p->params[0] = 0x08002000;
  1041. p->params[3] = 0x00000022;
  1042. status = fc_do_els(fc, alpa, p, sizeof(prli));
  1043. if (status == FC_STATUS_OK && p[1].code != LS_PRLI_ACC && p[1].code != LS_ACC)
  1044. status = FC_STATUS_BAD_RSP;
  1045. kfree(p);
  1046. return status;
  1047. }
  1048. MODULE_LICENSE("GPL");