fc_fcp.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  4. * Copyright(c) 2008 Mike Christie
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Maintained at www.Open-FCoE.org
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/err.h>
  28. #include <linux/crc32.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <scsi/fc/fc_fc2.h>
  35. #include <scsi/libfc.h>
  36. #include <scsi/fc_encode.h>
  37. #include "fc_libfc.h"
  38. struct kmem_cache *scsi_pkt_cachep;
  39. /* SRB state definitions */
  40. #define FC_SRB_FREE 0 /* cmd is free */
  41. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  42. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  43. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  44. #define FC_SRB_ABORTED (1 << 3) /* abort acknowleged */
  45. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  46. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  47. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  48. #define FC_SRB_NOMEM (1 << 7) /* dropped to out of mem */
  49. #define FC_SRB_READ (1 << 1)
  50. #define FC_SRB_WRITE (1 << 0)
  51. /*
  52. * The SCp.ptr should be tested and set under the host lock. NULL indicates
  53. * that the command has been retruned to the scsi layer.
  54. */
  55. #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr)
  56. #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in)
  57. #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual)
  58. #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status)
  59. #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual)
  60. /**
  61. * struct fc_fcp_internal - FCP layer internal data
  62. * @scsi_pkt_pool: Memory pool to draw FCP packets from
  63. * @scsi_pkt_queue: Current FCP packets
  64. * @throttled: The FCP packet queue is throttled
  65. */
  66. struct fc_fcp_internal {
  67. mempool_t *scsi_pkt_pool;
  68. struct list_head scsi_pkt_queue;
  69. u8 throttled;
  70. };
  71. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  72. /*
  73. * function prototypes
  74. * FC scsi I/O related functions
  75. */
  76. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  77. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  78. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  79. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  80. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  81. static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
  82. static void fc_timeout_error(struct fc_fcp_pkt *);
  83. static void fc_fcp_timeout(unsigned long);
  84. static void fc_fcp_rec(struct fc_fcp_pkt *);
  85. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  86. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  87. static void fc_io_compl(struct fc_fcp_pkt *);
  88. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  89. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  90. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  91. /*
  92. * command status codes
  93. */
  94. #define FC_COMPLETE 0
  95. #define FC_CMD_ABORTED 1
  96. #define FC_CMD_RESET 2
  97. #define FC_CMD_PLOGO 3
  98. #define FC_SNS_RCV 4
  99. #define FC_TRANS_ERR 5
  100. #define FC_DATA_OVRRUN 6
  101. #define FC_DATA_UNDRUN 7
  102. #define FC_ERROR 8
  103. #define FC_HRD_ERROR 9
  104. #define FC_CMD_TIME_OUT 10
  105. /*
  106. * Error recovery timeout values.
  107. */
  108. #define FC_SCSI_ER_TIMEOUT (10 * HZ)
  109. #define FC_SCSI_TM_TOV (10 * HZ)
  110. #define FC_SCSI_REC_TOV (2 * HZ)
  111. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  112. #define FC_MAX_ERROR_CNT 5
  113. #define FC_MAX_RECOV_RETRY 3
  114. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  115. /**
  116. * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
  117. * @lport: The local port that the FCP packet is for
  118. * @gfp: GFP flags for allocation
  119. *
  120. * Return value: fcp_pkt structure or null on allocation failure.
  121. * Context: Can be called from process context, no lock is required.
  122. */
  123. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
  124. {
  125. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  126. struct fc_fcp_pkt *fsp;
  127. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  128. if (fsp) {
  129. memset(fsp, 0, sizeof(*fsp));
  130. fsp->lp = lport;
  131. atomic_set(&fsp->ref_cnt, 1);
  132. init_timer(&fsp->timer);
  133. INIT_LIST_HEAD(&fsp->list);
  134. spin_lock_init(&fsp->scsi_pkt_lock);
  135. }
  136. return fsp;
  137. }
  138. /**
  139. * fc_fcp_pkt_release() - Release hold on a fcp_pkt
  140. * @fsp: The FCP packet to be released
  141. *
  142. * Context: Can be called from process or interrupt context,
  143. * no lock is required.
  144. */
  145. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  146. {
  147. if (atomic_dec_and_test(&fsp->ref_cnt)) {
  148. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  149. mempool_free(fsp, si->scsi_pkt_pool);
  150. }
  151. }
  152. /**
  153. * fc_fcp_pkt_hold() - Hold a fcp_pkt
  154. * @fsp: The FCP packet to be held
  155. */
  156. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  157. {
  158. atomic_inc(&fsp->ref_cnt);
  159. }
  160. /**
  161. * fc_fcp_pkt_destory() - Release hold on a fcp_pkt
  162. * @seq: The sequence that the FCP packet is on (required by destructor API)
  163. * @fsp: The FCP packet to be released
  164. *
  165. * This routine is called by a destructor callback in the exch_seq_send()
  166. * routine of the libfc Transport Template. The 'struct fc_seq' is a required
  167. * argument even though it is not used by this routine.
  168. *
  169. * Context: No locking required.
  170. */
  171. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  172. {
  173. fc_fcp_pkt_release(fsp);
  174. }
  175. /**
  176. * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
  177. * @fsp: The FCP packet to be locked and incremented
  178. *
  179. * We should only return error if we return a command to SCSI-ml before
  180. * getting a response. This can happen in cases where we send a abort, but
  181. * do not wait for the response and the abort and command can be passing
  182. * each other on the wire/network-layer.
  183. *
  184. * Note: this function locks the packet and gets a reference to allow
  185. * callers to call the completion function while the lock is held and
  186. * not have to worry about the packets refcount.
  187. *
  188. * TODO: Maybe we should just have callers grab/release the lock and
  189. * have a function that they call to verify the fsp and grab a ref if
  190. * needed.
  191. */
  192. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  193. {
  194. spin_lock_bh(&fsp->scsi_pkt_lock);
  195. if (fsp->state & FC_SRB_COMPL) {
  196. spin_unlock_bh(&fsp->scsi_pkt_lock);
  197. return -EPERM;
  198. }
  199. fc_fcp_pkt_hold(fsp);
  200. return 0;
  201. }
  202. /**
  203. * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
  204. * reference count
  205. * @fsp: The FCP packet to be unlocked and decremented
  206. */
  207. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  208. {
  209. spin_unlock_bh(&fsp->scsi_pkt_lock);
  210. fc_fcp_pkt_release(fsp);
  211. }
  212. /**
  213. * fc_fcp_timer_set() - Start a timer for a fcp_pkt
  214. * @fsp: The FCP packet to start a timer for
  215. * @delay: The timeout period for the timer
  216. */
  217. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  218. {
  219. if (!(fsp->state & FC_SRB_COMPL))
  220. mod_timer(&fsp->timer, jiffies + delay);
  221. }
  222. /**
  223. * fc_fcp_send_abort() - Send an abort for exchanges associated with a
  224. * fcp_pkt
  225. * @fsp: The FCP packet to abort exchanges on
  226. */
  227. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  228. {
  229. if (!fsp->seq_ptr)
  230. return -EINVAL;
  231. fsp->state |= FC_SRB_ABORT_PENDING;
  232. return fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0);
  233. }
  234. /**
  235. * fc_fcp_retry_cmd() - Retry a fcp_pkt
  236. * @fsp: The FCP packet to be retried
  237. *
  238. * Sets the status code to be FC_ERROR and then calls
  239. * fc_fcp_complete_locked() which in turn calls fc_io_compl().
  240. * fc_io_compl() will notify the SCSI-ml that the I/O is done.
  241. * The SCSI-ml will retry the command.
  242. */
  243. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp)
  244. {
  245. if (fsp->seq_ptr) {
  246. fsp->lp->tt.exch_done(fsp->seq_ptr);
  247. fsp->seq_ptr = NULL;
  248. }
  249. fsp->state &= ~FC_SRB_ABORT_PENDING;
  250. fsp->io_status = 0;
  251. fsp->status_code = FC_ERROR;
  252. fc_fcp_complete_locked(fsp);
  253. }
  254. /**
  255. * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
  256. * @fsp: The FCP packet that will manage the DDP frames
  257. * @xid: The XID that will be used for the DDP exchange
  258. */
  259. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  260. {
  261. struct fc_lport *lport;
  262. if (!fsp)
  263. return;
  264. lport = fsp->lp;
  265. if ((fsp->req_flags & FC_SRB_READ) &&
  266. (lport->lro_enabled) && (lport->tt.ddp_setup)) {
  267. if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
  268. scsi_sg_count(fsp->cmd)))
  269. fsp->xfer_ddp = xid;
  270. }
  271. }
  272. /**
  273. * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
  274. * DDP related resources for a fcp_pkt
  275. * @fsp: The FCP packet that DDP had been used on
  276. */
  277. static void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  278. {
  279. struct fc_lport *lport;
  280. if (!fsp)
  281. return;
  282. if (fsp->xfer_ddp == FC_XID_UNKNOWN)
  283. return;
  284. lport = fsp->lp;
  285. if (lport->tt.ddp_done) {
  286. fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
  287. fsp->xfer_ddp = FC_XID_UNKNOWN;
  288. }
  289. }
  290. /**
  291. * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
  292. * @fsp: The FCP packet the data is on
  293. * @fp: The data frame
  294. */
  295. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  296. {
  297. struct scsi_cmnd *sc = fsp->cmd;
  298. struct fc_lport *lport = fsp->lp;
  299. struct fcoe_dev_stats *stats;
  300. struct fc_frame_header *fh;
  301. size_t start_offset;
  302. size_t offset;
  303. u32 crc;
  304. u32 copy_len = 0;
  305. size_t len;
  306. void *buf;
  307. struct scatterlist *sg;
  308. u32 nents;
  309. fh = fc_frame_header_get(fp);
  310. offset = ntohl(fh->fh_parm_offset);
  311. start_offset = offset;
  312. len = fr_len(fp) - sizeof(*fh);
  313. buf = fc_frame_payload_get(fp, 0);
  314. /* if this I/O is ddped, update xfer len */
  315. fc_fcp_ddp_done(fsp);
  316. if (offset + len > fsp->data_len) {
  317. /* this should never happen */
  318. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  319. fc_frame_crc_check(fp))
  320. goto crc_err;
  321. FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
  322. "data_len %x\n", len, offset, fsp->data_len);
  323. fc_fcp_retry_cmd(fsp);
  324. return;
  325. }
  326. if (offset != fsp->xfer_len)
  327. fsp->state |= FC_SRB_DISCONTIG;
  328. sg = scsi_sglist(sc);
  329. nents = scsi_sg_count(sc);
  330. if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
  331. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  332. &offset, KM_SOFTIRQ0, NULL);
  333. } else {
  334. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  335. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  336. &offset, KM_SOFTIRQ0, &crc);
  337. buf = fc_frame_payload_get(fp, 0);
  338. if (len % 4)
  339. crc = crc32(crc, buf + len, 4 - (len % 4));
  340. if (~crc != le32_to_cpu(fr_crc(fp))) {
  341. crc_err:
  342. stats = fc_lport_get_stats(lport);
  343. stats->ErrorFrames++;
  344. /* FIXME - per cpu count, not total count! */
  345. if (stats->InvalidCRCCount++ < 5)
  346. printk(KERN_WARNING "libfc: CRC error on data "
  347. "frame for port (%6x)\n",
  348. fc_host_port_id(lport->host));
  349. /*
  350. * Assume the frame is total garbage.
  351. * We may have copied it over the good part
  352. * of the buffer.
  353. * If so, we need to retry the entire operation.
  354. * Otherwise, ignore it.
  355. */
  356. if (fsp->state & FC_SRB_DISCONTIG)
  357. fc_fcp_retry_cmd(fsp);
  358. return;
  359. }
  360. }
  361. if (fsp->xfer_contig_end == start_offset)
  362. fsp->xfer_contig_end += copy_len;
  363. fsp->xfer_len += copy_len;
  364. /*
  365. * In the very rare event that this data arrived after the response
  366. * and completes the transfer, call the completion handler.
  367. */
  368. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  369. fsp->xfer_len == fsp->data_len - fsp->scsi_resid)
  370. fc_fcp_complete_locked(fsp);
  371. }
  372. /**
  373. * fc_fcp_send_data() - Send SCSI data to a target
  374. * @fsp: The FCP packet the data is on
  375. * @sp: The sequence the data is to be sent on
  376. * @offset: The starting offset for this data request
  377. * @seq_blen: The burst length for this data request
  378. *
  379. * Called after receiving a Transfer Ready data descriptor.
  380. * If the LLD is capable of sequence offload then send down the
  381. * seq_blen ammount of data in single frame, otherwise send
  382. * multiple frames of the maximum frame payload supported by
  383. * the target port.
  384. */
  385. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  386. size_t offset, size_t seq_blen)
  387. {
  388. struct fc_exch *ep;
  389. struct scsi_cmnd *sc;
  390. struct scatterlist *sg;
  391. struct fc_frame *fp = NULL;
  392. struct fc_lport *lport = fsp->lp;
  393. size_t remaining;
  394. size_t t_blen;
  395. size_t tlen;
  396. size_t sg_bytes;
  397. size_t frame_offset, fh_parm_offset;
  398. int error;
  399. void *data = NULL;
  400. void *page_addr;
  401. int using_sg = lport->sg_supp;
  402. u32 f_ctl;
  403. WARN_ON(seq_blen <= 0);
  404. if (unlikely(offset + seq_blen > fsp->data_len)) {
  405. /* this should never happen */
  406. FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
  407. "offset %zx\n", seq_blen, offset);
  408. fc_fcp_send_abort(fsp);
  409. return 0;
  410. } else if (offset != fsp->xfer_len) {
  411. /* Out of Order Data Request - no problem, but unexpected. */
  412. FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
  413. "seq_blen %zx offset %zx\n", seq_blen, offset);
  414. }
  415. /*
  416. * if LLD is capable of seq_offload then set transport
  417. * burst length (t_blen) to seq_blen, otherwise set t_blen
  418. * to max FC frame payload previously set in fsp->max_payload.
  419. */
  420. t_blen = fsp->max_payload;
  421. if (lport->seq_offload) {
  422. t_blen = min(seq_blen, (size_t)lport->lso_max);
  423. FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  424. fsp, seq_blen, lport->lso_max, t_blen);
  425. }
  426. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD);
  427. if (t_blen > 512)
  428. t_blen &= ~(512 - 1); /* round down to block size */
  429. WARN_ON(t_blen < FC_MIN_MAX_PAYLOAD); /* won't go below 256 */
  430. sc = fsp->cmd;
  431. remaining = seq_blen;
  432. fh_parm_offset = frame_offset = offset;
  433. tlen = 0;
  434. seq = lport->tt.seq_start_next(seq);
  435. f_ctl = FC_FC_REL_OFF;
  436. WARN_ON(!seq);
  437. sg = scsi_sglist(sc);
  438. while (remaining > 0 && sg) {
  439. if (offset >= sg->length) {
  440. offset -= sg->length;
  441. sg = sg_next(sg);
  442. continue;
  443. }
  444. if (!fp) {
  445. tlen = min(t_blen, remaining);
  446. /*
  447. * TODO. Temporary workaround. fc_seq_send() can't
  448. * handle odd lengths in non-linear skbs.
  449. * This will be the final fragment only.
  450. */
  451. if (tlen % 4)
  452. using_sg = 0;
  453. if (using_sg) {
  454. fp = _fc_frame_alloc(lport, 0);
  455. if (!fp)
  456. return -ENOMEM;
  457. } else {
  458. fp = fc_frame_alloc(lport, tlen);
  459. if (!fp)
  460. return -ENOMEM;
  461. data = (void *)(fr_hdr(fp)) +
  462. sizeof(struct fc_frame_header);
  463. }
  464. fh_parm_offset = frame_offset;
  465. fr_max_payload(fp) = fsp->max_payload;
  466. }
  467. sg_bytes = min(tlen, sg->length - offset);
  468. if (using_sg) {
  469. get_page(sg_page(sg));
  470. skb_fill_page_desc(fp_skb(fp),
  471. skb_shinfo(fp_skb(fp))->nr_frags,
  472. sg_page(sg), sg->offset + offset,
  473. sg_bytes);
  474. fp_skb(fp)->data_len += sg_bytes;
  475. fr_len(fp) += sg_bytes;
  476. fp_skb(fp)->truesize += PAGE_SIZE;
  477. } else {
  478. size_t off = offset + sg->offset;
  479. /*
  480. * The scatterlist item may be bigger than PAGE_SIZE,
  481. * but we must not cross pages inside the kmap.
  482. */
  483. sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
  484. (off & ~PAGE_MASK)));
  485. page_addr = kmap_atomic(sg_page(sg) +
  486. (off >> PAGE_SHIFT),
  487. KM_SOFTIRQ0);
  488. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  489. sg_bytes);
  490. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  491. data += sg_bytes;
  492. }
  493. offset += sg_bytes;
  494. frame_offset += sg_bytes;
  495. tlen -= sg_bytes;
  496. remaining -= sg_bytes;
  497. if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
  498. (tlen))
  499. continue;
  500. /*
  501. * Send sequence with transfer sequence initiative in case
  502. * this is last FCP frame of the sequence.
  503. */
  504. if (remaining == 0)
  505. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  506. ep = fc_seq_exch(seq);
  507. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  508. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  509. /*
  510. * send fragment using for a sequence.
  511. */
  512. error = lport->tt.seq_send(lport, seq, fp);
  513. if (error) {
  514. WARN_ON(1); /* send error should be rare */
  515. fc_fcp_retry_cmd(fsp);
  516. return 0;
  517. }
  518. fp = NULL;
  519. }
  520. fsp->xfer_len += seq_blen; /* premature count? */
  521. return 0;
  522. }
  523. /**
  524. * fc_fcp_abts_resp() - Send an ABTS response
  525. * @fsp: The FCP packet that is being aborted
  526. * @fp: The response frame
  527. */
  528. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  529. {
  530. int ba_done = 1;
  531. struct fc_ba_rjt *brp;
  532. struct fc_frame_header *fh;
  533. fh = fc_frame_header_get(fp);
  534. switch (fh->fh_r_ctl) {
  535. case FC_RCTL_BA_ACC:
  536. break;
  537. case FC_RCTL_BA_RJT:
  538. brp = fc_frame_payload_get(fp, sizeof(*brp));
  539. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  540. break;
  541. /* fall thru */
  542. default:
  543. /*
  544. * we will let the command timeout
  545. * and scsi-ml recover in this case,
  546. * therefore cleared the ba_done flag.
  547. */
  548. ba_done = 0;
  549. }
  550. if (ba_done) {
  551. fsp->state |= FC_SRB_ABORTED;
  552. fsp->state &= ~FC_SRB_ABORT_PENDING;
  553. if (fsp->wait_for_comp)
  554. complete(&fsp->tm_done);
  555. else
  556. fc_fcp_complete_locked(fsp);
  557. }
  558. }
  559. /**
  560. * fc_fcp_reduce_can_queue() - Reduce the can_queue value for a local port
  561. * @lport: The local port to reduce can_queue on
  562. *
  563. * If we are getting memory allocation failures, then we may
  564. * be trying to execute too many commands. We let the running
  565. * commands complete or timeout, then try again with a reduced
  566. * can_queue. Eventually we will hit the point where we run
  567. * on all reserved structs.
  568. */
  569. static void fc_fcp_reduce_can_queue(struct fc_lport *lport)
  570. {
  571. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  572. unsigned long flags;
  573. int can_queue;
  574. spin_lock_irqsave(lport->host->host_lock, flags);
  575. if (si->throttled)
  576. goto done;
  577. si->throttled = 1;
  578. can_queue = lport->host->can_queue;
  579. can_queue >>= 1;
  580. if (!can_queue)
  581. can_queue = 1;
  582. lport->host->can_queue = can_queue;
  583. shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
  584. "Reducing can_queue to %d.\n", can_queue);
  585. done:
  586. spin_unlock_irqrestore(lport->host->host_lock, flags);
  587. }
  588. /**
  589. * fc_fcp_recv() - Reveive an FCP frame
  590. * @seq: The sequence the frame is on
  591. * @fp: The received frame
  592. * @arg: The related FCP packet
  593. *
  594. * Context: Called from Soft IRQ context. Can not be called
  595. * holding the FCP packet list lock.
  596. */
  597. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  598. {
  599. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  600. struct fc_lport *lport = fsp->lp;
  601. struct fc_frame_header *fh;
  602. struct fcp_txrdy *dd;
  603. u8 r_ctl;
  604. int rc = 0;
  605. if (IS_ERR(fp))
  606. goto errout;
  607. fh = fc_frame_header_get(fp);
  608. r_ctl = fh->fh_r_ctl;
  609. if (!(lport->state & LPORT_ST_READY))
  610. goto out;
  611. if (fc_fcp_lock_pkt(fsp))
  612. goto out;
  613. fsp->last_pkt_time = jiffies;
  614. if (fh->fh_type == FC_TYPE_BLS) {
  615. fc_fcp_abts_resp(fsp, fp);
  616. goto unlock;
  617. }
  618. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  619. goto unlock;
  620. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  621. /*
  622. * received XFER RDY from the target
  623. * need to send data to the target
  624. */
  625. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  626. dd = fc_frame_payload_get(fp, sizeof(*dd));
  627. WARN_ON(!dd);
  628. rc = fc_fcp_send_data(fsp, seq,
  629. (size_t) ntohl(dd->ft_data_ro),
  630. (size_t) ntohl(dd->ft_burst_len));
  631. if (!rc)
  632. seq->rec_data = fsp->xfer_len;
  633. else if (rc == -ENOMEM)
  634. fsp->state |= FC_SRB_NOMEM;
  635. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  636. /*
  637. * received a DATA frame
  638. * next we will copy the data to the system buffer
  639. */
  640. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  641. fc_fcp_recv_data(fsp, fp);
  642. seq->rec_data = fsp->xfer_contig_end;
  643. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  644. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  645. fc_fcp_resp(fsp, fp);
  646. } else {
  647. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  648. }
  649. unlock:
  650. fc_fcp_unlock_pkt(fsp);
  651. out:
  652. fc_frame_free(fp);
  653. errout:
  654. if (IS_ERR(fp))
  655. fc_fcp_error(fsp, fp);
  656. else if (rc == -ENOMEM)
  657. fc_fcp_reduce_can_queue(lport);
  658. }
  659. /**
  660. * fc_fcp_resp() - Handler for FCP responses
  661. * @fsp: The FCP packet the response is for
  662. * @fp: The response frame
  663. */
  664. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  665. {
  666. struct fc_frame_header *fh;
  667. struct fcp_resp *fc_rp;
  668. struct fcp_resp_ext *rp_ex;
  669. struct fcp_resp_rsp_info *fc_rp_info;
  670. u32 plen;
  671. u32 expected_len;
  672. u32 respl = 0;
  673. u32 snsl = 0;
  674. u8 flags = 0;
  675. plen = fr_len(fp);
  676. fh = (struct fc_frame_header *)fr_hdr(fp);
  677. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  678. goto len_err;
  679. plen -= sizeof(*fh);
  680. fc_rp = (struct fcp_resp *)(fh + 1);
  681. fsp->cdb_status = fc_rp->fr_status;
  682. flags = fc_rp->fr_flags;
  683. fsp->scsi_comp_flags = flags;
  684. expected_len = fsp->data_len;
  685. /* if ddp, update xfer len */
  686. fc_fcp_ddp_done(fsp);
  687. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  688. rp_ex = (void *)(fc_rp + 1);
  689. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  690. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  691. goto len_err;
  692. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  693. if (flags & FCP_RSP_LEN_VAL) {
  694. respl = ntohl(rp_ex->fr_rsp_len);
  695. if (respl != sizeof(*fc_rp_info))
  696. goto len_err;
  697. if (fsp->wait_for_comp) {
  698. /* Abuse cdb_status for rsp code */
  699. fsp->cdb_status = fc_rp_info->rsp_code;
  700. complete(&fsp->tm_done);
  701. /*
  702. * tmfs will not have any scsi cmd so
  703. * exit here
  704. */
  705. return;
  706. } else
  707. goto err;
  708. }
  709. if (flags & FCP_SNS_LEN_VAL) {
  710. snsl = ntohl(rp_ex->fr_sns_len);
  711. if (snsl > SCSI_SENSE_BUFFERSIZE)
  712. snsl = SCSI_SENSE_BUFFERSIZE;
  713. memcpy(fsp->cmd->sense_buffer,
  714. (char *)fc_rp_info + respl, snsl);
  715. }
  716. }
  717. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  718. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  719. goto len_err;
  720. if (flags & FCP_RESID_UNDER) {
  721. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  722. /*
  723. * The cmnd->underflow is the minimum number of
  724. * bytes that must be transfered for this
  725. * command. Provided a sense condition is not
  726. * present, make sure the actual amount
  727. * transferred is at least the underflow value
  728. * or fail.
  729. */
  730. if (!(flags & FCP_SNS_LEN_VAL) &&
  731. (fc_rp->fr_status == 0) &&
  732. (scsi_bufflen(fsp->cmd) -
  733. fsp->scsi_resid) < fsp->cmd->underflow)
  734. goto err;
  735. expected_len -= fsp->scsi_resid;
  736. } else {
  737. fsp->status_code = FC_ERROR;
  738. }
  739. }
  740. }
  741. fsp->state |= FC_SRB_RCV_STATUS;
  742. /*
  743. * Check for missing or extra data frames.
  744. */
  745. if (unlikely(fsp->xfer_len != expected_len)) {
  746. if (fsp->xfer_len < expected_len) {
  747. /*
  748. * Some data may be queued locally,
  749. * Wait a at least one jiffy to see if it is delivered.
  750. * If this expires without data, we may do SRR.
  751. */
  752. fc_fcp_timer_set(fsp, 2);
  753. return;
  754. }
  755. fsp->status_code = FC_DATA_OVRRUN;
  756. FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
  757. "len %x, data len %x\n",
  758. fsp->rport->port_id,
  759. fsp->xfer_len, expected_len, fsp->data_len);
  760. }
  761. fc_fcp_complete_locked(fsp);
  762. return;
  763. len_err:
  764. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  765. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  766. err:
  767. fsp->status_code = FC_ERROR;
  768. fc_fcp_complete_locked(fsp);
  769. }
  770. /**
  771. * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
  772. * fcp_pkt lock held
  773. * @fsp: The FCP packet to be completed
  774. *
  775. * This function may sleep if a timer is pending. The packet lock must be
  776. * held, and the host lock must not be held.
  777. */
  778. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  779. {
  780. struct fc_lport *lport = fsp->lp;
  781. struct fc_seq *seq;
  782. struct fc_exch *ep;
  783. u32 f_ctl;
  784. if (fsp->state & FC_SRB_ABORT_PENDING)
  785. return;
  786. if (fsp->state & FC_SRB_ABORTED) {
  787. if (!fsp->status_code)
  788. fsp->status_code = FC_CMD_ABORTED;
  789. } else {
  790. /*
  791. * Test for transport underrun, independent of response
  792. * underrun status.
  793. */
  794. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  795. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  796. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  797. fsp->status_code = FC_DATA_UNDRUN;
  798. fsp->io_status = 0;
  799. }
  800. }
  801. seq = fsp->seq_ptr;
  802. if (seq) {
  803. fsp->seq_ptr = NULL;
  804. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  805. struct fc_frame *conf_frame;
  806. struct fc_seq *csp;
  807. csp = lport->tt.seq_start_next(seq);
  808. conf_frame = fc_frame_alloc(fsp->lp, 0);
  809. if (conf_frame) {
  810. f_ctl = FC_FC_SEQ_INIT;
  811. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  812. ep = fc_seq_exch(seq);
  813. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  814. ep->did, ep->sid,
  815. FC_TYPE_FCP, f_ctl, 0);
  816. lport->tt.seq_send(lport, csp, conf_frame);
  817. }
  818. }
  819. lport->tt.exch_done(seq);
  820. }
  821. fc_io_compl(fsp);
  822. }
  823. /**
  824. * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
  825. * @fsp: The FCP packet whose exchanges should be canceled
  826. * @error: The reason for the cancellation
  827. */
  828. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  829. {
  830. struct fc_lport *lport = fsp->lp;
  831. if (fsp->seq_ptr) {
  832. lport->tt.exch_done(fsp->seq_ptr);
  833. fsp->seq_ptr = NULL;
  834. }
  835. fsp->status_code = error;
  836. }
  837. /**
  838. * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
  839. * @lport: The local port whose exchanges should be canceled
  840. * @id: The target's ID
  841. * @lun: The LUN
  842. * @error: The reason for cancellation
  843. *
  844. * If lun or id is -1, they are ignored.
  845. */
  846. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
  847. unsigned int lun, int error)
  848. {
  849. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  850. struct fc_fcp_pkt *fsp;
  851. struct scsi_cmnd *sc_cmd;
  852. unsigned long flags;
  853. spin_lock_irqsave(lport->host->host_lock, flags);
  854. restart:
  855. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  856. sc_cmd = fsp->cmd;
  857. if (id != -1 && scmd_id(sc_cmd) != id)
  858. continue;
  859. if (lun != -1 && sc_cmd->device->lun != lun)
  860. continue;
  861. fc_fcp_pkt_hold(fsp);
  862. spin_unlock_irqrestore(lport->host->host_lock, flags);
  863. if (!fc_fcp_lock_pkt(fsp)) {
  864. fc_fcp_cleanup_cmd(fsp, error);
  865. fc_io_compl(fsp);
  866. fc_fcp_unlock_pkt(fsp);
  867. }
  868. fc_fcp_pkt_release(fsp);
  869. spin_lock_irqsave(lport->host->host_lock, flags);
  870. /*
  871. * while we dropped the lock multiple pkts could
  872. * have been released, so we have to start over.
  873. */
  874. goto restart;
  875. }
  876. spin_unlock_irqrestore(lport->host->host_lock, flags);
  877. }
  878. /**
  879. * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
  880. * @lport: The local port whose exchanges are to be aborted
  881. */
  882. static void fc_fcp_abort_io(struct fc_lport *lport)
  883. {
  884. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
  885. }
  886. /**
  887. * fc_fcp_pkt_send() - Send a fcp_pkt
  888. * @lport: The local port to send the FCP packet on
  889. * @fsp: The FCP packet to send
  890. *
  891. * Return: Zero for success and -1 for failure
  892. * Locks: Called with the host lock and irqs disabled.
  893. */
  894. static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
  895. {
  896. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  897. int rc;
  898. fsp->cmd->SCp.ptr = (char *)fsp;
  899. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  900. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  901. int_to_scsilun(fsp->cmd->device->lun,
  902. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  903. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  904. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  905. spin_unlock_irq(lport->host->host_lock);
  906. rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
  907. spin_lock_irq(lport->host->host_lock);
  908. if (rc)
  909. list_del(&fsp->list);
  910. return rc;
  911. }
  912. /**
  913. * fc_fcp_cmd_send() - Send a FCP command
  914. * @lport: The local port to send the command on
  915. * @fsp: The FCP packet the command is on
  916. * @resp: The handler for the response
  917. */
  918. static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  919. void (*resp)(struct fc_seq *,
  920. struct fc_frame *fp,
  921. void *arg))
  922. {
  923. struct fc_frame *fp;
  924. struct fc_seq *seq;
  925. struct fc_rport *rport;
  926. struct fc_rport_libfc_priv *rpriv;
  927. const size_t len = sizeof(fsp->cdb_cmd);
  928. int rc = 0;
  929. if (fc_fcp_lock_pkt(fsp))
  930. return 0;
  931. fp = fc_frame_alloc(lport, sizeof(fsp->cdb_cmd));
  932. if (!fp) {
  933. rc = -1;
  934. goto unlock;
  935. }
  936. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  937. fr_fsp(fp) = fsp;
  938. rport = fsp->rport;
  939. fsp->max_payload = rport->maxframe_size;
  940. rpriv = rport->dd_data;
  941. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  942. fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
  943. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  944. seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
  945. fsp, 0);
  946. if (!seq) {
  947. rc = -1;
  948. goto unlock;
  949. }
  950. fsp->last_pkt_time = jiffies;
  951. fsp->seq_ptr = seq;
  952. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  953. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  954. fc_fcp_timer_set(fsp,
  955. (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
  956. FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
  957. unlock:
  958. fc_fcp_unlock_pkt(fsp);
  959. return rc;
  960. }
  961. /**
  962. * fc_fcp_error() - Handler for FCP layer errors
  963. * @fsp: The FCP packet the error is on
  964. * @fp: The frame that has errored
  965. */
  966. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  967. {
  968. int error = PTR_ERR(fp);
  969. if (fc_fcp_lock_pkt(fsp))
  970. return;
  971. if (error == -FC_EX_CLOSED) {
  972. fc_fcp_retry_cmd(fsp);
  973. goto unlock;
  974. }
  975. /*
  976. * clear abort pending, because the lower layer
  977. * decided to force completion.
  978. */
  979. fsp->state &= ~FC_SRB_ABORT_PENDING;
  980. fsp->status_code = FC_CMD_PLOGO;
  981. fc_fcp_complete_locked(fsp);
  982. unlock:
  983. fc_fcp_unlock_pkt(fsp);
  984. }
  985. /**
  986. * fc_fcp_pkt_abort() - Abort a fcp_pkt
  987. * @fsp: The FCP packet to abort on
  988. *
  989. * Called to send an abort and then wait for abort completion
  990. */
  991. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  992. {
  993. int rc = FAILED;
  994. if (fc_fcp_send_abort(fsp))
  995. return FAILED;
  996. init_completion(&fsp->tm_done);
  997. fsp->wait_for_comp = 1;
  998. spin_unlock_bh(&fsp->scsi_pkt_lock);
  999. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1000. spin_lock_bh(&fsp->scsi_pkt_lock);
  1001. fsp->wait_for_comp = 0;
  1002. if (!rc) {
  1003. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  1004. rc = FAILED;
  1005. } else if (fsp->state & FC_SRB_ABORTED) {
  1006. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  1007. rc = SUCCESS;
  1008. fc_fcp_complete_locked(fsp);
  1009. }
  1010. return rc;
  1011. }
  1012. /**
  1013. * fc_lun_reset_send() - Send LUN reset command
  1014. * @data: The FCP packet that identifies the LUN to be reset
  1015. */
  1016. static void fc_lun_reset_send(unsigned long data)
  1017. {
  1018. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1019. struct fc_lport *lport = fsp->lp;
  1020. if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
  1021. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1022. return;
  1023. if (fc_fcp_lock_pkt(fsp))
  1024. return;
  1025. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1026. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1027. fc_fcp_unlock_pkt(fsp);
  1028. }
  1029. }
  1030. /**
  1031. * fc_lun_reset() - Send a LUN RESET command to a device
  1032. * and wait for the reply
  1033. * @lport: The local port to sent the comand on
  1034. * @fsp: The FCP packet that identifies the LUN to be reset
  1035. * @id: The SCSI command ID
  1036. * @lun: The LUN ID to be reset
  1037. */
  1038. static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1039. unsigned int id, unsigned int lun)
  1040. {
  1041. int rc;
  1042. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1043. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1044. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  1045. fsp->wait_for_comp = 1;
  1046. init_completion(&fsp->tm_done);
  1047. fc_lun_reset_send((unsigned long)fsp);
  1048. /*
  1049. * wait for completion of reset
  1050. * after that make sure all commands are terminated
  1051. */
  1052. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1053. spin_lock_bh(&fsp->scsi_pkt_lock);
  1054. fsp->state |= FC_SRB_COMPL;
  1055. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1056. del_timer_sync(&fsp->timer);
  1057. spin_lock_bh(&fsp->scsi_pkt_lock);
  1058. if (fsp->seq_ptr) {
  1059. lport->tt.exch_done(fsp->seq_ptr);
  1060. fsp->seq_ptr = NULL;
  1061. }
  1062. fsp->wait_for_comp = 0;
  1063. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1064. if (!rc) {
  1065. FC_SCSI_DBG(lport, "lun reset failed\n");
  1066. return FAILED;
  1067. }
  1068. /* cdb_status holds the tmf's rsp code */
  1069. if (fsp->cdb_status != FCP_TMF_CMPL)
  1070. return FAILED;
  1071. FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
  1072. fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
  1073. return SUCCESS;
  1074. }
  1075. /**
  1076. * fc_tm_done() - Task Managment response handler
  1077. * @seq: The sequence that the response is on
  1078. * @fp: The response frame
  1079. * @arg: The FCP packet the response is for
  1080. */
  1081. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1082. {
  1083. struct fc_fcp_pkt *fsp = arg;
  1084. struct fc_frame_header *fh;
  1085. if (IS_ERR(fp)) {
  1086. /*
  1087. * If there is an error just let it timeout or wait
  1088. * for TMF to be aborted if it timedout.
  1089. *
  1090. * scsi-eh will escalate for when either happens.
  1091. */
  1092. return;
  1093. }
  1094. if (fc_fcp_lock_pkt(fsp))
  1095. return;
  1096. /*
  1097. * raced with eh timeout handler.
  1098. */
  1099. if (!fsp->seq_ptr || !fsp->wait_for_comp) {
  1100. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1101. return;
  1102. }
  1103. fh = fc_frame_header_get(fp);
  1104. if (fh->fh_type != FC_TYPE_BLS)
  1105. fc_fcp_resp(fsp, fp);
  1106. fsp->seq_ptr = NULL;
  1107. fsp->lp->tt.exch_done(seq);
  1108. fc_frame_free(fp);
  1109. fc_fcp_unlock_pkt(fsp);
  1110. }
  1111. /**
  1112. * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
  1113. * @lport: The local port to be cleaned up
  1114. */
  1115. static void fc_fcp_cleanup(struct fc_lport *lport)
  1116. {
  1117. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
  1118. }
  1119. /**
  1120. * fc_fcp_timeout() - Handler for fcp_pkt timeouts
  1121. * @data: The FCP packet that has timed out
  1122. *
  1123. * If REC is supported then just issue it and return. The REC exchange will
  1124. * complete or time out and recovery can continue at that point. Otherwise,
  1125. * if the response has been received without all the data it has been
  1126. * ER_TIMEOUT since the response was received. If the response has not been
  1127. * received we see if data was received recently. If it has been then we
  1128. * continue waiting, otherwise, we abort the command.
  1129. */
  1130. static void fc_fcp_timeout(unsigned long data)
  1131. {
  1132. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1133. struct fc_rport *rport = fsp->rport;
  1134. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1135. if (fc_fcp_lock_pkt(fsp))
  1136. return;
  1137. if (fsp->cdb_cmd.fc_tm_flags)
  1138. goto unlock;
  1139. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1140. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1141. fc_fcp_rec(fsp);
  1142. else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
  1143. jiffies))
  1144. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1145. else if (fsp->state & FC_SRB_RCV_STATUS)
  1146. fc_fcp_complete_locked(fsp);
  1147. else
  1148. fc_timeout_error(fsp);
  1149. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1150. unlock:
  1151. fc_fcp_unlock_pkt(fsp);
  1152. }
  1153. /**
  1154. * fc_fcp_rec() - Send a REC ELS request
  1155. * @fsp: The FCP packet to send the REC request on
  1156. */
  1157. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1158. {
  1159. struct fc_lport *lport;
  1160. struct fc_frame *fp;
  1161. struct fc_rport *rport;
  1162. struct fc_rport_libfc_priv *rpriv;
  1163. lport = fsp->lp;
  1164. rport = fsp->rport;
  1165. rpriv = rport->dd_data;
  1166. if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
  1167. fsp->status_code = FC_HRD_ERROR;
  1168. fsp->io_status = 0;
  1169. fc_fcp_complete_locked(fsp);
  1170. return;
  1171. }
  1172. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rec));
  1173. if (!fp)
  1174. goto retry;
  1175. fr_seq(fp) = fsp->seq_ptr;
  1176. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1177. fc_host_port_id(rpriv->local_port->host), FC_TYPE_ELS,
  1178. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1179. if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
  1180. fc_fcp_rec_resp, fsp,
  1181. jiffies_to_msecs(FC_SCSI_REC_TOV))) {
  1182. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1183. return;
  1184. }
  1185. retry:
  1186. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1187. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1188. else
  1189. fc_timeout_error(fsp);
  1190. }
  1191. /**
  1192. * fc_fcp_rec_resp() - Handler for REC ELS responses
  1193. * @seq: The sequence the response is on
  1194. * @fp: The response frame
  1195. * @arg: The FCP packet the response is on
  1196. *
  1197. * If the response is a reject then the scsi layer will handle
  1198. * the timeout. If the response is a LS_ACC then if the I/O was not completed
  1199. * set the timeout and return. If the I/O was completed then complete the
  1200. * exchange and tell the SCSI layer.
  1201. */
  1202. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1203. {
  1204. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1205. struct fc_els_rec_acc *recp;
  1206. struct fc_els_ls_rjt *rjt;
  1207. u32 e_stat;
  1208. u8 opcode;
  1209. u32 offset;
  1210. enum dma_data_direction data_dir;
  1211. enum fc_rctl r_ctl;
  1212. struct fc_rport_libfc_priv *rpriv;
  1213. if (IS_ERR(fp)) {
  1214. fc_fcp_rec_error(fsp, fp);
  1215. return;
  1216. }
  1217. if (fc_fcp_lock_pkt(fsp))
  1218. goto out;
  1219. fsp->recov_retry = 0;
  1220. opcode = fc_frame_payload_op(fp);
  1221. if (opcode == ELS_LS_RJT) {
  1222. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1223. switch (rjt->er_reason) {
  1224. default:
  1225. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1226. "reason %d expl %d\n",
  1227. fsp->rport->port_id, rjt->er_reason,
  1228. rjt->er_explan);
  1229. /* fall through */
  1230. case ELS_RJT_UNSUP:
  1231. FC_FCP_DBG(fsp, "device does not support REC\n");
  1232. rpriv = fsp->rport->dd_data;
  1233. /*
  1234. * if we do not spport RECs or got some bogus
  1235. * reason then resetup timer so we check for
  1236. * making progress.
  1237. */
  1238. rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1239. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1240. break;
  1241. case ELS_RJT_LOGIC:
  1242. case ELS_RJT_UNAB:
  1243. /*
  1244. * If no data transfer, the command frame got dropped
  1245. * so we just retry. If data was transferred, we
  1246. * lost the response but the target has no record,
  1247. * so we abort and retry.
  1248. */
  1249. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1250. fsp->xfer_len == 0) {
  1251. fc_fcp_retry_cmd(fsp);
  1252. break;
  1253. }
  1254. fc_timeout_error(fsp);
  1255. break;
  1256. }
  1257. } else if (opcode == ELS_LS_ACC) {
  1258. if (fsp->state & FC_SRB_ABORTED)
  1259. goto unlock_out;
  1260. data_dir = fsp->cmd->sc_data_direction;
  1261. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1262. offset = ntohl(recp->reca_fc4value);
  1263. e_stat = ntohl(recp->reca_e_stat);
  1264. if (e_stat & ESB_ST_COMPLETE) {
  1265. /*
  1266. * The exchange is complete.
  1267. *
  1268. * For output, we must've lost the response.
  1269. * For input, all data must've been sent.
  1270. * We lost may have lost the response
  1271. * (and a confirmation was requested) and maybe
  1272. * some data.
  1273. *
  1274. * If all data received, send SRR
  1275. * asking for response. If partial data received,
  1276. * or gaps, SRR requests data at start of gap.
  1277. * Recovery via SRR relies on in-order-delivery.
  1278. */
  1279. if (data_dir == DMA_TO_DEVICE) {
  1280. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1281. } else if (fsp->xfer_contig_end == offset) {
  1282. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1283. } else {
  1284. offset = fsp->xfer_contig_end;
  1285. r_ctl = FC_RCTL_DD_SOL_DATA;
  1286. }
  1287. fc_fcp_srr(fsp, r_ctl, offset);
  1288. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1289. /*
  1290. * The remote port has the initiative, so just
  1291. * keep waiting for it to complete.
  1292. */
  1293. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1294. } else {
  1295. /*
  1296. * The exchange is incomplete, we have seq. initiative.
  1297. * Lost response with requested confirmation,
  1298. * lost confirmation, lost transfer ready or
  1299. * lost write data.
  1300. *
  1301. * For output, if not all data was received, ask
  1302. * for transfer ready to be repeated.
  1303. *
  1304. * If we received or sent all the data, send SRR to
  1305. * request response.
  1306. *
  1307. * If we lost a response, we may have lost some read
  1308. * data as well.
  1309. */
  1310. r_ctl = FC_RCTL_DD_SOL_DATA;
  1311. if (data_dir == DMA_TO_DEVICE) {
  1312. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1313. if (offset < fsp->data_len)
  1314. r_ctl = FC_RCTL_DD_DATA_DESC;
  1315. } else if (offset == fsp->xfer_contig_end) {
  1316. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1317. } else if (fsp->xfer_contig_end < offset) {
  1318. offset = fsp->xfer_contig_end;
  1319. }
  1320. fc_fcp_srr(fsp, r_ctl, offset);
  1321. }
  1322. }
  1323. unlock_out:
  1324. fc_fcp_unlock_pkt(fsp);
  1325. out:
  1326. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1327. fc_frame_free(fp);
  1328. }
  1329. /**
  1330. * fc_fcp_rec_error() - Handler for REC errors
  1331. * @fsp: The FCP packet the error is on
  1332. * @fp: The REC frame
  1333. */
  1334. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1335. {
  1336. int error = PTR_ERR(fp);
  1337. if (fc_fcp_lock_pkt(fsp))
  1338. goto out;
  1339. switch (error) {
  1340. case -FC_EX_CLOSED:
  1341. fc_fcp_retry_cmd(fsp);
  1342. break;
  1343. default:
  1344. FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
  1345. fsp, fsp->rport->port_id, error);
  1346. fsp->status_code = FC_CMD_PLOGO;
  1347. /* fall through */
  1348. case -FC_EX_TIMEOUT:
  1349. /*
  1350. * Assume REC or LS_ACC was lost.
  1351. * The exchange manager will have aborted REC, so retry.
  1352. */
  1353. FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
  1354. fsp->rport->port_id, error, fsp->recov_retry,
  1355. FC_MAX_RECOV_RETRY);
  1356. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1357. fc_fcp_rec(fsp);
  1358. else
  1359. fc_timeout_error(fsp);
  1360. break;
  1361. }
  1362. fc_fcp_unlock_pkt(fsp);
  1363. out:
  1364. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1365. }
  1366. /**
  1367. * fc_timeout_error() - Handler for fcp_pkt timeouts
  1368. * @fsp: The FCP packt that has timed out
  1369. */
  1370. static void fc_timeout_error(struct fc_fcp_pkt *fsp)
  1371. {
  1372. fsp->status_code = FC_CMD_TIME_OUT;
  1373. fsp->cdb_status = 0;
  1374. fsp->io_status = 0;
  1375. /*
  1376. * if this fails then we let the scsi command timer fire and
  1377. * scsi-ml escalate.
  1378. */
  1379. fc_fcp_send_abort(fsp);
  1380. }
  1381. /**
  1382. * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
  1383. * @fsp: The FCP packet the SRR is to be sent on
  1384. * @r_ctl: The R_CTL field for the SRR request
  1385. * This is called after receiving status but insufficient data, or
  1386. * when expecting status but the request has timed out.
  1387. */
  1388. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1389. {
  1390. struct fc_lport *lport = fsp->lp;
  1391. struct fc_rport *rport;
  1392. struct fc_rport_libfc_priv *rpriv;
  1393. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1394. struct fc_seq *seq;
  1395. struct fcp_srr *srr;
  1396. struct fc_frame *fp;
  1397. u8 cdb_op;
  1398. rport = fsp->rport;
  1399. rpriv = rport->dd_data;
  1400. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1401. if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
  1402. rpriv->rp_state != RPORT_ST_READY)
  1403. goto retry; /* shouldn't happen */
  1404. fp = fc_frame_alloc(lport, sizeof(*srr));
  1405. if (!fp)
  1406. goto retry;
  1407. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1408. memset(srr, 0, sizeof(*srr));
  1409. srr->srr_op = ELS_SRR;
  1410. srr->srr_ox_id = htons(ep->oxid);
  1411. srr->srr_rx_id = htons(ep->rxid);
  1412. srr->srr_r_ctl = r_ctl;
  1413. srr->srr_rel_off = htonl(offset);
  1414. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1415. fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
  1416. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1417. seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
  1418. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
  1419. if (!seq)
  1420. goto retry;
  1421. fsp->recov_seq = seq;
  1422. fsp->xfer_len = offset;
  1423. fsp->xfer_contig_end = offset;
  1424. fsp->state &= ~FC_SRB_RCV_STATUS;
  1425. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1426. return;
  1427. retry:
  1428. fc_fcp_retry_cmd(fsp);
  1429. }
  1430. /**
  1431. * fc_fcp_srr_resp() - Handler for SRR response
  1432. * @seq: The sequence the SRR is on
  1433. * @fp: The SRR frame
  1434. * @arg: The FCP packet the SRR is on
  1435. */
  1436. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1437. {
  1438. struct fc_fcp_pkt *fsp = arg;
  1439. struct fc_frame_header *fh;
  1440. if (IS_ERR(fp)) {
  1441. fc_fcp_srr_error(fsp, fp);
  1442. return;
  1443. }
  1444. if (fc_fcp_lock_pkt(fsp))
  1445. goto out;
  1446. fh = fc_frame_header_get(fp);
  1447. /*
  1448. * BUG? fc_fcp_srr_error calls exch_done which would release
  1449. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1450. * then fc_exch_timeout would be sending an abort. The exch_done
  1451. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1452. * an abort response though.
  1453. */
  1454. if (fh->fh_type == FC_TYPE_BLS) {
  1455. fc_fcp_unlock_pkt(fsp);
  1456. return;
  1457. }
  1458. fsp->recov_seq = NULL;
  1459. switch (fc_frame_payload_op(fp)) {
  1460. case ELS_LS_ACC:
  1461. fsp->recov_retry = 0;
  1462. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1463. break;
  1464. case ELS_LS_RJT:
  1465. default:
  1466. fc_timeout_error(fsp);
  1467. break;
  1468. }
  1469. fc_fcp_unlock_pkt(fsp);
  1470. fsp->lp->tt.exch_done(seq);
  1471. out:
  1472. fc_frame_free(fp);
  1473. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1474. }
  1475. /**
  1476. * fc_fcp_srr_error() - Handler for SRR errors
  1477. * @fsp: The FCP packet that the SRR error is on
  1478. * @fp: The SRR frame
  1479. */
  1480. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1481. {
  1482. if (fc_fcp_lock_pkt(fsp))
  1483. goto out;
  1484. fsp->lp->tt.exch_done(fsp->recov_seq);
  1485. fsp->recov_seq = NULL;
  1486. switch (PTR_ERR(fp)) {
  1487. case -FC_EX_TIMEOUT:
  1488. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1489. fc_fcp_rec(fsp);
  1490. else
  1491. fc_timeout_error(fsp);
  1492. break;
  1493. case -FC_EX_CLOSED: /* e.g., link failure */
  1494. /* fall through */
  1495. default:
  1496. fc_fcp_retry_cmd(fsp);
  1497. break;
  1498. }
  1499. fc_fcp_unlock_pkt(fsp);
  1500. out:
  1501. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1502. }
  1503. /**
  1504. * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
  1505. * @lport: The local port to be checked
  1506. */
  1507. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
  1508. {
  1509. /* lock ? */
  1510. return (lport->state == LPORT_ST_READY) &&
  1511. lport->link_up && !lport->qfull;
  1512. }
  1513. /**
  1514. * fc_queuecommand() - The queuecommand function of the SCSI template
  1515. * @cmd: The scsi_cmnd to be executed
  1516. * @done: The callback function to be called when the scsi_cmnd is complete
  1517. *
  1518. * This is the i/o strategy routine, called by the SCSI layer. This routine
  1519. * is called with the host_lock held.
  1520. */
  1521. int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
  1522. {
  1523. struct fc_lport *lport;
  1524. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1525. struct fc_fcp_pkt *fsp;
  1526. struct fc_rport_libfc_priv *rpriv;
  1527. int rval;
  1528. int rc = 0;
  1529. struct fcoe_dev_stats *stats;
  1530. lport = shost_priv(sc_cmd->device->host);
  1531. rval = fc_remote_port_chkready(rport);
  1532. if (rval) {
  1533. sc_cmd->result = rval;
  1534. done(sc_cmd);
  1535. goto out;
  1536. }
  1537. if (!*(struct fc_remote_port **)rport->dd_data) {
  1538. /*
  1539. * rport is transitioning from blocked/deleted to
  1540. * online
  1541. */
  1542. sc_cmd->result = DID_IMM_RETRY << 16;
  1543. done(sc_cmd);
  1544. goto out;
  1545. }
  1546. rpriv = rport->dd_data;
  1547. if (!fc_fcp_lport_queue_ready(lport)) {
  1548. rc = SCSI_MLQUEUE_HOST_BUSY;
  1549. goto out;
  1550. }
  1551. fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
  1552. if (fsp == NULL) {
  1553. rc = SCSI_MLQUEUE_HOST_BUSY;
  1554. goto out;
  1555. }
  1556. /*
  1557. * build the libfc request pkt
  1558. */
  1559. fsp->cmd = sc_cmd; /* save the cmd */
  1560. fsp->lp = lport; /* save the softc ptr */
  1561. fsp->rport = rport; /* set the remote port ptr */
  1562. fsp->xfer_ddp = FC_XID_UNKNOWN;
  1563. sc_cmd->scsi_done = done;
  1564. /*
  1565. * set up the transfer length
  1566. */
  1567. fsp->data_len = scsi_bufflen(sc_cmd);
  1568. fsp->xfer_len = 0;
  1569. /*
  1570. * setup the data direction
  1571. */
  1572. stats = fc_lport_get_stats(lport);
  1573. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1574. fsp->req_flags = FC_SRB_READ;
  1575. stats->InputRequests++;
  1576. stats->InputMegabytes = fsp->data_len;
  1577. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1578. fsp->req_flags = FC_SRB_WRITE;
  1579. stats->OutputRequests++;
  1580. stats->OutputMegabytes = fsp->data_len;
  1581. } else {
  1582. fsp->req_flags = 0;
  1583. stats->ControlRequests++;
  1584. }
  1585. fsp->tgt_flags = rpriv->flags;
  1586. init_timer(&fsp->timer);
  1587. fsp->timer.data = (unsigned long)fsp;
  1588. /*
  1589. * send it to the lower layer
  1590. * if we get -1 return then put the request in the pending
  1591. * queue.
  1592. */
  1593. rval = fc_fcp_pkt_send(lport, fsp);
  1594. if (rval != 0) {
  1595. fsp->state = FC_SRB_FREE;
  1596. fc_fcp_pkt_release(fsp);
  1597. rc = SCSI_MLQUEUE_HOST_BUSY;
  1598. }
  1599. out:
  1600. return rc;
  1601. }
  1602. EXPORT_SYMBOL(fc_queuecommand);
  1603. /**
  1604. * fc_io_compl() - Handle responses for completed commands
  1605. * @fsp: The FCP packet that is complete
  1606. *
  1607. * Translates fcp_pkt errors to a Linux SCSI errors.
  1608. * The fcp packet lock must be held when calling.
  1609. */
  1610. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1611. {
  1612. struct fc_fcp_internal *si;
  1613. struct scsi_cmnd *sc_cmd;
  1614. struct fc_lport *lport;
  1615. unsigned long flags;
  1616. /* release outstanding ddp context */
  1617. fc_fcp_ddp_done(fsp);
  1618. fsp->state |= FC_SRB_COMPL;
  1619. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1620. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1621. del_timer_sync(&fsp->timer);
  1622. spin_lock_bh(&fsp->scsi_pkt_lock);
  1623. }
  1624. lport = fsp->lp;
  1625. si = fc_get_scsi_internal(lport);
  1626. spin_lock_irqsave(lport->host->host_lock, flags);
  1627. if (!fsp->cmd) {
  1628. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1629. return;
  1630. }
  1631. /*
  1632. * if a command timed out while we had to try and throttle IO
  1633. * and it is now getting cleaned up, then we are about to
  1634. * try again so clear the throttled flag incase we get more
  1635. * time outs.
  1636. */
  1637. if (si->throttled && fsp->state & FC_SRB_NOMEM)
  1638. si->throttled = 0;
  1639. sc_cmd = fsp->cmd;
  1640. fsp->cmd = NULL;
  1641. if (!sc_cmd->SCp.ptr) {
  1642. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1643. return;
  1644. }
  1645. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1646. switch (fsp->status_code) {
  1647. case FC_COMPLETE:
  1648. if (fsp->cdb_status == 0) {
  1649. /*
  1650. * good I/O status
  1651. */
  1652. sc_cmd->result = DID_OK << 16;
  1653. if (fsp->scsi_resid)
  1654. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1655. } else {
  1656. /*
  1657. * transport level I/O was ok but scsi
  1658. * has non zero status
  1659. */
  1660. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1661. }
  1662. break;
  1663. case FC_ERROR:
  1664. sc_cmd->result = DID_ERROR << 16;
  1665. break;
  1666. case FC_DATA_UNDRUN:
  1667. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1668. /*
  1669. * scsi status is good but transport level
  1670. * underrun.
  1671. */
  1672. sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
  1673. DID_OK : DID_ERROR) << 16;
  1674. } else {
  1675. /*
  1676. * scsi got underrun, this is an error
  1677. */
  1678. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1679. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1680. }
  1681. break;
  1682. case FC_DATA_OVRRUN:
  1683. /*
  1684. * overrun is an error
  1685. */
  1686. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1687. break;
  1688. case FC_CMD_ABORTED:
  1689. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1690. break;
  1691. case FC_CMD_TIME_OUT:
  1692. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1693. break;
  1694. case FC_CMD_RESET:
  1695. sc_cmd->result = (DID_RESET << 16);
  1696. break;
  1697. case FC_HRD_ERROR:
  1698. sc_cmd->result = (DID_NO_CONNECT << 16);
  1699. break;
  1700. default:
  1701. sc_cmd->result = (DID_ERROR << 16);
  1702. break;
  1703. }
  1704. list_del(&fsp->list);
  1705. sc_cmd->SCp.ptr = NULL;
  1706. sc_cmd->scsi_done(sc_cmd);
  1707. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1708. /* release ref from initial allocation in queue command */
  1709. fc_fcp_pkt_release(fsp);
  1710. }
  1711. /**
  1712. * fc_eh_abort() - Abort a command
  1713. * @sc_cmd: The SCSI command to abort
  1714. *
  1715. * From SCSI host template.
  1716. * Send an ABTS to the target device and wait for the response.
  1717. */
  1718. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1719. {
  1720. struct fc_fcp_pkt *fsp;
  1721. struct fc_lport *lport;
  1722. int rc = FAILED;
  1723. unsigned long flags;
  1724. lport = shost_priv(sc_cmd->device->host);
  1725. if (lport->state != LPORT_ST_READY)
  1726. return rc;
  1727. else if (!lport->link_up)
  1728. return rc;
  1729. spin_lock_irqsave(lport->host->host_lock, flags);
  1730. fsp = CMD_SP(sc_cmd);
  1731. if (!fsp) {
  1732. /* command completed while scsi eh was setting up */
  1733. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1734. return SUCCESS;
  1735. }
  1736. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1737. fc_fcp_pkt_hold(fsp);
  1738. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1739. if (fc_fcp_lock_pkt(fsp)) {
  1740. /* completed while we were waiting for timer to be deleted */
  1741. rc = SUCCESS;
  1742. goto release_pkt;
  1743. }
  1744. rc = fc_fcp_pkt_abort(fsp);
  1745. fc_fcp_unlock_pkt(fsp);
  1746. release_pkt:
  1747. fc_fcp_pkt_release(fsp);
  1748. return rc;
  1749. }
  1750. EXPORT_SYMBOL(fc_eh_abort);
  1751. /**
  1752. * fc_eh_device_reset() - Reset a single LUN
  1753. * @sc_cmd: The SCSI command which identifies the device whose
  1754. * LUN is to be reset
  1755. *
  1756. * Set from SCSI host template.
  1757. */
  1758. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1759. {
  1760. struct fc_lport *lport;
  1761. struct fc_fcp_pkt *fsp;
  1762. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1763. int rc = FAILED;
  1764. int rval;
  1765. rval = fc_remote_port_chkready(rport);
  1766. if (rval)
  1767. goto out;
  1768. lport = shost_priv(sc_cmd->device->host);
  1769. if (lport->state != LPORT_ST_READY)
  1770. return rc;
  1771. FC_SCSI_DBG(lport, "Resetting rport (%6x)\n", rport->port_id);
  1772. fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
  1773. if (fsp == NULL) {
  1774. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1775. sc_cmd->result = DID_NO_CONNECT << 16;
  1776. goto out;
  1777. }
  1778. /*
  1779. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1780. * the sc passed in is not setup for execution like when sent
  1781. * through the queuecommand callout.
  1782. */
  1783. fsp->lp = lport; /* save the softc ptr */
  1784. fsp->rport = rport; /* set the remote port ptr */
  1785. /*
  1786. * flush outstanding commands
  1787. */
  1788. rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1789. fsp->state = FC_SRB_FREE;
  1790. fc_fcp_pkt_release(fsp);
  1791. out:
  1792. return rc;
  1793. }
  1794. EXPORT_SYMBOL(fc_eh_device_reset);
  1795. /**
  1796. * fc_eh_host_reset() - Reset a Scsi_Host.
  1797. * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
  1798. */
  1799. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1800. {
  1801. struct Scsi_Host *shost = sc_cmd->device->host;
  1802. struct fc_lport *lport = shost_priv(shost);
  1803. unsigned long wait_tmo;
  1804. FC_SCSI_DBG(lport, "Resetting host\n");
  1805. lport->tt.lport_reset(lport);
  1806. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1807. while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
  1808. wait_tmo))
  1809. msleep(1000);
  1810. if (fc_fcp_lport_queue_ready(lport)) {
  1811. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1812. "on port (%6x)\n", fc_host_port_id(lport->host));
  1813. return SUCCESS;
  1814. } else {
  1815. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1816. "port (%6x) is not ready.\n",
  1817. fc_host_port_id(lport->host));
  1818. return FAILED;
  1819. }
  1820. }
  1821. EXPORT_SYMBOL(fc_eh_host_reset);
  1822. /**
  1823. * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
  1824. * @sdev: The SCSI device that identifies the SCSI host
  1825. *
  1826. * Configures queue depth based on host's cmd_per_len. If not set
  1827. * then we use the libfc default.
  1828. */
  1829. int fc_slave_alloc(struct scsi_device *sdev)
  1830. {
  1831. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1832. if (!rport || fc_remote_port_chkready(rport))
  1833. return -ENXIO;
  1834. if (sdev->tagged_supported)
  1835. scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
  1836. else
  1837. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
  1838. FC_FCP_DFLT_QUEUE_DEPTH);
  1839. return 0;
  1840. }
  1841. EXPORT_SYMBOL(fc_slave_alloc);
  1842. /**
  1843. * fc_change_queue_depth() - Change a device's queue depth
  1844. * @sdev: The SCSI device whose queue depth is to change
  1845. * @qdepth: The new queue depth
  1846. * @reason: The resason for the change
  1847. */
  1848. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  1849. {
  1850. switch (reason) {
  1851. case SCSI_QDEPTH_DEFAULT:
  1852. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1853. break;
  1854. case SCSI_QDEPTH_QFULL:
  1855. scsi_track_queue_full(sdev, qdepth);
  1856. break;
  1857. case SCSI_QDEPTH_RAMP_UP:
  1858. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1859. break;
  1860. default:
  1861. return -EOPNOTSUPP;
  1862. }
  1863. return sdev->queue_depth;
  1864. }
  1865. EXPORT_SYMBOL(fc_change_queue_depth);
  1866. /**
  1867. * fc_change_queue_type() - Change a device's queue type
  1868. * @sdev: The SCSI device whose queue depth is to change
  1869. * @tag_type: Identifier for queue type
  1870. */
  1871. int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
  1872. {
  1873. if (sdev->tagged_supported) {
  1874. scsi_set_tag_type(sdev, tag_type);
  1875. if (tag_type)
  1876. scsi_activate_tcq(sdev, sdev->queue_depth);
  1877. else
  1878. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1879. } else
  1880. tag_type = 0;
  1881. return tag_type;
  1882. }
  1883. EXPORT_SYMBOL(fc_change_queue_type);
  1884. /**
  1885. * fc_fcp_destory() - Tear down the FCP layer for a given local port
  1886. * @lport: The local port that no longer needs the FCP layer
  1887. */
  1888. void fc_fcp_destroy(struct fc_lport *lport)
  1889. {
  1890. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  1891. if (!list_empty(&si->scsi_pkt_queue))
  1892. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  1893. "port (%6x)\n", fc_host_port_id(lport->host));
  1894. mempool_destroy(si->scsi_pkt_pool);
  1895. kfree(si);
  1896. lport->scsi_priv = NULL;
  1897. }
  1898. EXPORT_SYMBOL(fc_fcp_destroy);
  1899. int fc_setup_fcp()
  1900. {
  1901. int rc = 0;
  1902. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1903. sizeof(struct fc_fcp_pkt),
  1904. 0, SLAB_HWCACHE_ALIGN, NULL);
  1905. if (!scsi_pkt_cachep) {
  1906. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  1907. "module load failed!");
  1908. rc = -ENOMEM;
  1909. }
  1910. return rc;
  1911. }
  1912. void fc_destroy_fcp()
  1913. {
  1914. if (scsi_pkt_cachep)
  1915. kmem_cache_destroy(scsi_pkt_cachep);
  1916. }
  1917. /**
  1918. * fc_fcp_init() - Initialize the FCP layer for a local port
  1919. * @lport: The local port to initialize the exchange layer for
  1920. */
  1921. int fc_fcp_init(struct fc_lport *lport)
  1922. {
  1923. int rc;
  1924. struct fc_fcp_internal *si;
  1925. if (!lport->tt.fcp_cmd_send)
  1926. lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1927. if (!lport->tt.fcp_cleanup)
  1928. lport->tt.fcp_cleanup = fc_fcp_cleanup;
  1929. if (!lport->tt.fcp_abort_io)
  1930. lport->tt.fcp_abort_io = fc_fcp_abort_io;
  1931. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  1932. if (!si)
  1933. return -ENOMEM;
  1934. lport->scsi_priv = si;
  1935. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  1936. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  1937. if (!si->scsi_pkt_pool) {
  1938. rc = -ENOMEM;
  1939. goto free_internal;
  1940. }
  1941. return 0;
  1942. free_internal:
  1943. kfree(si);
  1944. return rc;
  1945. }
  1946. EXPORT_SYMBOL(fc_fcp_init);