fc_fcp.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  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. fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
  454. if (!fp)
  455. return -ENOMEM;
  456. data = fc_frame_header_get(fp) + 1;
  457. fh_parm_offset = frame_offset;
  458. fr_max_payload(fp) = fsp->max_payload;
  459. }
  460. sg_bytes = min(tlen, sg->length - offset);
  461. if (using_sg) {
  462. get_page(sg_page(sg));
  463. skb_fill_page_desc(fp_skb(fp),
  464. skb_shinfo(fp_skb(fp))->nr_frags,
  465. sg_page(sg), sg->offset + offset,
  466. sg_bytes);
  467. fp_skb(fp)->data_len += sg_bytes;
  468. fr_len(fp) += sg_bytes;
  469. fp_skb(fp)->truesize += PAGE_SIZE;
  470. } else {
  471. size_t off = offset + sg->offset;
  472. /*
  473. * The scatterlist item may be bigger than PAGE_SIZE,
  474. * but we must not cross pages inside the kmap.
  475. */
  476. sg_bytes = min(sg_bytes, (size_t) (PAGE_SIZE -
  477. (off & ~PAGE_MASK)));
  478. page_addr = kmap_atomic(sg_page(sg) +
  479. (off >> PAGE_SHIFT),
  480. KM_SOFTIRQ0);
  481. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  482. sg_bytes);
  483. kunmap_atomic(page_addr, KM_SOFTIRQ0);
  484. data += sg_bytes;
  485. }
  486. offset += sg_bytes;
  487. frame_offset += sg_bytes;
  488. tlen -= sg_bytes;
  489. remaining -= sg_bytes;
  490. if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
  491. (tlen))
  492. continue;
  493. /*
  494. * Send sequence with transfer sequence initiative in case
  495. * this is last FCP frame of the sequence.
  496. */
  497. if (remaining == 0)
  498. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  499. ep = fc_seq_exch(seq);
  500. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  501. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  502. /*
  503. * send fragment using for a sequence.
  504. */
  505. error = lport->tt.seq_send(lport, seq, fp);
  506. if (error) {
  507. WARN_ON(1); /* send error should be rare */
  508. fc_fcp_retry_cmd(fsp);
  509. return 0;
  510. }
  511. fp = NULL;
  512. }
  513. fsp->xfer_len += seq_blen; /* premature count? */
  514. return 0;
  515. }
  516. /**
  517. * fc_fcp_abts_resp() - Send an ABTS response
  518. * @fsp: The FCP packet that is being aborted
  519. * @fp: The response frame
  520. */
  521. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  522. {
  523. int ba_done = 1;
  524. struct fc_ba_rjt *brp;
  525. struct fc_frame_header *fh;
  526. fh = fc_frame_header_get(fp);
  527. switch (fh->fh_r_ctl) {
  528. case FC_RCTL_BA_ACC:
  529. break;
  530. case FC_RCTL_BA_RJT:
  531. brp = fc_frame_payload_get(fp, sizeof(*brp));
  532. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  533. break;
  534. /* fall thru */
  535. default:
  536. /*
  537. * we will let the command timeout
  538. * and scsi-ml recover in this case,
  539. * therefore cleared the ba_done flag.
  540. */
  541. ba_done = 0;
  542. }
  543. if (ba_done) {
  544. fsp->state |= FC_SRB_ABORTED;
  545. fsp->state &= ~FC_SRB_ABORT_PENDING;
  546. if (fsp->wait_for_comp)
  547. complete(&fsp->tm_done);
  548. else
  549. fc_fcp_complete_locked(fsp);
  550. }
  551. }
  552. /**
  553. * fc_fcp_reduce_can_queue() - Reduce the can_queue value for a local port
  554. * @lport: The local port to reduce can_queue on
  555. *
  556. * If we are getting memory allocation failures, then we may
  557. * be trying to execute too many commands. We let the running
  558. * commands complete or timeout, then try again with a reduced
  559. * can_queue. Eventually we will hit the point where we run
  560. * on all reserved structs.
  561. */
  562. static void fc_fcp_reduce_can_queue(struct fc_lport *lport)
  563. {
  564. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  565. unsigned long flags;
  566. int can_queue;
  567. spin_lock_irqsave(lport->host->host_lock, flags);
  568. if (si->throttled)
  569. goto done;
  570. si->throttled = 1;
  571. can_queue = lport->host->can_queue;
  572. can_queue >>= 1;
  573. if (!can_queue)
  574. can_queue = 1;
  575. lport->host->can_queue = can_queue;
  576. shost_printk(KERN_ERR, lport->host, "libfc: Could not allocate frame.\n"
  577. "Reducing can_queue to %d.\n", can_queue);
  578. done:
  579. spin_unlock_irqrestore(lport->host->host_lock, flags);
  580. }
  581. /**
  582. * fc_fcp_recv() - Reveive an FCP frame
  583. * @seq: The sequence the frame is on
  584. * @fp: The received frame
  585. * @arg: The related FCP packet
  586. *
  587. * Context: Called from Soft IRQ context. Can not be called
  588. * holding the FCP packet list lock.
  589. */
  590. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  591. {
  592. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  593. struct fc_lport *lport = fsp->lp;
  594. struct fc_frame_header *fh;
  595. struct fcp_txrdy *dd;
  596. u8 r_ctl;
  597. int rc = 0;
  598. if (IS_ERR(fp))
  599. goto errout;
  600. fh = fc_frame_header_get(fp);
  601. r_ctl = fh->fh_r_ctl;
  602. if (!(lport->state & LPORT_ST_READY))
  603. goto out;
  604. if (fc_fcp_lock_pkt(fsp))
  605. goto out;
  606. fsp->last_pkt_time = jiffies;
  607. if (fh->fh_type == FC_TYPE_BLS) {
  608. fc_fcp_abts_resp(fsp, fp);
  609. goto unlock;
  610. }
  611. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING))
  612. goto unlock;
  613. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  614. /*
  615. * received XFER RDY from the target
  616. * need to send data to the target
  617. */
  618. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  619. dd = fc_frame_payload_get(fp, sizeof(*dd));
  620. WARN_ON(!dd);
  621. rc = fc_fcp_send_data(fsp, seq,
  622. (size_t) ntohl(dd->ft_data_ro),
  623. (size_t) ntohl(dd->ft_burst_len));
  624. if (!rc)
  625. seq->rec_data = fsp->xfer_len;
  626. else if (rc == -ENOMEM)
  627. fsp->state |= FC_SRB_NOMEM;
  628. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  629. /*
  630. * received a DATA frame
  631. * next we will copy the data to the system buffer
  632. */
  633. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  634. fc_fcp_recv_data(fsp, fp);
  635. seq->rec_data = fsp->xfer_contig_end;
  636. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  637. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  638. fc_fcp_resp(fsp, fp);
  639. } else {
  640. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  641. }
  642. unlock:
  643. fc_fcp_unlock_pkt(fsp);
  644. out:
  645. fc_frame_free(fp);
  646. errout:
  647. if (IS_ERR(fp))
  648. fc_fcp_error(fsp, fp);
  649. else if (rc == -ENOMEM)
  650. fc_fcp_reduce_can_queue(lport);
  651. }
  652. /**
  653. * fc_fcp_resp() - Handler for FCP responses
  654. * @fsp: The FCP packet the response is for
  655. * @fp: The response frame
  656. */
  657. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  658. {
  659. struct fc_frame_header *fh;
  660. struct fcp_resp *fc_rp;
  661. struct fcp_resp_ext *rp_ex;
  662. struct fcp_resp_rsp_info *fc_rp_info;
  663. u32 plen;
  664. u32 expected_len;
  665. u32 respl = 0;
  666. u32 snsl = 0;
  667. u8 flags = 0;
  668. plen = fr_len(fp);
  669. fh = (struct fc_frame_header *)fr_hdr(fp);
  670. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  671. goto len_err;
  672. plen -= sizeof(*fh);
  673. fc_rp = (struct fcp_resp *)(fh + 1);
  674. fsp->cdb_status = fc_rp->fr_status;
  675. flags = fc_rp->fr_flags;
  676. fsp->scsi_comp_flags = flags;
  677. expected_len = fsp->data_len;
  678. /* if ddp, update xfer len */
  679. fc_fcp_ddp_done(fsp);
  680. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  681. rp_ex = (void *)(fc_rp + 1);
  682. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  683. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  684. goto len_err;
  685. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  686. if (flags & FCP_RSP_LEN_VAL) {
  687. respl = ntohl(rp_ex->fr_rsp_len);
  688. if (respl != sizeof(*fc_rp_info))
  689. goto len_err;
  690. if (fsp->wait_for_comp) {
  691. /* Abuse cdb_status for rsp code */
  692. fsp->cdb_status = fc_rp_info->rsp_code;
  693. complete(&fsp->tm_done);
  694. /*
  695. * tmfs will not have any scsi cmd so
  696. * exit here
  697. */
  698. return;
  699. } else
  700. goto err;
  701. }
  702. if (flags & FCP_SNS_LEN_VAL) {
  703. snsl = ntohl(rp_ex->fr_sns_len);
  704. if (snsl > SCSI_SENSE_BUFFERSIZE)
  705. snsl = SCSI_SENSE_BUFFERSIZE;
  706. memcpy(fsp->cmd->sense_buffer,
  707. (char *)fc_rp_info + respl, snsl);
  708. }
  709. }
  710. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  711. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  712. goto len_err;
  713. if (flags & FCP_RESID_UNDER) {
  714. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  715. /*
  716. * The cmnd->underflow is the minimum number of
  717. * bytes that must be transfered for this
  718. * command. Provided a sense condition is not
  719. * present, make sure the actual amount
  720. * transferred is at least the underflow value
  721. * or fail.
  722. */
  723. if (!(flags & FCP_SNS_LEN_VAL) &&
  724. (fc_rp->fr_status == 0) &&
  725. (scsi_bufflen(fsp->cmd) -
  726. fsp->scsi_resid) < fsp->cmd->underflow)
  727. goto err;
  728. expected_len -= fsp->scsi_resid;
  729. } else {
  730. fsp->status_code = FC_ERROR;
  731. }
  732. }
  733. }
  734. fsp->state |= FC_SRB_RCV_STATUS;
  735. /*
  736. * Check for missing or extra data frames.
  737. */
  738. if (unlikely(fsp->xfer_len != expected_len)) {
  739. if (fsp->xfer_len < expected_len) {
  740. /*
  741. * Some data may be queued locally,
  742. * Wait a at least one jiffy to see if it is delivered.
  743. * If this expires without data, we may do SRR.
  744. */
  745. fc_fcp_timer_set(fsp, 2);
  746. return;
  747. }
  748. fsp->status_code = FC_DATA_OVRRUN;
  749. FC_FCP_DBG(fsp, "tgt %6x xfer len %zx greater than expected, "
  750. "len %x, data len %x\n",
  751. fsp->rport->port_id,
  752. fsp->xfer_len, expected_len, fsp->data_len);
  753. }
  754. fc_fcp_complete_locked(fsp);
  755. return;
  756. len_err:
  757. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  758. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  759. err:
  760. fsp->status_code = FC_ERROR;
  761. fc_fcp_complete_locked(fsp);
  762. }
  763. /**
  764. * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
  765. * fcp_pkt lock held
  766. * @fsp: The FCP packet to be completed
  767. *
  768. * This function may sleep if a timer is pending. The packet lock must be
  769. * held, and the host lock must not be held.
  770. */
  771. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  772. {
  773. struct fc_lport *lport = fsp->lp;
  774. struct fc_seq *seq;
  775. struct fc_exch *ep;
  776. u32 f_ctl;
  777. if (fsp->state & FC_SRB_ABORT_PENDING)
  778. return;
  779. if (fsp->state & FC_SRB_ABORTED) {
  780. if (!fsp->status_code)
  781. fsp->status_code = FC_CMD_ABORTED;
  782. } else {
  783. /*
  784. * Test for transport underrun, independent of response
  785. * underrun status.
  786. */
  787. if (fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  788. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  789. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  790. fsp->status_code = FC_DATA_UNDRUN;
  791. fsp->io_status = 0;
  792. }
  793. }
  794. seq = fsp->seq_ptr;
  795. if (seq) {
  796. fsp->seq_ptr = NULL;
  797. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  798. struct fc_frame *conf_frame;
  799. struct fc_seq *csp;
  800. csp = lport->tt.seq_start_next(seq);
  801. conf_frame = fc_frame_alloc(fsp->lp, 0);
  802. if (conf_frame) {
  803. f_ctl = FC_FC_SEQ_INIT;
  804. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  805. ep = fc_seq_exch(seq);
  806. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  807. ep->did, ep->sid,
  808. FC_TYPE_FCP, f_ctl, 0);
  809. lport->tt.seq_send(lport, csp, conf_frame);
  810. }
  811. }
  812. lport->tt.exch_done(seq);
  813. }
  814. fc_io_compl(fsp);
  815. }
  816. /**
  817. * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
  818. * @fsp: The FCP packet whose exchanges should be canceled
  819. * @error: The reason for the cancellation
  820. */
  821. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  822. {
  823. struct fc_lport *lport = fsp->lp;
  824. if (fsp->seq_ptr) {
  825. lport->tt.exch_done(fsp->seq_ptr);
  826. fsp->seq_ptr = NULL;
  827. }
  828. fsp->status_code = error;
  829. }
  830. /**
  831. * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
  832. * @lport: The local port whose exchanges should be canceled
  833. * @id: The target's ID
  834. * @lun: The LUN
  835. * @error: The reason for cancellation
  836. *
  837. * If lun or id is -1, they are ignored.
  838. */
  839. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
  840. unsigned int lun, int error)
  841. {
  842. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  843. struct fc_fcp_pkt *fsp;
  844. struct scsi_cmnd *sc_cmd;
  845. unsigned long flags;
  846. spin_lock_irqsave(lport->host->host_lock, flags);
  847. restart:
  848. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  849. sc_cmd = fsp->cmd;
  850. if (id != -1 && scmd_id(sc_cmd) != id)
  851. continue;
  852. if (lun != -1 && sc_cmd->device->lun != lun)
  853. continue;
  854. fc_fcp_pkt_hold(fsp);
  855. spin_unlock_irqrestore(lport->host->host_lock, flags);
  856. if (!fc_fcp_lock_pkt(fsp)) {
  857. fc_fcp_cleanup_cmd(fsp, error);
  858. fc_io_compl(fsp);
  859. fc_fcp_unlock_pkt(fsp);
  860. }
  861. fc_fcp_pkt_release(fsp);
  862. spin_lock_irqsave(lport->host->host_lock, flags);
  863. /*
  864. * while we dropped the lock multiple pkts could
  865. * have been released, so we have to start over.
  866. */
  867. goto restart;
  868. }
  869. spin_unlock_irqrestore(lport->host->host_lock, flags);
  870. }
  871. /**
  872. * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
  873. * @lport: The local port whose exchanges are to be aborted
  874. */
  875. static void fc_fcp_abort_io(struct fc_lport *lport)
  876. {
  877. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
  878. }
  879. /**
  880. * fc_fcp_pkt_send() - Send a fcp_pkt
  881. * @lport: The local port to send the FCP packet on
  882. * @fsp: The FCP packet to send
  883. *
  884. * Return: Zero for success and -1 for failure
  885. * Locks: Called with the host lock and irqs disabled.
  886. */
  887. static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
  888. {
  889. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  890. int rc;
  891. fsp->cmd->SCp.ptr = (char *)fsp;
  892. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  893. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  894. int_to_scsilun(fsp->cmd->device->lun,
  895. (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  896. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  897. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  898. spin_unlock_irq(lport->host->host_lock);
  899. rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
  900. spin_lock_irq(lport->host->host_lock);
  901. if (rc)
  902. list_del(&fsp->list);
  903. return rc;
  904. }
  905. /**
  906. * fc_fcp_cmd_send() - Send a FCP command
  907. * @lport: The local port to send the command on
  908. * @fsp: The FCP packet the command is on
  909. * @resp: The handler for the response
  910. */
  911. static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  912. void (*resp)(struct fc_seq *,
  913. struct fc_frame *fp,
  914. void *arg))
  915. {
  916. struct fc_frame *fp;
  917. struct fc_seq *seq;
  918. struct fc_rport *rport;
  919. struct fc_rport_libfc_priv *rpriv;
  920. const size_t len = sizeof(fsp->cdb_cmd);
  921. int rc = 0;
  922. if (fc_fcp_lock_pkt(fsp))
  923. return 0;
  924. fp = fc_frame_alloc(lport, sizeof(fsp->cdb_cmd));
  925. if (!fp) {
  926. rc = -1;
  927. goto unlock;
  928. }
  929. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  930. fr_fsp(fp) = fsp;
  931. rport = fsp->rport;
  932. fsp->max_payload = rport->maxframe_size;
  933. rpriv = rport->dd_data;
  934. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  935. fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
  936. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  937. seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy,
  938. fsp, 0);
  939. if (!seq) {
  940. rc = -1;
  941. goto unlock;
  942. }
  943. fsp->last_pkt_time = jiffies;
  944. fsp->seq_ptr = seq;
  945. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  946. setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp);
  947. fc_fcp_timer_set(fsp,
  948. (fsp->tgt_flags & FC_RP_FLAGS_REC_SUPPORTED) ?
  949. FC_SCSI_REC_TOV : FC_SCSI_ER_TIMEOUT);
  950. unlock:
  951. fc_fcp_unlock_pkt(fsp);
  952. return rc;
  953. }
  954. /**
  955. * fc_fcp_error() - Handler for FCP layer errors
  956. * @fsp: The FCP packet the error is on
  957. * @fp: The frame that has errored
  958. */
  959. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  960. {
  961. int error = PTR_ERR(fp);
  962. if (fc_fcp_lock_pkt(fsp))
  963. return;
  964. if (error == -FC_EX_CLOSED) {
  965. fc_fcp_retry_cmd(fsp);
  966. goto unlock;
  967. }
  968. /*
  969. * clear abort pending, because the lower layer
  970. * decided to force completion.
  971. */
  972. fsp->state &= ~FC_SRB_ABORT_PENDING;
  973. fsp->status_code = FC_CMD_PLOGO;
  974. fc_fcp_complete_locked(fsp);
  975. unlock:
  976. fc_fcp_unlock_pkt(fsp);
  977. }
  978. /**
  979. * fc_fcp_pkt_abort() - Abort a fcp_pkt
  980. * @fsp: The FCP packet to abort on
  981. *
  982. * Called to send an abort and then wait for abort completion
  983. */
  984. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  985. {
  986. int rc = FAILED;
  987. if (fc_fcp_send_abort(fsp))
  988. return FAILED;
  989. init_completion(&fsp->tm_done);
  990. fsp->wait_for_comp = 1;
  991. spin_unlock_bh(&fsp->scsi_pkt_lock);
  992. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  993. spin_lock_bh(&fsp->scsi_pkt_lock);
  994. fsp->wait_for_comp = 0;
  995. if (!rc) {
  996. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  997. rc = FAILED;
  998. } else if (fsp->state & FC_SRB_ABORTED) {
  999. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  1000. rc = SUCCESS;
  1001. fc_fcp_complete_locked(fsp);
  1002. }
  1003. return rc;
  1004. }
  1005. /**
  1006. * fc_lun_reset_send() - Send LUN reset command
  1007. * @data: The FCP packet that identifies the LUN to be reset
  1008. */
  1009. static void fc_lun_reset_send(unsigned long data)
  1010. {
  1011. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1012. struct fc_lport *lport = fsp->lp;
  1013. if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
  1014. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1015. return;
  1016. if (fc_fcp_lock_pkt(fsp))
  1017. return;
  1018. setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp);
  1019. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1020. fc_fcp_unlock_pkt(fsp);
  1021. }
  1022. }
  1023. /**
  1024. * fc_lun_reset() - Send a LUN RESET command to a device
  1025. * and wait for the reply
  1026. * @lport: The local port to sent the comand on
  1027. * @fsp: The FCP packet that identifies the LUN to be reset
  1028. * @id: The SCSI command ID
  1029. * @lun: The LUN ID to be reset
  1030. */
  1031. static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1032. unsigned int id, unsigned int lun)
  1033. {
  1034. int rc;
  1035. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1036. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1037. int_to_scsilun(lun, (struct scsi_lun *)fsp->cdb_cmd.fc_lun);
  1038. fsp->wait_for_comp = 1;
  1039. init_completion(&fsp->tm_done);
  1040. fc_lun_reset_send((unsigned long)fsp);
  1041. /*
  1042. * wait for completion of reset
  1043. * after that make sure all commands are terminated
  1044. */
  1045. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1046. spin_lock_bh(&fsp->scsi_pkt_lock);
  1047. fsp->state |= FC_SRB_COMPL;
  1048. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1049. del_timer_sync(&fsp->timer);
  1050. spin_lock_bh(&fsp->scsi_pkt_lock);
  1051. if (fsp->seq_ptr) {
  1052. lport->tt.exch_done(fsp->seq_ptr);
  1053. fsp->seq_ptr = NULL;
  1054. }
  1055. fsp->wait_for_comp = 0;
  1056. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1057. if (!rc) {
  1058. FC_SCSI_DBG(lport, "lun reset failed\n");
  1059. return FAILED;
  1060. }
  1061. /* cdb_status holds the tmf's rsp code */
  1062. if (fsp->cdb_status != FCP_TMF_CMPL)
  1063. return FAILED;
  1064. FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
  1065. fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
  1066. return SUCCESS;
  1067. }
  1068. /**
  1069. * fc_tm_done() - Task Managment response handler
  1070. * @seq: The sequence that the response is on
  1071. * @fp: The response frame
  1072. * @arg: The FCP packet the response is for
  1073. */
  1074. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1075. {
  1076. struct fc_fcp_pkt *fsp = arg;
  1077. struct fc_frame_header *fh;
  1078. if (IS_ERR(fp)) {
  1079. /*
  1080. * If there is an error just let it timeout or wait
  1081. * for TMF to be aborted if it timedout.
  1082. *
  1083. * scsi-eh will escalate for when either happens.
  1084. */
  1085. return;
  1086. }
  1087. if (fc_fcp_lock_pkt(fsp))
  1088. return;
  1089. /*
  1090. * raced with eh timeout handler.
  1091. */
  1092. if (!fsp->seq_ptr || !fsp->wait_for_comp) {
  1093. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1094. return;
  1095. }
  1096. fh = fc_frame_header_get(fp);
  1097. if (fh->fh_type != FC_TYPE_BLS)
  1098. fc_fcp_resp(fsp, fp);
  1099. fsp->seq_ptr = NULL;
  1100. fsp->lp->tt.exch_done(seq);
  1101. fc_frame_free(fp);
  1102. fc_fcp_unlock_pkt(fsp);
  1103. }
  1104. /**
  1105. * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
  1106. * @lport: The local port to be cleaned up
  1107. */
  1108. static void fc_fcp_cleanup(struct fc_lport *lport)
  1109. {
  1110. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
  1111. }
  1112. /**
  1113. * fc_fcp_timeout() - Handler for fcp_pkt timeouts
  1114. * @data: The FCP packet that has timed out
  1115. *
  1116. * If REC is supported then just issue it and return. The REC exchange will
  1117. * complete or time out and recovery can continue at that point. Otherwise,
  1118. * if the response has been received without all the data it has been
  1119. * ER_TIMEOUT since the response was received. If the response has not been
  1120. * received we see if data was received recently. If it has been then we
  1121. * continue waiting, otherwise, we abort the command.
  1122. */
  1123. static void fc_fcp_timeout(unsigned long data)
  1124. {
  1125. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data;
  1126. struct fc_rport *rport = fsp->rport;
  1127. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1128. if (fc_fcp_lock_pkt(fsp))
  1129. return;
  1130. if (fsp->cdb_cmd.fc_tm_flags)
  1131. goto unlock;
  1132. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1133. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1134. fc_fcp_rec(fsp);
  1135. else if (time_after_eq(fsp->last_pkt_time + (FC_SCSI_ER_TIMEOUT / 2),
  1136. jiffies))
  1137. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1138. else if (fsp->state & FC_SRB_RCV_STATUS)
  1139. fc_fcp_complete_locked(fsp);
  1140. else
  1141. fc_timeout_error(fsp);
  1142. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1143. unlock:
  1144. fc_fcp_unlock_pkt(fsp);
  1145. }
  1146. /**
  1147. * fc_fcp_rec() - Send a REC ELS request
  1148. * @fsp: The FCP packet to send the REC request on
  1149. */
  1150. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1151. {
  1152. struct fc_lport *lport;
  1153. struct fc_frame *fp;
  1154. struct fc_rport *rport;
  1155. struct fc_rport_libfc_priv *rpriv;
  1156. lport = fsp->lp;
  1157. rport = fsp->rport;
  1158. rpriv = rport->dd_data;
  1159. if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
  1160. fsp->status_code = FC_HRD_ERROR;
  1161. fsp->io_status = 0;
  1162. fc_fcp_complete_locked(fsp);
  1163. return;
  1164. }
  1165. fp = fc_frame_alloc(lport, sizeof(struct fc_els_rec));
  1166. if (!fp)
  1167. goto retry;
  1168. fr_seq(fp) = fsp->seq_ptr;
  1169. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1170. fc_host_port_id(rpriv->local_port->host), FC_TYPE_ELS,
  1171. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1172. if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
  1173. fc_fcp_rec_resp, fsp,
  1174. jiffies_to_msecs(FC_SCSI_REC_TOV))) {
  1175. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1176. return;
  1177. }
  1178. retry:
  1179. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1180. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1181. else
  1182. fc_timeout_error(fsp);
  1183. }
  1184. /**
  1185. * fc_fcp_rec_resp() - Handler for REC ELS responses
  1186. * @seq: The sequence the response is on
  1187. * @fp: The response frame
  1188. * @arg: The FCP packet the response is on
  1189. *
  1190. * If the response is a reject then the scsi layer will handle
  1191. * the timeout. If the response is a LS_ACC then if the I/O was not completed
  1192. * set the timeout and return. If the I/O was completed then complete the
  1193. * exchange and tell the SCSI layer.
  1194. */
  1195. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1196. {
  1197. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1198. struct fc_els_rec_acc *recp;
  1199. struct fc_els_ls_rjt *rjt;
  1200. u32 e_stat;
  1201. u8 opcode;
  1202. u32 offset;
  1203. enum dma_data_direction data_dir;
  1204. enum fc_rctl r_ctl;
  1205. struct fc_rport_libfc_priv *rpriv;
  1206. if (IS_ERR(fp)) {
  1207. fc_fcp_rec_error(fsp, fp);
  1208. return;
  1209. }
  1210. if (fc_fcp_lock_pkt(fsp))
  1211. goto out;
  1212. fsp->recov_retry = 0;
  1213. opcode = fc_frame_payload_op(fp);
  1214. if (opcode == ELS_LS_RJT) {
  1215. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1216. switch (rjt->er_reason) {
  1217. default:
  1218. FC_FCP_DBG(fsp, "device %x unexpected REC reject "
  1219. "reason %d expl %d\n",
  1220. fsp->rport->port_id, rjt->er_reason,
  1221. rjt->er_explan);
  1222. /* fall through */
  1223. case ELS_RJT_UNSUP:
  1224. FC_FCP_DBG(fsp, "device does not support REC\n");
  1225. rpriv = fsp->rport->dd_data;
  1226. /*
  1227. * if we do not spport RECs or got some bogus
  1228. * reason then resetup timer so we check for
  1229. * making progress.
  1230. */
  1231. rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1232. fc_fcp_timer_set(fsp, FC_SCSI_ER_TIMEOUT);
  1233. break;
  1234. case ELS_RJT_LOGIC:
  1235. case ELS_RJT_UNAB:
  1236. /*
  1237. * If no data transfer, the command frame got dropped
  1238. * so we just retry. If data was transferred, we
  1239. * lost the response but the target has no record,
  1240. * so we abort and retry.
  1241. */
  1242. if (rjt->er_explan == ELS_EXPL_OXID_RXID &&
  1243. fsp->xfer_len == 0) {
  1244. fc_fcp_retry_cmd(fsp);
  1245. break;
  1246. }
  1247. fc_timeout_error(fsp);
  1248. break;
  1249. }
  1250. } else if (opcode == ELS_LS_ACC) {
  1251. if (fsp->state & FC_SRB_ABORTED)
  1252. goto unlock_out;
  1253. data_dir = fsp->cmd->sc_data_direction;
  1254. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1255. offset = ntohl(recp->reca_fc4value);
  1256. e_stat = ntohl(recp->reca_e_stat);
  1257. if (e_stat & ESB_ST_COMPLETE) {
  1258. /*
  1259. * The exchange is complete.
  1260. *
  1261. * For output, we must've lost the response.
  1262. * For input, all data must've been sent.
  1263. * We lost may have lost the response
  1264. * (and a confirmation was requested) and maybe
  1265. * some data.
  1266. *
  1267. * If all data received, send SRR
  1268. * asking for response. If partial data received,
  1269. * or gaps, SRR requests data at start of gap.
  1270. * Recovery via SRR relies on in-order-delivery.
  1271. */
  1272. if (data_dir == DMA_TO_DEVICE) {
  1273. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1274. } else if (fsp->xfer_contig_end == offset) {
  1275. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1276. } else {
  1277. offset = fsp->xfer_contig_end;
  1278. r_ctl = FC_RCTL_DD_SOL_DATA;
  1279. }
  1280. fc_fcp_srr(fsp, r_ctl, offset);
  1281. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1282. /*
  1283. * The remote port has the initiative, so just
  1284. * keep waiting for it to complete.
  1285. */
  1286. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1287. } else {
  1288. /*
  1289. * The exchange is incomplete, we have seq. initiative.
  1290. * Lost response with requested confirmation,
  1291. * lost confirmation, lost transfer ready or
  1292. * lost write data.
  1293. *
  1294. * For output, if not all data was received, ask
  1295. * for transfer ready to be repeated.
  1296. *
  1297. * If we received or sent all the data, send SRR to
  1298. * request response.
  1299. *
  1300. * If we lost a response, we may have lost some read
  1301. * data as well.
  1302. */
  1303. r_ctl = FC_RCTL_DD_SOL_DATA;
  1304. if (data_dir == DMA_TO_DEVICE) {
  1305. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1306. if (offset < fsp->data_len)
  1307. r_ctl = FC_RCTL_DD_DATA_DESC;
  1308. } else if (offset == fsp->xfer_contig_end) {
  1309. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1310. } else if (fsp->xfer_contig_end < offset) {
  1311. offset = fsp->xfer_contig_end;
  1312. }
  1313. fc_fcp_srr(fsp, r_ctl, offset);
  1314. }
  1315. }
  1316. unlock_out:
  1317. fc_fcp_unlock_pkt(fsp);
  1318. out:
  1319. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1320. fc_frame_free(fp);
  1321. }
  1322. /**
  1323. * fc_fcp_rec_error() - Handler for REC errors
  1324. * @fsp: The FCP packet the error is on
  1325. * @fp: The REC frame
  1326. */
  1327. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1328. {
  1329. int error = PTR_ERR(fp);
  1330. if (fc_fcp_lock_pkt(fsp))
  1331. goto out;
  1332. switch (error) {
  1333. case -FC_EX_CLOSED:
  1334. fc_fcp_retry_cmd(fsp);
  1335. break;
  1336. default:
  1337. FC_FCP_DBG(fsp, "REC %p fid %x error unexpected error %d\n",
  1338. fsp, fsp->rport->port_id, error);
  1339. fsp->status_code = FC_CMD_PLOGO;
  1340. /* fall through */
  1341. case -FC_EX_TIMEOUT:
  1342. /*
  1343. * Assume REC or LS_ACC was lost.
  1344. * The exchange manager will have aborted REC, so retry.
  1345. */
  1346. FC_FCP_DBG(fsp, "REC fid %x error error %d retry %d/%d\n",
  1347. fsp->rport->port_id, error, fsp->recov_retry,
  1348. FC_MAX_RECOV_RETRY);
  1349. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1350. fc_fcp_rec(fsp);
  1351. else
  1352. fc_timeout_error(fsp);
  1353. break;
  1354. }
  1355. fc_fcp_unlock_pkt(fsp);
  1356. out:
  1357. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1358. }
  1359. /**
  1360. * fc_timeout_error() - Handler for fcp_pkt timeouts
  1361. * @fsp: The FCP packt that has timed out
  1362. */
  1363. static void fc_timeout_error(struct fc_fcp_pkt *fsp)
  1364. {
  1365. fsp->status_code = FC_CMD_TIME_OUT;
  1366. fsp->cdb_status = 0;
  1367. fsp->io_status = 0;
  1368. /*
  1369. * if this fails then we let the scsi command timer fire and
  1370. * scsi-ml escalate.
  1371. */
  1372. fc_fcp_send_abort(fsp);
  1373. }
  1374. /**
  1375. * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
  1376. * @fsp: The FCP packet the SRR is to be sent on
  1377. * @r_ctl: The R_CTL field for the SRR request
  1378. * This is called after receiving status but insufficient data, or
  1379. * when expecting status but the request has timed out.
  1380. */
  1381. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1382. {
  1383. struct fc_lport *lport = fsp->lp;
  1384. struct fc_rport *rport;
  1385. struct fc_rport_libfc_priv *rpriv;
  1386. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1387. struct fc_seq *seq;
  1388. struct fcp_srr *srr;
  1389. struct fc_frame *fp;
  1390. u8 cdb_op;
  1391. rport = fsp->rport;
  1392. rpriv = rport->dd_data;
  1393. cdb_op = fsp->cdb_cmd.fc_cdb[0];
  1394. if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
  1395. rpriv->rp_state != RPORT_ST_READY)
  1396. goto retry; /* shouldn't happen */
  1397. fp = fc_frame_alloc(lport, sizeof(*srr));
  1398. if (!fp)
  1399. goto retry;
  1400. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1401. memset(srr, 0, sizeof(*srr));
  1402. srr->srr_op = ELS_SRR;
  1403. srr->srr_ox_id = htons(ep->oxid);
  1404. srr->srr_rx_id = htons(ep->rxid);
  1405. srr->srr_r_ctl = r_ctl;
  1406. srr->srr_rel_off = htonl(offset);
  1407. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1408. fc_host_port_id(rpriv->local_port->host), FC_TYPE_FCP,
  1409. FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT, 0);
  1410. seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, NULL,
  1411. fsp, jiffies_to_msecs(FC_SCSI_REC_TOV));
  1412. if (!seq)
  1413. goto retry;
  1414. fsp->recov_seq = seq;
  1415. fsp->xfer_len = offset;
  1416. fsp->xfer_contig_end = offset;
  1417. fsp->state &= ~FC_SRB_RCV_STATUS;
  1418. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1419. return;
  1420. retry:
  1421. fc_fcp_retry_cmd(fsp);
  1422. }
  1423. /**
  1424. * fc_fcp_srr_resp() - Handler for SRR response
  1425. * @seq: The sequence the SRR is on
  1426. * @fp: The SRR frame
  1427. * @arg: The FCP packet the SRR is on
  1428. */
  1429. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1430. {
  1431. struct fc_fcp_pkt *fsp = arg;
  1432. struct fc_frame_header *fh;
  1433. if (IS_ERR(fp)) {
  1434. fc_fcp_srr_error(fsp, fp);
  1435. return;
  1436. }
  1437. if (fc_fcp_lock_pkt(fsp))
  1438. goto out;
  1439. fh = fc_frame_header_get(fp);
  1440. /*
  1441. * BUG? fc_fcp_srr_error calls exch_done which would release
  1442. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1443. * then fc_exch_timeout would be sending an abort. The exch_done
  1444. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1445. * an abort response though.
  1446. */
  1447. if (fh->fh_type == FC_TYPE_BLS) {
  1448. fc_fcp_unlock_pkt(fsp);
  1449. return;
  1450. }
  1451. fsp->recov_seq = NULL;
  1452. switch (fc_frame_payload_op(fp)) {
  1453. case ELS_LS_ACC:
  1454. fsp->recov_retry = 0;
  1455. fc_fcp_timer_set(fsp, FC_SCSI_REC_TOV);
  1456. break;
  1457. case ELS_LS_RJT:
  1458. default:
  1459. fc_timeout_error(fsp);
  1460. break;
  1461. }
  1462. fc_fcp_unlock_pkt(fsp);
  1463. fsp->lp->tt.exch_done(seq);
  1464. out:
  1465. fc_frame_free(fp);
  1466. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1467. }
  1468. /**
  1469. * fc_fcp_srr_error() - Handler for SRR errors
  1470. * @fsp: The FCP packet that the SRR error is on
  1471. * @fp: The SRR frame
  1472. */
  1473. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1474. {
  1475. if (fc_fcp_lock_pkt(fsp))
  1476. goto out;
  1477. fsp->lp->tt.exch_done(fsp->recov_seq);
  1478. fsp->recov_seq = NULL;
  1479. switch (PTR_ERR(fp)) {
  1480. case -FC_EX_TIMEOUT:
  1481. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1482. fc_fcp_rec(fsp);
  1483. else
  1484. fc_timeout_error(fsp);
  1485. break;
  1486. case -FC_EX_CLOSED: /* e.g., link failure */
  1487. /* fall through */
  1488. default:
  1489. fc_fcp_retry_cmd(fsp);
  1490. break;
  1491. }
  1492. fc_fcp_unlock_pkt(fsp);
  1493. out:
  1494. fc_fcp_pkt_release(fsp); /* drop hold for outstanding SRR */
  1495. }
  1496. /**
  1497. * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
  1498. * @lport: The local port to be checked
  1499. */
  1500. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
  1501. {
  1502. /* lock ? */
  1503. return (lport->state == LPORT_ST_READY) &&
  1504. lport->link_up && !lport->qfull;
  1505. }
  1506. /**
  1507. * fc_queuecommand() - The queuecommand function of the SCSI template
  1508. * @cmd: The scsi_cmnd to be executed
  1509. * @done: The callback function to be called when the scsi_cmnd is complete
  1510. *
  1511. * This is the i/o strategy routine, called by the SCSI layer. This routine
  1512. * is called with the host_lock held.
  1513. */
  1514. int fc_queuecommand(struct scsi_cmnd *sc_cmd, void (*done)(struct scsi_cmnd *))
  1515. {
  1516. struct fc_lport *lport;
  1517. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1518. struct fc_fcp_pkt *fsp;
  1519. struct fc_rport_libfc_priv *rpriv;
  1520. int rval;
  1521. int rc = 0;
  1522. struct fcoe_dev_stats *stats;
  1523. lport = shost_priv(sc_cmd->device->host);
  1524. rval = fc_remote_port_chkready(rport);
  1525. if (rval) {
  1526. sc_cmd->result = rval;
  1527. done(sc_cmd);
  1528. goto out;
  1529. }
  1530. if (!*(struct fc_remote_port **)rport->dd_data) {
  1531. /*
  1532. * rport is transitioning from blocked/deleted to
  1533. * online
  1534. */
  1535. sc_cmd->result = DID_IMM_RETRY << 16;
  1536. done(sc_cmd);
  1537. goto out;
  1538. }
  1539. rpriv = rport->dd_data;
  1540. if (!fc_fcp_lport_queue_ready(lport)) {
  1541. rc = SCSI_MLQUEUE_HOST_BUSY;
  1542. goto out;
  1543. }
  1544. fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
  1545. if (fsp == NULL) {
  1546. rc = SCSI_MLQUEUE_HOST_BUSY;
  1547. goto out;
  1548. }
  1549. /*
  1550. * build the libfc request pkt
  1551. */
  1552. fsp->cmd = sc_cmd; /* save the cmd */
  1553. fsp->lp = lport; /* save the softc ptr */
  1554. fsp->rport = rport; /* set the remote port ptr */
  1555. fsp->xfer_ddp = FC_XID_UNKNOWN;
  1556. sc_cmd->scsi_done = done;
  1557. /*
  1558. * set up the transfer length
  1559. */
  1560. fsp->data_len = scsi_bufflen(sc_cmd);
  1561. fsp->xfer_len = 0;
  1562. /*
  1563. * setup the data direction
  1564. */
  1565. stats = fc_lport_get_stats(lport);
  1566. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1567. fsp->req_flags = FC_SRB_READ;
  1568. stats->InputRequests++;
  1569. stats->InputMegabytes = fsp->data_len;
  1570. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1571. fsp->req_flags = FC_SRB_WRITE;
  1572. stats->OutputRequests++;
  1573. stats->OutputMegabytes = fsp->data_len;
  1574. } else {
  1575. fsp->req_flags = 0;
  1576. stats->ControlRequests++;
  1577. }
  1578. fsp->tgt_flags = rpriv->flags;
  1579. init_timer(&fsp->timer);
  1580. fsp->timer.data = (unsigned long)fsp;
  1581. /*
  1582. * send it to the lower layer
  1583. * if we get -1 return then put the request in the pending
  1584. * queue.
  1585. */
  1586. rval = fc_fcp_pkt_send(lport, fsp);
  1587. if (rval != 0) {
  1588. fsp->state = FC_SRB_FREE;
  1589. fc_fcp_pkt_release(fsp);
  1590. rc = SCSI_MLQUEUE_HOST_BUSY;
  1591. }
  1592. out:
  1593. return rc;
  1594. }
  1595. EXPORT_SYMBOL(fc_queuecommand);
  1596. /**
  1597. * fc_io_compl() - Handle responses for completed commands
  1598. * @fsp: The FCP packet that is complete
  1599. *
  1600. * Translates fcp_pkt errors to a Linux SCSI errors.
  1601. * The fcp packet lock must be held when calling.
  1602. */
  1603. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1604. {
  1605. struct fc_fcp_internal *si;
  1606. struct scsi_cmnd *sc_cmd;
  1607. struct fc_lport *lport;
  1608. unsigned long flags;
  1609. /* release outstanding ddp context */
  1610. fc_fcp_ddp_done(fsp);
  1611. fsp->state |= FC_SRB_COMPL;
  1612. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1613. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1614. del_timer_sync(&fsp->timer);
  1615. spin_lock_bh(&fsp->scsi_pkt_lock);
  1616. }
  1617. lport = fsp->lp;
  1618. si = fc_get_scsi_internal(lport);
  1619. spin_lock_irqsave(lport->host->host_lock, flags);
  1620. if (!fsp->cmd) {
  1621. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1622. return;
  1623. }
  1624. /*
  1625. * if a command timed out while we had to try and throttle IO
  1626. * and it is now getting cleaned up, then we are about to
  1627. * try again so clear the throttled flag incase we get more
  1628. * time outs.
  1629. */
  1630. if (si->throttled && fsp->state & FC_SRB_NOMEM)
  1631. si->throttled = 0;
  1632. sc_cmd = fsp->cmd;
  1633. fsp->cmd = NULL;
  1634. if (!sc_cmd->SCp.ptr) {
  1635. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1636. return;
  1637. }
  1638. CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status;
  1639. switch (fsp->status_code) {
  1640. case FC_COMPLETE:
  1641. if (fsp->cdb_status == 0) {
  1642. /*
  1643. * good I/O status
  1644. */
  1645. sc_cmd->result = DID_OK << 16;
  1646. if (fsp->scsi_resid)
  1647. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1648. } else {
  1649. /*
  1650. * transport level I/O was ok but scsi
  1651. * has non zero status
  1652. */
  1653. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1654. }
  1655. break;
  1656. case FC_ERROR:
  1657. sc_cmd->result = DID_ERROR << 16;
  1658. break;
  1659. case FC_DATA_UNDRUN:
  1660. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1661. /*
  1662. * scsi status is good but transport level
  1663. * underrun.
  1664. */
  1665. sc_cmd->result = (fsp->state & FC_SRB_RCV_STATUS ?
  1666. DID_OK : DID_ERROR) << 16;
  1667. } else {
  1668. /*
  1669. * scsi got underrun, this is an error
  1670. */
  1671. CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid;
  1672. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1673. }
  1674. break;
  1675. case FC_DATA_OVRRUN:
  1676. /*
  1677. * overrun is an error
  1678. */
  1679. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1680. break;
  1681. case FC_CMD_ABORTED:
  1682. sc_cmd->result = (DID_ERROR << 16) | fsp->io_status;
  1683. break;
  1684. case FC_CMD_TIME_OUT:
  1685. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1686. break;
  1687. case FC_CMD_RESET:
  1688. sc_cmd->result = (DID_RESET << 16);
  1689. break;
  1690. case FC_HRD_ERROR:
  1691. sc_cmd->result = (DID_NO_CONNECT << 16);
  1692. break;
  1693. default:
  1694. sc_cmd->result = (DID_ERROR << 16);
  1695. break;
  1696. }
  1697. list_del(&fsp->list);
  1698. sc_cmd->SCp.ptr = NULL;
  1699. sc_cmd->scsi_done(sc_cmd);
  1700. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1701. /* release ref from initial allocation in queue command */
  1702. fc_fcp_pkt_release(fsp);
  1703. }
  1704. /**
  1705. * fc_eh_abort() - Abort a command
  1706. * @sc_cmd: The SCSI command to abort
  1707. *
  1708. * From SCSI host template.
  1709. * Send an ABTS to the target device and wait for the response.
  1710. */
  1711. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1712. {
  1713. struct fc_fcp_pkt *fsp;
  1714. struct fc_lport *lport;
  1715. int rc = FAILED;
  1716. unsigned long flags;
  1717. lport = shost_priv(sc_cmd->device->host);
  1718. if (lport->state != LPORT_ST_READY)
  1719. return rc;
  1720. else if (!lport->link_up)
  1721. return rc;
  1722. spin_lock_irqsave(lport->host->host_lock, flags);
  1723. fsp = CMD_SP(sc_cmd);
  1724. if (!fsp) {
  1725. /* command completed while scsi eh was setting up */
  1726. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1727. return SUCCESS;
  1728. }
  1729. /* grab a ref so the fsp and sc_cmd cannot be relased from under us */
  1730. fc_fcp_pkt_hold(fsp);
  1731. spin_unlock_irqrestore(lport->host->host_lock, flags);
  1732. if (fc_fcp_lock_pkt(fsp)) {
  1733. /* completed while we were waiting for timer to be deleted */
  1734. rc = SUCCESS;
  1735. goto release_pkt;
  1736. }
  1737. rc = fc_fcp_pkt_abort(fsp);
  1738. fc_fcp_unlock_pkt(fsp);
  1739. release_pkt:
  1740. fc_fcp_pkt_release(fsp);
  1741. return rc;
  1742. }
  1743. EXPORT_SYMBOL(fc_eh_abort);
  1744. /**
  1745. * fc_eh_device_reset() - Reset a single LUN
  1746. * @sc_cmd: The SCSI command which identifies the device whose
  1747. * LUN is to be reset
  1748. *
  1749. * Set from SCSI host template.
  1750. */
  1751. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1752. {
  1753. struct fc_lport *lport;
  1754. struct fc_fcp_pkt *fsp;
  1755. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1756. int rc = FAILED;
  1757. int rval;
  1758. rval = fc_remote_port_chkready(rport);
  1759. if (rval)
  1760. goto out;
  1761. lport = shost_priv(sc_cmd->device->host);
  1762. if (lport->state != LPORT_ST_READY)
  1763. return rc;
  1764. FC_SCSI_DBG(lport, "Resetting rport (%6x)\n", rport->port_id);
  1765. fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
  1766. if (fsp == NULL) {
  1767. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1768. sc_cmd->result = DID_NO_CONNECT << 16;
  1769. goto out;
  1770. }
  1771. /*
  1772. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1773. * the sc passed in is not setup for execution like when sent
  1774. * through the queuecommand callout.
  1775. */
  1776. fsp->lp = lport; /* save the softc ptr */
  1777. fsp->rport = rport; /* set the remote port ptr */
  1778. /*
  1779. * flush outstanding commands
  1780. */
  1781. rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1782. fsp->state = FC_SRB_FREE;
  1783. fc_fcp_pkt_release(fsp);
  1784. out:
  1785. return rc;
  1786. }
  1787. EXPORT_SYMBOL(fc_eh_device_reset);
  1788. /**
  1789. * fc_eh_host_reset() - Reset a Scsi_Host.
  1790. * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
  1791. */
  1792. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1793. {
  1794. struct Scsi_Host *shost = sc_cmd->device->host;
  1795. struct fc_lport *lport = shost_priv(shost);
  1796. unsigned long wait_tmo;
  1797. FC_SCSI_DBG(lport, "Resetting host\n");
  1798. lport->tt.lport_reset(lport);
  1799. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1800. while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
  1801. wait_tmo))
  1802. msleep(1000);
  1803. if (fc_fcp_lport_queue_ready(lport)) {
  1804. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1805. "on port (%6x)\n", fc_host_port_id(lport->host));
  1806. return SUCCESS;
  1807. } else {
  1808. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1809. "port (%6x) is not ready.\n",
  1810. fc_host_port_id(lport->host));
  1811. return FAILED;
  1812. }
  1813. }
  1814. EXPORT_SYMBOL(fc_eh_host_reset);
  1815. /**
  1816. * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
  1817. * @sdev: The SCSI device that identifies the SCSI host
  1818. *
  1819. * Configures queue depth based on host's cmd_per_len. If not set
  1820. * then we use the libfc default.
  1821. */
  1822. int fc_slave_alloc(struct scsi_device *sdev)
  1823. {
  1824. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1825. if (!rport || fc_remote_port_chkready(rport))
  1826. return -ENXIO;
  1827. if (sdev->tagged_supported)
  1828. scsi_activate_tcq(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
  1829. else
  1830. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
  1831. FC_FCP_DFLT_QUEUE_DEPTH);
  1832. return 0;
  1833. }
  1834. EXPORT_SYMBOL(fc_slave_alloc);
  1835. /**
  1836. * fc_change_queue_depth() - Change a device's queue depth
  1837. * @sdev: The SCSI device whose queue depth is to change
  1838. * @qdepth: The new queue depth
  1839. * @reason: The resason for the change
  1840. */
  1841. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  1842. {
  1843. switch (reason) {
  1844. case SCSI_QDEPTH_DEFAULT:
  1845. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1846. break;
  1847. case SCSI_QDEPTH_QFULL:
  1848. scsi_track_queue_full(sdev, qdepth);
  1849. break;
  1850. case SCSI_QDEPTH_RAMP_UP:
  1851. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1852. break;
  1853. default:
  1854. return -EOPNOTSUPP;
  1855. }
  1856. return sdev->queue_depth;
  1857. }
  1858. EXPORT_SYMBOL(fc_change_queue_depth);
  1859. /**
  1860. * fc_change_queue_type() - Change a device's queue type
  1861. * @sdev: The SCSI device whose queue depth is to change
  1862. * @tag_type: Identifier for queue type
  1863. */
  1864. int fc_change_queue_type(struct scsi_device *sdev, int tag_type)
  1865. {
  1866. if (sdev->tagged_supported) {
  1867. scsi_set_tag_type(sdev, tag_type);
  1868. if (tag_type)
  1869. scsi_activate_tcq(sdev, sdev->queue_depth);
  1870. else
  1871. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1872. } else
  1873. tag_type = 0;
  1874. return tag_type;
  1875. }
  1876. EXPORT_SYMBOL(fc_change_queue_type);
  1877. /**
  1878. * fc_fcp_destory() - Tear down the FCP layer for a given local port
  1879. * @lport: The local port that no longer needs the FCP layer
  1880. */
  1881. void fc_fcp_destroy(struct fc_lport *lport)
  1882. {
  1883. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  1884. if (!list_empty(&si->scsi_pkt_queue))
  1885. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  1886. "port (%6x)\n", fc_host_port_id(lport->host));
  1887. mempool_destroy(si->scsi_pkt_pool);
  1888. kfree(si);
  1889. lport->scsi_priv = NULL;
  1890. }
  1891. EXPORT_SYMBOL(fc_fcp_destroy);
  1892. int fc_setup_fcp()
  1893. {
  1894. int rc = 0;
  1895. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  1896. sizeof(struct fc_fcp_pkt),
  1897. 0, SLAB_HWCACHE_ALIGN, NULL);
  1898. if (!scsi_pkt_cachep) {
  1899. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  1900. "module load failed!");
  1901. rc = -ENOMEM;
  1902. }
  1903. return rc;
  1904. }
  1905. void fc_destroy_fcp()
  1906. {
  1907. if (scsi_pkt_cachep)
  1908. kmem_cache_destroy(scsi_pkt_cachep);
  1909. }
  1910. /**
  1911. * fc_fcp_init() - Initialize the FCP layer for a local port
  1912. * @lport: The local port to initialize the exchange layer for
  1913. */
  1914. int fc_fcp_init(struct fc_lport *lport)
  1915. {
  1916. int rc;
  1917. struct fc_fcp_internal *si;
  1918. if (!lport->tt.fcp_cmd_send)
  1919. lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
  1920. if (!lport->tt.fcp_cleanup)
  1921. lport->tt.fcp_cleanup = fc_fcp_cleanup;
  1922. if (!lport->tt.fcp_abort_io)
  1923. lport->tt.fcp_abort_io = fc_fcp_abort_io;
  1924. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  1925. if (!si)
  1926. return -ENOMEM;
  1927. lport->scsi_priv = si;
  1928. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  1929. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  1930. if (!si->scsi_pkt_pool) {
  1931. rc = -ENOMEM;
  1932. goto free_internal;
  1933. }
  1934. return 0;
  1935. free_internal:
  1936. kfree(si);
  1937. return rc;
  1938. }
  1939. EXPORT_SYMBOL(fc_fcp_init);