fc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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 0
  347. if (fc->rst_pkt) {
  348. if (fc->rst_pkt->eh_state == SCSI_STATE_UNUSED)
  349. kfree(fc->rst_pkt);
  350. else {
  351. /* Can't happen. Some memory would be lost. */
  352. printk("FC: Reset in progress. Now?!");
  353. }
  354. }
  355. #endif
  356. FCND(("Unregistered\n"));
  357. }
  358. } else
  359. printk ("FC: %segistering unknown type %02x\n", unregister ? "Unr" : "R", type);
  360. }
  361. static void fcp_scsi_done(Scsi_Cmnd *SCpnt);
  362. static inline void fcp_scsi_receive(fc_channel *fc, int token, int status, fc_hdr *fch)
  363. {
  364. fcp_cmnd *fcmd;
  365. fcp_rsp *rsp;
  366. int host_status;
  367. Scsi_Cmnd *SCpnt;
  368. int sense_len;
  369. int rsp_status;
  370. fcmd = fc->cmd_slots[token];
  371. if (!fcmd) return;
  372. rsp = (fcp_rsp *) (fc->scsi_rsp_pool + fc->rsp_size * token);
  373. SCpnt = SC_FCMND(fcmd);
  374. if (SCpnt->done != fcp_scsi_done)
  375. return;
  376. rsp_status = rsp->fcp_status;
  377. FCD(("rsp_status %08x status %08x\n", rsp_status, status))
  378. switch (status) {
  379. case FC_STATUS_OK:
  380. host_status=DID_OK;
  381. if (rsp_status & FCP_STATUS_RESID) {
  382. #ifdef FCDEBUG
  383. FCD(("Resid %d\n", rsp->fcp_resid))
  384. {
  385. fcp_cmd *cmd = fc->scsi_cmd_pool + token;
  386. int i;
  387. printk ("Command ");
  388. for (i = 0; i < sizeof(fcp_cmd); i+=4)
  389. printk ("%08x ", *(u32 *)(((char *)cmd)+i));
  390. printk ("\nResponse ");
  391. for (i = 0; i < fc->rsp_size; i+=4)
  392. printk ("%08x ", *(u32 *)(((char *)rsp)+i));
  393. printk ("\n");
  394. }
  395. #endif
  396. }
  397. if (rsp_status & FCP_STATUS_SENSE_LEN) {
  398. sense_len = rsp->fcp_sense_len;
  399. if (sense_len > sizeof(SCpnt->sense_buffer)) sense_len = sizeof(SCpnt->sense_buffer);
  400. memcpy(SCpnt->sense_buffer, ((char *)(rsp+1)), sense_len);
  401. }
  402. if (fcmd->data) {
  403. if (SCpnt->use_sg)
  404. dma_unmap_sg(fc->dev, (struct scatterlist *)SCpnt->buffer,
  405. SCpnt->use_sg,
  406. SCpnt->sc_data_direction);
  407. else
  408. dma_unmap_single(fc->dev, fcmd->data, SCpnt->request_bufflen,
  409. SCpnt->sc_data_direction);
  410. }
  411. break;
  412. default:
  413. host_status=DID_ERROR; /* FIXME */
  414. FCD(("Wrong FC status %d for token %d\n", status, token))
  415. break;
  416. }
  417. if (status_byte(rsp_status) == QUEUE_FULL) {
  418. printk ("%s: (%d,%d) Received rsp_status 0x%x\n", fc->name, SCpnt->device->channel, SCpnt->device->id, rsp_status);
  419. }
  420. SCpnt->result = (host_status << 16) | (rsp_status & 0xff);
  421. #ifdef FCDEBUG
  422. if (host_status || SCpnt->result || rsp_status) printk("FC: host_status %d, packet status %d\n",
  423. host_status, SCpnt->result);
  424. #endif
  425. SCpnt->done = fcmd->done;
  426. fcmd->done=NULL;
  427. clear_bit(token, fc->scsi_bitmap);
  428. fc->scsi_free++;
  429. FCD(("Calling scsi_done with %08x\n", SCpnt->result))
  430. SCpnt->scsi_done(SCpnt);
  431. }
  432. void fcp_receive_solicited(fc_channel *fc, int proto, int token, int status, fc_hdr *fch)
  433. {
  434. int magic;
  435. FCD(("receive_solicited %d %d %d\n", proto, token, status))
  436. switch (proto) {
  437. case TYPE_SCSI_FCP:
  438. fcp_scsi_receive(fc, token, status, fch); break;
  439. case TYPE_EXTENDED_LS:
  440. case PROTO_REPORT_AL_MAP:
  441. magic = 0;
  442. if (fc->ls)
  443. magic = ((ls *)(fc->ls))->magic;
  444. if (magic == LSMAGIC) {
  445. ls *l = (ls *)fc->ls;
  446. int i = (token >= l->count) ? token - l->count : token;
  447. /* Let's be sure */
  448. if ((unsigned)i < l->count && l->fcmds[i].fc == fc) {
  449. if (proto == TYPE_EXTENDED_LS)
  450. fcp_login_done(fc, token, status);
  451. else
  452. fcp_report_map_done(fc, token, status);
  453. break;
  454. }
  455. }
  456. FCD(("fc %p fc->ls %p fc->cmd_slots %p\n", fc, fc->ls, fc->cmd_slots))
  457. if (proto == TYPE_EXTENDED_LS && !fc->ls && fc->cmd_slots) {
  458. fcp_cmnd *fcmd;
  459. fcmd = fc->cmd_slots[token];
  460. if (fcmd && fcmd->ls && ((ls *)(fcmd->ls))->magic == LSEMAGIC) {
  461. lse *l = (lse *)fcmd->ls;
  462. l->status = status;
  463. up (&l->sem);
  464. }
  465. }
  466. break;
  467. case PROTO_OFFLINE:
  468. if (fc->ls && ((lso *)(fc->ls))->magic == LSOMAGIC) {
  469. lso *l = (lso *)fc->ls;
  470. if ((unsigned)token < l->count && l->fcmds[token].fc == fc) {
  471. /* Wow, OFFLINE response arrived :) */
  472. FCD(("OFFLINE Response arrived\n"))
  473. fc->state = FC_STATE_OFFLINE;
  474. if (atomic_dec_and_test (&l->todo))
  475. up(&l->sem);
  476. }
  477. }
  478. break;
  479. default:
  480. break;
  481. }
  482. }
  483. void fcp_state_change(fc_channel *fc, int state)
  484. {
  485. FCD(("state_change %d %d\n", state, fc->state))
  486. if (state == FC_STATE_ONLINE && fc->state == FC_STATE_MAYBEOFFLINE)
  487. fc->state = FC_STATE_UNINITED;
  488. else if (state == FC_STATE_ONLINE)
  489. printk (KERN_WARNING "%s: state change to ONLINE\n", fc->name);
  490. else
  491. printk (KERN_ERR "%s: state change to OFFLINE\n", fc->name);
  492. }
  493. int fcp_initialize(fc_channel *fcchain, int count)
  494. {
  495. fc_channel *fc;
  496. fcp_cmnd *fcmd;
  497. int i, retry, ret;
  498. ls *l;
  499. FCND(("fcp_inititialize %08lx\n", (long)fcp_init))
  500. FCND(("fc_channels %08lx\n", (long)fc_channels))
  501. FCND((" SID %d DID %d\n", fcchain->sid, fcchain->did))
  502. l = kmalloc(sizeof (ls) + count, GFP_KERNEL);
  503. if (!l) {
  504. printk ("FC: Cannot allocate memory for initialization\n");
  505. return -ENOMEM;
  506. }
  507. memset (l, 0, sizeof(ls) + count);
  508. l->magic = LSMAGIC;
  509. l->count = count;
  510. FCND(("FCP Init for %d channels\n", count))
  511. init_MUTEX_LOCKED(&l->sem);
  512. init_timer(&l->timer);
  513. l->timer.function = fcp_login_timeout;
  514. l->timer.data = (unsigned long)l;
  515. atomic_set (&l->todo, count);
  516. l->logi = kmalloc (count * 3 * sizeof(logi), GFP_KERNEL);
  517. l->fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  518. if (!l->logi || !l->fcmds) {
  519. if (l->logi) kfree (l->logi);
  520. if (l->fcmds) kfree (l->fcmds);
  521. kfree (l);
  522. printk ("FC: Cannot allocate DMA memory for initialization\n");
  523. return -ENOMEM;
  524. }
  525. memset (l->logi, 0, count * 3 * sizeof(logi));
  526. memset (l->fcmds, 0, count * sizeof(fcp_cmnd));
  527. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  528. fc->state = FC_STATE_UNINITED;
  529. fc->rst_pkt = NULL; /* kmalloc when first used */
  530. }
  531. /* First try if we are in a AL topology */
  532. FCND(("Initializing REPORT_MAP packets\n"))
  533. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  534. fcmd = l->fcmds + i;
  535. fc->login = fcmd;
  536. fc->ls = (void *)l;
  537. /* Assumes sizeof(fc_al_posmap) < 3 * sizeof(logi), which is true */
  538. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi),
  539. DMA_BIDIRECTIONAL);
  540. fcmd->proto = PROTO_REPORT_AL_MAP;
  541. fcmd->token = i;
  542. fcmd->fc = fc;
  543. }
  544. for (retry = 0; retry < 8; retry++) {
  545. int nqueued = 0;
  546. FCND(("Sending REPORT_MAP/FLOGI/PLOGI packets\n"))
  547. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  548. if (fc->state == FC_STATE_ONLINE || fc->state == FC_STATE_OFFLINE)
  549. continue;
  550. disable_irq(fc->irq);
  551. if (fc->state == FC_STATE_MAYBEOFFLINE) {
  552. if (!l->grace[i]) {
  553. l->grace[i]++;
  554. FCD(("Grace\n"))
  555. } else {
  556. fc->state = FC_STATE_OFFLINE;
  557. enable_irq(fc->irq);
  558. dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  559. if (atomic_dec_and_test (&l->todo))
  560. goto all_done;
  561. }
  562. }
  563. ret = fc->hw_enque (fc, fc->login);
  564. enable_irq(fc->irq);
  565. if (!ret) {
  566. nqueued++;
  567. continue;
  568. }
  569. if (ret == -ENOSYS && fc->login->proto == PROTO_REPORT_AL_MAP) {
  570. /* Oh yes, this card handles Point-to-Point only, so let's try that. */
  571. fc_hdr *fch;
  572. FCD(("SID %d DID %d\n", fc->sid, fc->did))
  573. fcmd = l->fcmds + i;
  574. dma_unmap_single(fc->dev, fcmd->cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  575. fch = &fcmd->fch;
  576. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, FS_FABRIC_F_PORT);
  577. FILL_FCHDR_SID(fch, 0);
  578. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  579. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  580. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  581. fch->param = 0;
  582. l->logi [3 * i].code = LS_FLOGI;
  583. fcmd->cmd = dma_map_single (fc->dev, l->logi + 3 * i, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  584. fcmd->rsp = fcmd->cmd + sizeof(logi);
  585. fcmd->cmdlen = sizeof(logi);
  586. fcmd->rsplen = sizeof(logi);
  587. fcmd->data = (dma_addr_t)NULL;
  588. fcmd->class = FC_CLASS_SIMPLE;
  589. fcmd->proto = TYPE_EXTENDED_LS;
  590. } else
  591. printk ("FC: Cannot enque FLOGI/REPORT_MAP packet on %s\n", fc->name);
  592. }
  593. if (nqueued) {
  594. l->timer.expires = jiffies + 5 * HZ;
  595. add_timer(&l->timer);
  596. down(&l->sem);
  597. if (!atomic_read(&l->todo)) {
  598. FCND(("All channels answered in time\n"))
  599. break; /* All fc channels have answered us */
  600. }
  601. }
  602. }
  603. all_done:
  604. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  605. fc->ls = NULL;
  606. switch (fc->state) {
  607. case FC_STATE_ONLINE: break;
  608. case FC_STATE_OFFLINE: break;
  609. default: dma_unmap_single (fc->dev, l->fcmds[i].cmd, 3 * sizeof(logi), DMA_BIDIRECTIONAL);
  610. break;
  611. }
  612. }
  613. del_timer(&l->timer);
  614. kfree (l->logi);
  615. kfree (l->fcmds);
  616. kfree (l);
  617. return 0;
  618. }
  619. int fcp_forceoffline(fc_channel *fcchain, int count)
  620. {
  621. fc_channel *fc;
  622. fcp_cmnd *fcmd;
  623. int i, ret;
  624. lso l;
  625. memset (&l, 0, sizeof(lso));
  626. l.count = count;
  627. l.magic = LSOMAGIC;
  628. FCND(("FCP Force Offline for %d channels\n", count))
  629. init_MUTEX_LOCKED(&l.sem);
  630. init_timer(&l.timer);
  631. l.timer.function = fcp_login_timeout;
  632. l.timer.data = (unsigned long)&l;
  633. atomic_set (&l.todo, count);
  634. l.fcmds = kmalloc (count * sizeof(fcp_cmnd), GFP_KERNEL);
  635. if (!l.fcmds) {
  636. kfree (l.fcmds);
  637. printk ("FC: Cannot allocate memory for forcing offline\n");
  638. return -ENOMEM;
  639. }
  640. memset (l.fcmds, 0, count * sizeof(fcp_cmnd));
  641. FCND(("Initializing OFFLINE packets\n"))
  642. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++) {
  643. fc->state = FC_STATE_UNINITED;
  644. fcmd = l.fcmds + i;
  645. fc->login = fcmd;
  646. fc->ls = (void *)&l;
  647. fcmd->did = fc->did;
  648. fcmd->class = FC_CLASS_OFFLINE;
  649. fcmd->proto = PROTO_OFFLINE;
  650. fcmd->token = i;
  651. fcmd->fc = fc;
  652. disable_irq(fc->irq);
  653. ret = fc->hw_enque (fc, fc->login);
  654. enable_irq(fc->irq);
  655. if (ret) printk ("FC: Cannot enque OFFLINE packet on %s\n", fc->name);
  656. }
  657. l.timer.expires = jiffies + 5 * HZ;
  658. add_timer(&l.timer);
  659. down(&l.sem);
  660. del_timer(&l.timer);
  661. for (fc = fcchain, i = 0; fc && i < count; fc = fc->next, i++)
  662. fc->ls = NULL;
  663. kfree (l.fcmds);
  664. return 0;
  665. }
  666. int fcp_init(fc_channel *fcchain)
  667. {
  668. fc_channel *fc;
  669. int count=0;
  670. int ret;
  671. for (fc = fcchain; fc; fc = fc->next) {
  672. fc->fcp_register = fcp_register;
  673. count++;
  674. }
  675. ret = fcp_initialize (fcchain, count);
  676. if (ret)
  677. return ret;
  678. if (!fc_channels)
  679. fc_channels = fcchain;
  680. else {
  681. for (fc = fc_channels; fc->next; fc = fc->next);
  682. fc->next = fcchain;
  683. }
  684. return ret;
  685. }
  686. void fcp_release(fc_channel *fcchain, int count) /* count must > 0 */
  687. {
  688. fc_channel *fc;
  689. fc_channel *fcx;
  690. for (fc = fcchain; --count && fc->next; fc = fc->next);
  691. if (count) {
  692. printk("FC: nothing to release\n");
  693. return;
  694. }
  695. if (fc_channels == fcchain)
  696. fc_channels = fc->next;
  697. else {
  698. for (fcx = fc_channels; fcx->next != fcchain; fcx = fcx->next);
  699. fcx->next = fc->next;
  700. }
  701. fc->next = NULL;
  702. /*
  703. * We've just grabbed fcchain out of the fc_channel list
  704. * and zero-terminated it, while destroying the count.
  705. *
  706. * Freeing the fc's is the low level driver's responsibility.
  707. */
  708. }
  709. static void fcp_scsi_done (Scsi_Cmnd *SCpnt)
  710. {
  711. if (FCP_CMND(SCpnt)->done)
  712. FCP_CMND(SCpnt)->done(SCpnt);
  713. }
  714. static int fcp_scsi_queue_it(fc_channel *fc, Scsi_Cmnd *SCpnt, fcp_cmnd *fcmd, int prepare)
  715. {
  716. long i;
  717. fcp_cmd *cmd;
  718. u32 fcp_cntl;
  719. if (prepare) {
  720. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  721. set_bit (i, fc->scsi_bitmap);
  722. fcmd->token = i;
  723. cmd = fc->scsi_cmd_pool + i;
  724. if (fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd)) {
  725. /* Invalid channel/id/lun and couldn't map it into fcp_addr */
  726. clear_bit (i, fc->scsi_bitmap);
  727. SCpnt->result = (DID_BAD_TARGET << 16);
  728. SCpnt->scsi_done(SCpnt);
  729. return 0;
  730. }
  731. fc->scsi_free--;
  732. fc->cmd_slots[fcmd->token] = fcmd;
  733. if (SCpnt->device->tagged_supported) {
  734. if (jiffies - fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] > (5 * 60 * HZ)) {
  735. fc->ages[SCpnt->device->channel * fc->targets + SCpnt->device->id] = jiffies;
  736. fcp_cntl = FCP_CNTL_QTYPE_ORDERED;
  737. } else
  738. fcp_cntl = FCP_CNTL_QTYPE_SIMPLE;
  739. } else
  740. fcp_cntl = FCP_CNTL_QTYPE_UNTAGGED;
  741. if (!SCpnt->request_bufflen && !SCpnt->use_sg) {
  742. cmd->fcp_cntl = fcp_cntl;
  743. fcmd->data = (dma_addr_t)NULL;
  744. } else {
  745. switch (SCpnt->cmnd[0]) {
  746. case WRITE_6:
  747. case WRITE_10:
  748. case WRITE_12:
  749. cmd->fcp_cntl = (FCP_CNTL_WRITE | fcp_cntl); break;
  750. default:
  751. cmd->fcp_cntl = (FCP_CNTL_READ | fcp_cntl); break;
  752. }
  753. if (!SCpnt->use_sg) {
  754. cmd->fcp_data_len = SCpnt->request_bufflen;
  755. fcmd->data = dma_map_single (fc->dev, (char *)SCpnt->request_buffer,
  756. SCpnt->request_bufflen,
  757. SCpnt->sc_data_direction);
  758. } else {
  759. struct scatterlist *sg = (struct scatterlist *)SCpnt->buffer;
  760. int nents;
  761. FCD(("XXX: Use_sg %d %d\n", SCpnt->use_sg, sg->length))
  762. nents = dma_map_sg (fc->dev, sg, SCpnt->use_sg,
  763. SCpnt->sc_data_direction);
  764. if (nents > 1) printk ("%s: SG for nents %d (use_sg %d) not handled yet\n", fc->name, nents, SCpnt->use_sg);
  765. fcmd->data = sg_dma_address(sg);
  766. cmd->fcp_data_len = sg_dma_len(sg);
  767. }
  768. }
  769. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  770. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  771. 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)))
  772. }
  773. FCD(("Trying to enque %p\n", fcmd))
  774. if (!fc->scsi_que) {
  775. if (!fc->hw_enque (fc, fcmd)) {
  776. FCD(("hw_enque succeeded for %p\n", fcmd))
  777. return 0;
  778. }
  779. }
  780. FCD(("Putting into que1 %p\n", fcmd))
  781. fcp_scsi_insert_queue (fc, fcmd);
  782. return 0;
  783. }
  784. int fcp_scsi_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *))
  785. {
  786. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  787. fc_channel *fc = FC_SCMND(SCpnt);
  788. FCD(("Entering SCSI queuecommand %p\n", fcmd))
  789. if (SCpnt->done != fcp_scsi_done) {
  790. fcmd->done = SCpnt->done;
  791. SCpnt->done = fcp_scsi_done;
  792. SCpnt->scsi_done = done;
  793. fcmd->proto = TYPE_SCSI_FCP;
  794. if (!fc->scsi_free) {
  795. FCD(("FC: !scsi_free, putting cmd on ML queue\n"))
  796. #if (FCP_SCSI_USE_NEW_EH_CODE == 0)
  797. printk("fcp_scsi_queue_command: queue full, losing cmd, bad\n");
  798. #endif
  799. return 1;
  800. }
  801. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 1);
  802. }
  803. return fcp_scsi_queue_it(fc, SCpnt, fcmd, 0);
  804. }
  805. void fcp_queue_empty(fc_channel *fc)
  806. {
  807. fcp_cmnd *fcmd;
  808. FCD(("Queue empty\n"))
  809. while ((fcmd = fc->scsi_que)) {
  810. /* The hw told us we can try again queue some packet */
  811. if (fc->hw_enque (fc, fcmd))
  812. break;
  813. fcp_scsi_remove_queue (fc, fcmd);
  814. }
  815. }
  816. int fcp_scsi_abort(Scsi_Cmnd *SCpnt)
  817. {
  818. /* Internal bookkeeping only. Lose 1 cmd_slots slot. */
  819. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  820. fc_channel *fc = FC_SCMND(SCpnt);
  821. /*
  822. * We react to abort requests by simply forgetting
  823. * about the command and pretending everything's sweet.
  824. * This may or may not be silly. We can't, however,
  825. * immediately reuse the command's cmd_slots slot,
  826. * as its result may arrive later and we cannot
  827. * check whether it is the aborted one, can't we?
  828. *
  829. * Therefore, after the first few aborts are done,
  830. * we tell the scsi error handler to do something clever.
  831. * It will eventually call host reset, refreshing
  832. * cmd_slots for us.
  833. *
  834. * There is a theoretical chance that we sometimes allow
  835. * more than can_queue packets to the jungle this way,
  836. * but the worst outcome possible is a series of
  837. * more aborts and eventually the dev_reset catharsis.
  838. */
  839. if (++fc->abort_count < (fc->can_queue >> 1)) {
  840. SCpnt->result = DID_ABORT;
  841. fcmd->done(SCpnt);
  842. printk("FC: soft abort\n");
  843. return SUCCESS;
  844. } else {
  845. printk("FC: hard abort refused\n");
  846. return FAILED;
  847. }
  848. }
  849. #if 0
  850. void fcp_scsi_reset_done(Scsi_Cmnd *SCpnt)
  851. {
  852. fc_channel *fc = FC_SCMND(SCpnt);
  853. fc->rst_pkt->eh_state = SCSI_STATE_FINISHED;
  854. up(fc->rst_pkt->device->host->eh_action);
  855. }
  856. #endif
  857. #define FCP_RESET_TIMEOUT (2*HZ)
  858. int fcp_scsi_dev_reset(Scsi_Cmnd *SCpnt)
  859. {
  860. #if 0 /* broken junk, but if davem wants to compile this driver, let him.. */
  861. unsigned long flags;
  862. fcp_cmd *cmd;
  863. fcp_cmnd *fcmd;
  864. fc_channel *fc = FC_SCMND(SCpnt);
  865. DECLARE_MUTEX_LOCKED(sem);
  866. if (!fc->rst_pkt) {
  867. fc->rst_pkt = (Scsi_Cmnd *) kmalloc(sizeof(SCpnt), GFP_KERNEL);
  868. if (!fc->rst_pkt) return FAILED;
  869. fcmd = FCP_CMND(fc->rst_pkt);
  870. fcmd->token = 0;
  871. cmd = fc->scsi_cmd_pool + 0;
  872. FCD(("Preparing rst packet\n"))
  873. fc->encode_addr (SCpnt, cmd->fcp_addr, fc, fcmd);
  874. fc->rst_pkt->device = SCpnt->device;
  875. fc->rst_pkt->cmd_len = 0;
  876. fc->cmd_slots[0] = fcmd;
  877. cmd->fcp_cntl = FCP_CNTL_QTYPE_ORDERED | FCP_CNTL_RESET;
  878. fcmd->data = (dma_addr_t)NULL;
  879. fcmd->proto = TYPE_SCSI_FCP;
  880. memcpy (cmd->fcp_cdb, SCpnt->cmnd, SCpnt->cmd_len);
  881. memset (cmd->fcp_cdb+SCpnt->cmd_len, 0, sizeof(cmd->fcp_cdb)-SCpnt->cmd_len);
  882. 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)))
  883. } else {
  884. fcmd = FCP_CMND(fc->rst_pkt);
  885. if (fc->rst_pkt->eh_state == SCSI_STATE_QUEUED)
  886. return FAILED; /* or SUCCESS. Only these */
  887. }
  888. fc->rst_pkt->done = NULL;
  889. fc->rst_pkt->eh_state = SCSI_STATE_QUEUED;
  890. init_timer(&fc->rst_pkt->eh_timeout);
  891. fc->rst_pkt->eh_timeout.data = (unsigned long) fc->rst_pkt;
  892. fc->rst_pkt->eh_timeout.expires = jiffies + FCP_RESET_TIMEOUT;
  893. fc->rst_pkt->eh_timeout.function = (void (*)(unsigned long))fcp_scsi_reset_done;
  894. add_timer(&fc->rst_pkt->eh_timeout);
  895. /*
  896. * Set up the semaphore so we wait for the command to complete.
  897. */
  898. fc->rst_pkt->device->host->eh_action = &sem;
  899. fc->rst_pkt->request->rq_status = RQ_SCSI_BUSY;
  900. fc->rst_pkt->done = fcp_scsi_reset_done;
  901. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  902. fcp_scsi_queue_it(fc, fc->rst_pkt, fcmd, 0);
  903. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  904. down(&sem);
  905. fc->rst_pkt->device->host->eh_action = NULL;
  906. del_timer(&fc->rst_pkt->eh_timeout);
  907. /*
  908. * See if timeout. If so, tell the host to forget about it.
  909. * In other words, we don't want a callback any more.
  910. */
  911. if (fc->rst_pkt->eh_state == SCSI_STATE_TIMEOUT ) {
  912. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  913. return FAILED;
  914. }
  915. fc->rst_pkt->eh_state = SCSI_STATE_UNUSED;
  916. #endif
  917. return SUCCESS;
  918. }
  919. static int __fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  920. {
  921. fc_channel *fc = FC_SCMND(SCpnt);
  922. fcp_cmnd *fcmd = FCP_CMND(SCpnt);
  923. int i;
  924. printk ("FC: host reset\n");
  925. for (i=0; i < fc->can_queue; i++) {
  926. if (fc->cmd_slots[i] && SCpnt->result != DID_ABORT) {
  927. SCpnt->result = DID_RESET;
  928. fcmd->done(SCpnt);
  929. fc->cmd_slots[i] = NULL;
  930. }
  931. }
  932. fc->reset(fc);
  933. fc->abort_count = 0;
  934. if (fcp_initialize(fc, 1)) return SUCCESS;
  935. else return FAILED;
  936. }
  937. int fcp_scsi_host_reset(Scsi_Cmnd *SCpnt)
  938. {
  939. unsigned long flags;
  940. int rc;
  941. spin_lock_irqsave(SCpnt->device->host->host_lock, flags);
  942. rc = __fcp_scsi_host_reset(SCpnt);
  943. spin_unlock_irqrestore(SCpnt->device->host->host_lock, flags);
  944. return rc;
  945. }
  946. static int fcp_els_queue_it(fc_channel *fc, fcp_cmnd *fcmd)
  947. {
  948. long i;
  949. i = find_first_zero_bit (fc->scsi_bitmap, fc->scsi_bitmap_end);
  950. set_bit (i, fc->scsi_bitmap);
  951. fcmd->token = i;
  952. fc->scsi_free--;
  953. fc->cmd_slots[fcmd->token] = fcmd;
  954. return fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  955. }
  956. static int fc_do_els(fc_channel *fc, unsigned int alpa, void *data, int len)
  957. {
  958. fcp_cmnd _fcmd, *fcmd;
  959. fc_hdr *fch;
  960. lse l;
  961. int i;
  962. fcmd = &_fcmd;
  963. memset(fcmd, 0, sizeof(fcmd));
  964. FCD(("PLOGI SID %d DID %d\n", fc->sid, alpa))
  965. fch = &fcmd->fch;
  966. FILL_FCHDR_RCTL_DID(fch, R_CTL_ELS_REQ, alpa);
  967. FILL_FCHDR_SID(fch, fc->sid);
  968. FILL_FCHDR_TYPE_FCTL(fch, TYPE_EXTENDED_LS, F_CTL_FIRST_SEQ | F_CTL_SEQ_INITIATIVE);
  969. FILL_FCHDR_SEQ_DF_SEQ(fch, 0, 0, 0);
  970. FILL_FCHDR_OXRX(fch, 0xffff, 0xffff);
  971. fch->param = 0;
  972. fcmd->cmd = dma_map_single (fc->dev, data, 2 * len, DMA_BIDIRECTIONAL);
  973. fcmd->rsp = fcmd->cmd + len;
  974. fcmd->cmdlen = len;
  975. fcmd->rsplen = len;
  976. fcmd->data = (dma_addr_t)NULL;
  977. fcmd->fc = fc;
  978. fcmd->class = FC_CLASS_SIMPLE;
  979. fcmd->proto = TYPE_EXTENDED_LS;
  980. memset (&l, 0, sizeof(lse));
  981. l.magic = LSEMAGIC;
  982. init_MUTEX_LOCKED(&l.sem);
  983. l.timer.function = fcp_login_timeout;
  984. l.timer.data = (unsigned long)&l;
  985. l.status = FC_STATUS_TIMED_OUT;
  986. fcmd->ls = (void *)&l;
  987. disable_irq(fc->irq);
  988. fcp_els_queue_it(fc, fcmd);
  989. enable_irq(fc->irq);
  990. for (i = 0;;) {
  991. l.timer.expires = jiffies + 5 * HZ;
  992. add_timer(&l.timer);
  993. down(&l.sem);
  994. del_timer(&l.timer);
  995. if (l.status != FC_STATUS_TIMED_OUT) break;
  996. if (++i == 3) break;
  997. disable_irq(fc->irq);
  998. fcp_scsi_queue_it(fc, NULL, fcmd, 0);
  999. enable_irq(fc->irq);
  1000. }
  1001. clear_bit(fcmd->token, fc->scsi_bitmap);
  1002. fc->scsi_free++;
  1003. dma_unmap_single (fc->dev, fcmd->cmd, 2 * len, DMA_BIDIRECTIONAL);
  1004. return l.status;
  1005. }
  1006. int fc_do_plogi(fc_channel *fc, unsigned char alpa, fc_wwn *node, fc_wwn *nport)
  1007. {
  1008. logi *l;
  1009. int status;
  1010. l = (logi *)kmalloc(2 * sizeof(logi), GFP_KERNEL);
  1011. if (!l) return -ENOMEM;
  1012. memset(l, 0, 2 * sizeof(logi));
  1013. l->code = LS_PLOGI;
  1014. memcpy (&l->nport_wwn, &fc->wwn_nport, sizeof(fc_wwn));
  1015. memcpy (&l->node_wwn, &fc->wwn_node, sizeof(fc_wwn));
  1016. memcpy (&l->common, fc->common_svc, sizeof(common_svc_parm));
  1017. memcpy (&l->class1, fc->class_svcs, 3*sizeof(svc_parm));
  1018. status = fc_do_els(fc, alpa, l, sizeof(logi));
  1019. if (status == FC_STATUS_OK) {
  1020. if (l[1].code == LS_ACC) {
  1021. #ifdef FCDEBUG
  1022. u32 *u = (u32 *)&l[1].nport_wwn;
  1023. FCD(("AL-PA %02x: Port WWN %08x%08x Node WWN %08x%08x\n", alpa,
  1024. u[0], u[1], u[2], u[3]))
  1025. #endif
  1026. memcpy(nport, &l[1].nport_wwn, sizeof(fc_wwn));
  1027. memcpy(node, &l[1].node_wwn, sizeof(fc_wwn));
  1028. } else
  1029. status = FC_STATUS_BAD_RSP;
  1030. }
  1031. kfree(l);
  1032. return status;
  1033. }
  1034. typedef struct {
  1035. unsigned int code;
  1036. unsigned params[4];
  1037. } prli;
  1038. int fc_do_prli(fc_channel *fc, unsigned char alpa)
  1039. {
  1040. prli *p;
  1041. int status;
  1042. p = (prli *)kmalloc(2 * sizeof(prli), GFP_KERNEL);
  1043. if (!p) return -ENOMEM;
  1044. memset(p, 0, 2 * sizeof(prli));
  1045. p->code = LS_PRLI;
  1046. p->params[0] = 0x08002000;
  1047. p->params[3] = 0x00000022;
  1048. status = fc_do_els(fc, alpa, p, sizeof(prli));
  1049. if (status == FC_STATUS_OK && p[1].code != LS_PRLI_ACC && p[1].code != LS_ACC)
  1050. status = FC_STATUS_BAD_RSP;
  1051. kfree(p);
  1052. return status;
  1053. }
  1054. MODULE_LICENSE("GPL");