libiscsi.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/delay.h>
  27. #include <asm/unaligned.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/iscsi_proto.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_iscsi.h>
  38. #include <scsi/libiscsi.h>
  39. struct iscsi_session *
  40. class_to_transport_session(struct iscsi_cls_session *cls_session)
  41. {
  42. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  43. return iscsi_hostdata(shost->hostdata);
  44. }
  45. EXPORT_SYMBOL_GPL(class_to_transport_session);
  46. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  47. #define SNA32_CHECK 2147483648UL
  48. static int iscsi_sna_lt(u32 n1, u32 n2)
  49. {
  50. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  51. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  52. }
  53. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  54. static int iscsi_sna_lte(u32 n1, u32 n2)
  55. {
  56. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  57. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  58. }
  59. void
  60. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  61. {
  62. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  63. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  64. /*
  65. * standard specifies this check for when to update expected and
  66. * max sequence numbers
  67. */
  68. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  69. return;
  70. if (exp_cmdsn != session->exp_cmdsn &&
  71. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  72. session->exp_cmdsn = exp_cmdsn;
  73. if (max_cmdsn != session->max_cmdsn &&
  74. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  75. session->max_cmdsn = max_cmdsn;
  76. /*
  77. * if the window closed with IO queued, then kick the
  78. * xmit thread
  79. */
  80. if (!list_empty(&session->leadconn->xmitqueue) ||
  81. __kfifo_len(session->leadconn->mgmtqueue))
  82. scsi_queue_work(session->host,
  83. &session->leadconn->xmitwork);
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  87. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  88. struct iscsi_data *hdr)
  89. {
  90. struct iscsi_conn *conn = ctask->conn;
  91. memset(hdr, 0, sizeof(struct iscsi_data));
  92. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  93. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  94. ctask->unsol_datasn++;
  95. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  96. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  97. hdr->itt = ctask->hdr->itt;
  98. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  99. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  100. if (ctask->unsol_count > conn->max_xmit_dlength) {
  101. hton24(hdr->dlength, conn->max_xmit_dlength);
  102. ctask->data_count = conn->max_xmit_dlength;
  103. ctask->unsol_offset += ctask->data_count;
  104. hdr->flags = 0;
  105. } else {
  106. hton24(hdr->dlength, ctask->unsol_count);
  107. ctask->data_count = ctask->unsol_count;
  108. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  112. /**
  113. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  114. * @ctask: iscsi cmd task
  115. *
  116. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  117. * fields like dlength or final based on how much data it sends
  118. */
  119. static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  120. {
  121. struct iscsi_conn *conn = ctask->conn;
  122. struct iscsi_session *session = conn->session;
  123. struct iscsi_cmd *hdr = ctask->hdr;
  124. struct scsi_cmnd *sc = ctask->sc;
  125. hdr->opcode = ISCSI_OP_SCSI_CMD;
  126. hdr->flags = ISCSI_ATTR_SIMPLE;
  127. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  128. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  129. hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
  130. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  131. session->cmdsn++;
  132. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  133. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  134. if (sc->cmd_len < MAX_COMMAND_SIZE)
  135. memset(&hdr->cdb[sc->cmd_len], 0,
  136. MAX_COMMAND_SIZE - sc->cmd_len);
  137. ctask->data_count = 0;
  138. ctask->imm_count = 0;
  139. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  140. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  141. /*
  142. * Write counters:
  143. *
  144. * imm_count bytes to be sent right after
  145. * SCSI PDU Header
  146. *
  147. * unsol_count bytes(as Data-Out) to be sent
  148. * without R2T ack right after
  149. * immediate data
  150. *
  151. * r2t_data_count bytes to be sent via R2T ack's
  152. *
  153. * pad_count bytes to be sent as zero-padding
  154. */
  155. ctask->unsol_count = 0;
  156. ctask->unsol_offset = 0;
  157. ctask->unsol_datasn = 0;
  158. if (session->imm_data_en) {
  159. if (scsi_bufflen(sc) >= session->first_burst)
  160. ctask->imm_count = min(session->first_burst,
  161. conn->max_xmit_dlength);
  162. else
  163. ctask->imm_count = min(scsi_bufflen(sc),
  164. conn->max_xmit_dlength);
  165. hton24(ctask->hdr->dlength, ctask->imm_count);
  166. } else
  167. zero_data(ctask->hdr->dlength);
  168. if (!session->initial_r2t_en) {
  169. ctask->unsol_count = min((session->first_burst),
  170. (scsi_bufflen(sc))) - ctask->imm_count;
  171. ctask->unsol_offset = ctask->imm_count;
  172. }
  173. if (!ctask->unsol_count)
  174. /* No unsolicit Data-Out's */
  175. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  176. } else {
  177. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  178. zero_data(hdr->dlength);
  179. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  180. hdr->flags |= ISCSI_FLAG_CMD_READ;
  181. }
  182. conn->scsicmd_pdus_cnt++;
  183. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
  184. "cmdsn %d win %d]\n",
  185. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  186. conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
  187. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  188. }
  189. /**
  190. * iscsi_complete_command - return command back to scsi-ml
  191. * @ctask: iscsi cmd task
  192. *
  193. * Must be called with session lock.
  194. * This function returns the scsi command to scsi-ml and returns
  195. * the cmd task to the pool of available cmd tasks.
  196. */
  197. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  198. {
  199. struct iscsi_session *session = ctask->conn->session;
  200. struct scsi_cmnd *sc = ctask->sc;
  201. ctask->state = ISCSI_TASK_COMPLETED;
  202. ctask->sc = NULL;
  203. /* SCSI eh reuses commands to verify us */
  204. sc->SCp.ptr = NULL;
  205. list_del_init(&ctask->running);
  206. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  207. sc->scsi_done(sc);
  208. }
  209. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  210. {
  211. atomic_inc(&ctask->refcount);
  212. }
  213. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  214. {
  215. if (atomic_dec_and_test(&ctask->refcount))
  216. iscsi_complete_command(ctask);
  217. }
  218. /**
  219. * iscsi_cmd_rsp - SCSI Command Response processing
  220. * @conn: iscsi connection
  221. * @hdr: iscsi header
  222. * @ctask: scsi command task
  223. * @data: cmd data buffer
  224. * @datalen: len of buffer
  225. *
  226. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  227. * then completes the command and task.
  228. **/
  229. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  230. struct iscsi_cmd_task *ctask, char *data,
  231. int datalen)
  232. {
  233. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  234. struct iscsi_session *session = conn->session;
  235. struct scsi_cmnd *sc = ctask->sc;
  236. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  237. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  238. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  239. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  240. sc->result = DID_ERROR << 16;
  241. goto out;
  242. }
  243. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  244. uint16_t senselen;
  245. if (datalen < 2) {
  246. invalid_datalen:
  247. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  248. "invalid data buffer size of %d\n", datalen);
  249. sc->result = DID_BAD_TARGET << 16;
  250. goto out;
  251. }
  252. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  253. if (datalen < senselen)
  254. goto invalid_datalen;
  255. memcpy(sc->sense_buffer, data + 2,
  256. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  257. debug_scsi("copied %d bytes of sense\n",
  258. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  259. }
  260. if (sc->sc_data_direction == DMA_TO_DEVICE)
  261. goto out;
  262. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  263. int res_count = be32_to_cpu(rhdr->residual_count);
  264. if (res_count > 0 && res_count <= scsi_bufflen(sc))
  265. scsi_set_resid(sc, res_count);
  266. else
  267. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  268. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  269. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  270. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  271. scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
  272. out:
  273. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  274. (long)sc, sc->result, ctask->itt);
  275. conn->scsirsp_pdus_cnt++;
  276. __iscsi_put_ctask(ctask);
  277. }
  278. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  279. {
  280. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  281. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  282. conn->tmfrsp_pdus_cnt++;
  283. if (conn->tmabort_state != TMABORT_INITIAL)
  284. return;
  285. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  286. conn->tmabort_state = TMABORT_SUCCESS;
  287. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  288. conn->tmabort_state = TMABORT_NOT_FOUND;
  289. else
  290. conn->tmabort_state = TMABORT_FAILED;
  291. wake_up(&conn->ehwait);
  292. }
  293. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  294. char *data, int datalen)
  295. {
  296. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  297. struct iscsi_hdr rejected_pdu;
  298. uint32_t itt;
  299. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  300. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  301. if (ntoh24(reject->dlength) > datalen)
  302. return ISCSI_ERR_PROTO;
  303. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  304. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  305. itt = get_itt(rejected_pdu.itt);
  306. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  307. "due to DataDigest error.\n", itt,
  308. rejected_pdu.opcode);
  309. }
  310. }
  311. return 0;
  312. }
  313. /**
  314. * __iscsi_complete_pdu - complete pdu
  315. * @conn: iscsi conn
  316. * @hdr: iscsi header
  317. * @data: data buffer
  318. * @datalen: len of data buffer
  319. *
  320. * Completes pdu processing by freeing any resources allocated at
  321. * queuecommand or send generic. session lock must be held and verify
  322. * itt must have been called.
  323. */
  324. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  325. char *data, int datalen)
  326. {
  327. struct iscsi_session *session = conn->session;
  328. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  329. struct iscsi_cmd_task *ctask;
  330. struct iscsi_mgmt_task *mtask;
  331. uint32_t itt;
  332. if (hdr->itt != RESERVED_ITT)
  333. itt = get_itt(hdr->itt);
  334. else
  335. itt = ~0U;
  336. if (itt < session->cmds_max) {
  337. ctask = session->cmds[itt];
  338. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  339. opcode, conn->id, ctask->itt, datalen);
  340. switch(opcode) {
  341. case ISCSI_OP_SCSI_CMD_RSP:
  342. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  343. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  344. datalen);
  345. break;
  346. case ISCSI_OP_SCSI_DATA_IN:
  347. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  348. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  349. conn->scsirsp_pdus_cnt++;
  350. __iscsi_put_ctask(ctask);
  351. }
  352. break;
  353. case ISCSI_OP_R2T:
  354. /* LLD handles this for now */
  355. break;
  356. default:
  357. rc = ISCSI_ERR_BAD_OPCODE;
  358. break;
  359. }
  360. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  361. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  362. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  363. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  364. opcode, conn->id, mtask->itt, datalen);
  365. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  366. switch(opcode) {
  367. case ISCSI_OP_LOGOUT_RSP:
  368. if (datalen) {
  369. rc = ISCSI_ERR_PROTO;
  370. break;
  371. }
  372. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  373. /* fall through */
  374. case ISCSI_OP_LOGIN_RSP:
  375. case ISCSI_OP_TEXT_RSP:
  376. /*
  377. * login related PDU's exp_statsn is handled in
  378. * userspace
  379. */
  380. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  381. rc = ISCSI_ERR_CONN_FAILED;
  382. list_del(&mtask->running);
  383. if (conn->login_mtask != mtask)
  384. __kfifo_put(session->mgmtpool.queue,
  385. (void*)&mtask, sizeof(void*));
  386. break;
  387. case ISCSI_OP_SCSI_TMFUNC_RSP:
  388. if (datalen) {
  389. rc = ISCSI_ERR_PROTO;
  390. break;
  391. }
  392. iscsi_tmf_rsp(conn, hdr);
  393. break;
  394. case ISCSI_OP_NOOP_IN:
  395. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
  396. rc = ISCSI_ERR_PROTO;
  397. break;
  398. }
  399. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  400. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  401. rc = ISCSI_ERR_CONN_FAILED;
  402. list_del(&mtask->running);
  403. if (conn->login_mtask != mtask)
  404. __kfifo_put(session->mgmtpool.queue,
  405. (void*)&mtask, sizeof(void*));
  406. break;
  407. default:
  408. rc = ISCSI_ERR_BAD_OPCODE;
  409. break;
  410. }
  411. } else if (itt == ~0U) {
  412. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  413. switch(opcode) {
  414. case ISCSI_OP_NOOP_IN:
  415. if (datalen) {
  416. rc = ISCSI_ERR_PROTO;
  417. break;
  418. }
  419. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  420. break;
  421. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  422. rc = ISCSI_ERR_CONN_FAILED;
  423. break;
  424. case ISCSI_OP_REJECT:
  425. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  426. break;
  427. case ISCSI_OP_ASYNC_EVENT:
  428. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  429. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  430. rc = ISCSI_ERR_CONN_FAILED;
  431. break;
  432. default:
  433. rc = ISCSI_ERR_BAD_OPCODE;
  434. break;
  435. }
  436. } else
  437. rc = ISCSI_ERR_BAD_ITT;
  438. return rc;
  439. }
  440. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  441. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  442. char *data, int datalen)
  443. {
  444. int rc;
  445. spin_lock(&conn->session->lock);
  446. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  447. spin_unlock(&conn->session->lock);
  448. return rc;
  449. }
  450. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  451. /* verify itt (itt encoding: age+cid+itt) */
  452. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  453. uint32_t *ret_itt)
  454. {
  455. struct iscsi_session *session = conn->session;
  456. struct iscsi_cmd_task *ctask;
  457. uint32_t itt;
  458. if (hdr->itt != RESERVED_ITT) {
  459. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  460. (session->age << ISCSI_AGE_SHIFT)) {
  461. printk(KERN_ERR "iscsi: received itt %x expected "
  462. "session age (%x)\n", (__force u32)hdr->itt,
  463. session->age & ISCSI_AGE_MASK);
  464. return ISCSI_ERR_BAD_ITT;
  465. }
  466. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  467. (conn->id << ISCSI_CID_SHIFT)) {
  468. printk(KERN_ERR "iscsi: received itt %x, expected "
  469. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  470. return ISCSI_ERR_BAD_ITT;
  471. }
  472. itt = get_itt(hdr->itt);
  473. } else
  474. itt = ~0U;
  475. if (itt < session->cmds_max) {
  476. ctask = session->cmds[itt];
  477. if (!ctask->sc) {
  478. printk(KERN_INFO "iscsi: dropping ctask with "
  479. "itt 0x%x\n", ctask->itt);
  480. /* force drop */
  481. return ISCSI_ERR_NO_SCSI_CMD;
  482. }
  483. if (ctask->sc->SCp.phase != session->age) {
  484. printk(KERN_ERR "iscsi: ctask's session age %d, "
  485. "expected %d\n", ctask->sc->SCp.phase,
  486. session->age);
  487. return ISCSI_ERR_SESSION_FAILED;
  488. }
  489. }
  490. *ret_itt = itt;
  491. return 0;
  492. }
  493. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  494. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  495. {
  496. struct iscsi_session *session = conn->session;
  497. unsigned long flags;
  498. spin_lock_irqsave(&session->lock, flags);
  499. if (session->state == ISCSI_STATE_FAILED) {
  500. spin_unlock_irqrestore(&session->lock, flags);
  501. return;
  502. }
  503. if (conn->stop_stage == 0)
  504. session->state = ISCSI_STATE_FAILED;
  505. spin_unlock_irqrestore(&session->lock, flags);
  506. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  507. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  508. iscsi_conn_error(conn->cls_conn, err);
  509. }
  510. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  511. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  512. struct iscsi_mgmt_task *mtask)
  513. {
  514. struct iscsi_session *session = conn->session;
  515. struct iscsi_hdr *hdr = mtask->hdr;
  516. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  517. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  518. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  519. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  520. /*
  521. * pre-format CmdSN for outgoing PDU.
  522. */
  523. nop->cmdsn = cpu_to_be32(session->cmdsn);
  524. if (hdr->itt != RESERVED_ITT) {
  525. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  526. /*
  527. * TODO: We always use immediate, so we never hit this.
  528. * If we start to send tmfs or nops as non-immediate then
  529. * we should start checking the cmdsn numbers for mgmt tasks.
  530. */
  531. if (conn->c_stage == ISCSI_CONN_STARTED &&
  532. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  533. session->queued_cmdsn++;
  534. session->cmdsn++;
  535. }
  536. }
  537. if (session->tt->init_mgmt_task)
  538. session->tt->init_mgmt_task(conn, mtask);
  539. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  540. hdr->opcode, hdr->itt, mtask->data_count);
  541. }
  542. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  543. {
  544. struct iscsi_hdr *hdr = conn->mtask->hdr;
  545. int rc, was_logout = 0;
  546. spin_unlock_bh(&conn->session->lock);
  547. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
  548. conn->session->state = ISCSI_STATE_IN_RECOVERY;
  549. iscsi_block_session(session_to_cls(conn->session));
  550. was_logout = 1;
  551. }
  552. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  553. spin_lock_bh(&conn->session->lock);
  554. if (rc)
  555. return rc;
  556. /* done with this in-progress mtask */
  557. conn->mtask = NULL;
  558. if (was_logout) {
  559. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  560. return -ENODATA;
  561. }
  562. return 0;
  563. }
  564. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  565. {
  566. struct iscsi_session *session = conn->session;
  567. /*
  568. * Check for iSCSI window and take care of CmdSN wrap-around
  569. */
  570. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  571. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  572. "CmdSN %u/%u\n", session->exp_cmdsn,
  573. session->max_cmdsn, session->cmdsn,
  574. session->queued_cmdsn);
  575. return -ENOSPC;
  576. }
  577. return 0;
  578. }
  579. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  580. {
  581. struct iscsi_cmd_task *ctask = conn->ctask;
  582. int rc = 0;
  583. /*
  584. * serialize with TMF AbortTask
  585. */
  586. if (ctask->state == ISCSI_TASK_ABORTING)
  587. goto done;
  588. __iscsi_get_ctask(ctask);
  589. spin_unlock_bh(&conn->session->lock);
  590. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  591. spin_lock_bh(&conn->session->lock);
  592. __iscsi_put_ctask(ctask);
  593. done:
  594. if (!rc)
  595. /* done with this ctask */
  596. conn->ctask = NULL;
  597. return rc;
  598. }
  599. /**
  600. * iscsi_data_xmit - xmit any command into the scheduled connection
  601. * @conn: iscsi connection
  602. *
  603. * Notes:
  604. * The function can return -EAGAIN in which case the caller must
  605. * re-schedule it again later or recover. '0' return code means
  606. * successful xmit.
  607. **/
  608. static int iscsi_data_xmit(struct iscsi_conn *conn)
  609. {
  610. int rc = 0;
  611. spin_lock_bh(&conn->session->lock);
  612. if (unlikely(conn->suspend_tx)) {
  613. debug_scsi("conn %d Tx suspended!\n", conn->id);
  614. spin_unlock_bh(&conn->session->lock);
  615. return -ENODATA;
  616. }
  617. if (conn->ctask) {
  618. rc = iscsi_xmit_ctask(conn);
  619. if (rc)
  620. goto again;
  621. }
  622. if (conn->mtask) {
  623. rc = iscsi_xmit_mtask(conn);
  624. if (rc)
  625. goto again;
  626. }
  627. /*
  628. * process mgmt pdus like nops before commands since we should
  629. * only have one nop-out as a ping from us and targets should not
  630. * overflow us with nop-ins
  631. */
  632. check_mgmt:
  633. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  634. sizeof(void*))) {
  635. iscsi_prep_mtask(conn, conn->mtask);
  636. list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
  637. rc = iscsi_xmit_mtask(conn);
  638. if (rc)
  639. goto again;
  640. }
  641. /* process command queue */
  642. while (!list_empty(&conn->xmitqueue)) {
  643. /*
  644. * iscsi tcp may readd the task to the xmitqueue to send
  645. * write data
  646. */
  647. conn->ctask = list_entry(conn->xmitqueue.next,
  648. struct iscsi_cmd_task, running);
  649. if (conn->ctask->state == ISCSI_TASK_PENDING) {
  650. iscsi_prep_scsi_cmd_pdu(conn->ctask);
  651. conn->session->tt->init_cmd_task(conn->ctask);
  652. }
  653. conn->ctask->state = ISCSI_TASK_RUNNING;
  654. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  655. rc = iscsi_xmit_ctask(conn);
  656. if (rc)
  657. goto again;
  658. /*
  659. * we could continuously get new ctask requests so
  660. * we need to check the mgmt queue for nops that need to
  661. * be sent to aviod starvation
  662. */
  663. if (__kfifo_len(conn->mgmtqueue))
  664. goto check_mgmt;
  665. }
  666. spin_unlock_bh(&conn->session->lock);
  667. return -ENODATA;
  668. again:
  669. if (unlikely(conn->suspend_tx))
  670. rc = -ENODATA;
  671. spin_unlock_bh(&conn->session->lock);
  672. return rc;
  673. }
  674. static void iscsi_xmitworker(struct work_struct *work)
  675. {
  676. struct iscsi_conn *conn =
  677. container_of(work, struct iscsi_conn, xmitwork);
  678. int rc;
  679. /*
  680. * serialize Xmit worker on a per-connection basis.
  681. */
  682. do {
  683. rc = iscsi_data_xmit(conn);
  684. } while (rc >= 0 || rc == -EAGAIN);
  685. }
  686. enum {
  687. FAILURE_BAD_HOST = 1,
  688. FAILURE_SESSION_FAILED,
  689. FAILURE_SESSION_FREED,
  690. FAILURE_WINDOW_CLOSED,
  691. FAILURE_OOM,
  692. FAILURE_SESSION_TERMINATE,
  693. FAILURE_SESSION_IN_RECOVERY,
  694. FAILURE_SESSION_RECOVERY_TIMEOUT,
  695. };
  696. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  697. {
  698. struct Scsi_Host *host;
  699. int reason = 0;
  700. struct iscsi_session *session;
  701. struct iscsi_conn *conn;
  702. struct iscsi_cmd_task *ctask = NULL;
  703. sc->scsi_done = done;
  704. sc->result = 0;
  705. sc->SCp.ptr = NULL;
  706. host = sc->device->host;
  707. session = iscsi_hostdata(host->hostdata);
  708. spin_lock(&session->lock);
  709. /*
  710. * ISCSI_STATE_FAILED is a temp. state. The recovery
  711. * code will decide what is best to do with command queued
  712. * during this time
  713. */
  714. if (session->state != ISCSI_STATE_LOGGED_IN &&
  715. session->state != ISCSI_STATE_FAILED) {
  716. /*
  717. * to handle the race between when we set the recovery state
  718. * and block the session we requeue here (commands could
  719. * be entering our queuecommand while a block is starting
  720. * up because the block code is not locked)
  721. */
  722. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  723. reason = FAILURE_SESSION_IN_RECOVERY;
  724. goto reject;
  725. }
  726. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  727. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  728. else if (session->state == ISCSI_STATE_TERMINATE)
  729. reason = FAILURE_SESSION_TERMINATE;
  730. else
  731. reason = FAILURE_SESSION_FREED;
  732. goto fault;
  733. }
  734. conn = session->leadconn;
  735. if (!conn) {
  736. reason = FAILURE_SESSION_FREED;
  737. goto fault;
  738. }
  739. if (iscsi_check_cmdsn_window_closed(conn)) {
  740. reason = FAILURE_WINDOW_CLOSED;
  741. goto reject;
  742. }
  743. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  744. sizeof(void*))) {
  745. reason = FAILURE_OOM;
  746. goto reject;
  747. }
  748. session->queued_cmdsn++;
  749. sc->SCp.phase = session->age;
  750. sc->SCp.ptr = (char *)ctask;
  751. atomic_set(&ctask->refcount, 1);
  752. ctask->state = ISCSI_TASK_PENDING;
  753. ctask->mtask = NULL;
  754. ctask->conn = conn;
  755. ctask->sc = sc;
  756. INIT_LIST_HEAD(&ctask->running);
  757. list_add_tail(&ctask->running, &conn->xmitqueue);
  758. spin_unlock(&session->lock);
  759. scsi_queue_work(host, &conn->xmitwork);
  760. return 0;
  761. reject:
  762. spin_unlock(&session->lock);
  763. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  764. return SCSI_MLQUEUE_HOST_BUSY;
  765. fault:
  766. spin_unlock(&session->lock);
  767. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  768. sc->cmnd[0], reason);
  769. sc->result = (DID_NO_CONNECT << 16);
  770. scsi_set_resid(sc, scsi_bufflen(sc));
  771. sc->scsi_done(sc);
  772. return 0;
  773. }
  774. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  775. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  776. {
  777. if (depth > ISCSI_MAX_CMD_PER_LUN)
  778. depth = ISCSI_MAX_CMD_PER_LUN;
  779. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  780. return sdev->queue_depth;
  781. }
  782. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  783. static struct iscsi_mgmt_task *
  784. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  785. char *data, uint32_t data_size)
  786. {
  787. struct iscsi_session *session = conn->session;
  788. struct iscsi_mgmt_task *mtask;
  789. if (session->state == ISCSI_STATE_TERMINATE)
  790. return NULL;
  791. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  792. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  793. /*
  794. * Login and Text are sent serially, in
  795. * request-followed-by-response sequence.
  796. * Same mtask can be used. Same ITT must be used.
  797. * Note that login_mtask is preallocated at conn_create().
  798. */
  799. mtask = conn->login_mtask;
  800. else {
  801. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  802. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  803. if (!__kfifo_get(session->mgmtpool.queue,
  804. (void*)&mtask, sizeof(void*)))
  805. return NULL;
  806. }
  807. if (data_size) {
  808. memcpy(mtask->data, data, data_size);
  809. mtask->data_count = data_size;
  810. } else
  811. mtask->data_count = 0;
  812. INIT_LIST_HEAD(&mtask->running);
  813. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  814. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  815. return mtask;
  816. }
  817. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  818. char *data, uint32_t data_size)
  819. {
  820. struct iscsi_conn *conn = cls_conn->dd_data;
  821. struct iscsi_session *session = conn->session;
  822. int err = 0;
  823. spin_lock_bh(&session->lock);
  824. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  825. err = -EPERM;
  826. spin_unlock_bh(&session->lock);
  827. scsi_queue_work(session->host, &conn->xmitwork);
  828. return err;
  829. }
  830. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  831. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  832. {
  833. struct iscsi_session *session = class_to_transport_session(cls_session);
  834. struct iscsi_conn *conn = session->leadconn;
  835. spin_lock_bh(&session->lock);
  836. if (session->state != ISCSI_STATE_LOGGED_IN) {
  837. session->state = ISCSI_STATE_RECOVERY_FAILED;
  838. if (conn)
  839. wake_up(&conn->ehwait);
  840. }
  841. spin_unlock_bh(&session->lock);
  842. }
  843. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  844. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  845. {
  846. struct Scsi_Host *host = sc->device->host;
  847. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  848. struct iscsi_conn *conn = session->leadconn;
  849. int fail_session = 0;
  850. spin_lock_bh(&session->lock);
  851. if (session->state == ISCSI_STATE_TERMINATE) {
  852. failed:
  853. debug_scsi("failing host reset: session terminated "
  854. "[CID %d age %d]\n", conn->id, session->age);
  855. spin_unlock_bh(&session->lock);
  856. return FAILED;
  857. }
  858. if (sc->SCp.phase == session->age) {
  859. debug_scsi("failing connection CID %d due to SCSI host reset\n",
  860. conn->id);
  861. fail_session = 1;
  862. }
  863. spin_unlock_bh(&session->lock);
  864. /*
  865. * we drop the lock here but the leadconn cannot be destoyed while
  866. * we are in the scsi eh
  867. */
  868. if (fail_session)
  869. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  870. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  871. wait_event_interruptible(conn->ehwait,
  872. session->state == ISCSI_STATE_TERMINATE ||
  873. session->state == ISCSI_STATE_LOGGED_IN ||
  874. session->state == ISCSI_STATE_RECOVERY_FAILED);
  875. if (signal_pending(current))
  876. flush_signals(current);
  877. spin_lock_bh(&session->lock);
  878. if (session->state == ISCSI_STATE_LOGGED_IN)
  879. printk(KERN_INFO "iscsi: host reset succeeded\n");
  880. else
  881. goto failed;
  882. spin_unlock_bh(&session->lock);
  883. return SUCCESS;
  884. }
  885. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  886. static void iscsi_tmabort_timedout(unsigned long data)
  887. {
  888. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  889. struct iscsi_conn *conn = ctask->conn;
  890. struct iscsi_session *session = conn->session;
  891. spin_lock(&session->lock);
  892. if (conn->tmabort_state == TMABORT_INITIAL) {
  893. conn->tmabort_state = TMABORT_TIMEDOUT;
  894. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  895. ctask->sc, ctask->itt);
  896. /* unblock eh_abort() */
  897. wake_up(&conn->ehwait);
  898. }
  899. spin_unlock(&session->lock);
  900. }
  901. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  902. struct iscsi_cmd_task *ctask)
  903. {
  904. struct iscsi_conn *conn = ctask->conn;
  905. struct iscsi_session *session = conn->session;
  906. struct iscsi_tm *hdr = &conn->tmhdr;
  907. /*
  908. * ctask timed out but session is OK requests must be serialized.
  909. */
  910. memset(hdr, 0, sizeof(struct iscsi_tm));
  911. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  912. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  913. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  914. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  915. hdr->rtt = ctask->hdr->itt;
  916. hdr->refcmdsn = ctask->hdr->cmdsn;
  917. ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  918. NULL, 0);
  919. if (!ctask->mtask) {
  920. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  921. debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
  922. return -EPERM;
  923. }
  924. ctask->state = ISCSI_TASK_ABORTING;
  925. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  926. if (conn->tmabort_state == TMABORT_INITIAL) {
  927. conn->tmfcmd_pdus_cnt++;
  928. conn->tmabort_timer.expires = 20*HZ + jiffies;
  929. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  930. conn->tmabort_timer.data = (unsigned long)ctask;
  931. add_timer(&conn->tmabort_timer);
  932. debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
  933. }
  934. spin_unlock_bh(&session->lock);
  935. scsi_queue_work(session->host, &conn->xmitwork);
  936. /*
  937. * block eh thread until:
  938. *
  939. * 1) abort response
  940. * 2) abort timeout
  941. * 3) session is terminated or restarted or userspace has
  942. * given up on recovery
  943. */
  944. wait_event_interruptible(conn->ehwait,
  945. sc->SCp.phase != session->age ||
  946. session->state != ISCSI_STATE_LOGGED_IN ||
  947. conn->tmabort_state != TMABORT_INITIAL);
  948. if (signal_pending(current))
  949. flush_signals(current);
  950. del_timer_sync(&conn->tmabort_timer);
  951. spin_lock_bh(&session->lock);
  952. return 0;
  953. }
  954. /*
  955. * session lock must be held
  956. */
  957. static struct iscsi_mgmt_task *
  958. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  959. {
  960. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  961. struct iscsi_mgmt_task *task;
  962. debug_scsi("searching %d tasks\n", nr_tasks);
  963. for (i = 0; i < nr_tasks; i++) {
  964. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  965. debug_scsi("check task %u\n", task->itt);
  966. if (task->itt == itt) {
  967. debug_scsi("matched task\n");
  968. return task;
  969. }
  970. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  971. }
  972. return NULL;
  973. }
  974. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  975. {
  976. struct iscsi_conn *conn = ctask->conn;
  977. struct iscsi_session *session = conn->session;
  978. if (!ctask->mtask)
  979. return -EINVAL;
  980. if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
  981. list_del(&ctask->mtask->running);
  982. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  983. sizeof(void*));
  984. ctask->mtask = NULL;
  985. return 0;
  986. }
  987. /*
  988. * session lock must be held
  989. */
  990. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  991. int err)
  992. {
  993. struct scsi_cmnd *sc;
  994. sc = ctask->sc;
  995. if (!sc)
  996. return;
  997. if (ctask->state == ISCSI_TASK_PENDING)
  998. /*
  999. * cmd never made it to the xmit thread, so we should not count
  1000. * the cmd in the sequencing
  1001. */
  1002. conn->session->queued_cmdsn--;
  1003. else
  1004. conn->session->tt->cleanup_cmd_task(conn, ctask);
  1005. iscsi_ctask_mtask_cleanup(ctask);
  1006. sc->result = err;
  1007. scsi_set_resid(sc, scsi_bufflen(sc));
  1008. if (conn->ctask == ctask)
  1009. conn->ctask = NULL;
  1010. /* release ref from queuecommand */
  1011. __iscsi_put_ctask(ctask);
  1012. }
  1013. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1014. {
  1015. struct iscsi_cmd_task *ctask;
  1016. struct iscsi_conn *conn;
  1017. struct iscsi_session *session;
  1018. int rc;
  1019. /*
  1020. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1021. * got the command.
  1022. */
  1023. if (!sc->SCp.ptr) {
  1024. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1025. return SUCCESS;
  1026. }
  1027. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1028. conn = ctask->conn;
  1029. session = conn->session;
  1030. conn->eh_abort_cnt++;
  1031. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1032. spin_lock_bh(&session->lock);
  1033. /*
  1034. * If we are not logged in or we have started a new session
  1035. * then let the host reset code handle this
  1036. */
  1037. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1038. sc->SCp.phase != session->age)
  1039. goto failed;
  1040. /* ctask completed before time out */
  1041. if (!ctask->sc) {
  1042. debug_scsi("sc completed while abort in progress\n");
  1043. goto success;
  1044. }
  1045. /* what should we do here ? */
  1046. if (conn->ctask == ctask) {
  1047. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1048. "Failing abort\n", sc, ctask->itt);
  1049. goto failed;
  1050. }
  1051. if (ctask->state == ISCSI_TASK_PENDING) {
  1052. fail_command(conn, ctask, DID_ABORT << 16);
  1053. goto success;
  1054. }
  1055. conn->tmabort_state = TMABORT_INITIAL;
  1056. rc = iscsi_exec_abort_task(sc, ctask);
  1057. if (rc || sc->SCp.phase != session->age ||
  1058. session->state != ISCSI_STATE_LOGGED_IN)
  1059. goto failed;
  1060. iscsi_ctask_mtask_cleanup(ctask);
  1061. switch (conn->tmabort_state) {
  1062. case TMABORT_SUCCESS:
  1063. spin_unlock_bh(&session->lock);
  1064. /*
  1065. * clean up task if aborted. grab the recv lock as a writer
  1066. */
  1067. write_lock_bh(conn->recv_lock);
  1068. spin_lock(&session->lock);
  1069. fail_command(conn, ctask, DID_ABORT << 16);
  1070. spin_unlock(&session->lock);
  1071. write_unlock_bh(conn->recv_lock);
  1072. /*
  1073. * make sure xmit thread is not still touching the
  1074. * ctask/scsi_cmnd
  1075. */
  1076. scsi_flush_work(session->host);
  1077. goto success_unlocked;
  1078. case TMABORT_NOT_FOUND:
  1079. if (!ctask->sc) {
  1080. /* ctask completed before tmf abort response */
  1081. debug_scsi("sc completed while abort in progress\n");
  1082. goto success;
  1083. }
  1084. /* fall through */
  1085. default:
  1086. /* timedout or failed */
  1087. spin_unlock_bh(&session->lock);
  1088. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1089. goto failed_unlocked;
  1090. }
  1091. success:
  1092. spin_unlock_bh(&session->lock);
  1093. success_unlocked:
  1094. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1095. return SUCCESS;
  1096. failed:
  1097. spin_unlock_bh(&session->lock);
  1098. failed_unlocked:
  1099. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1100. return FAILED;
  1101. }
  1102. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1103. int
  1104. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1105. {
  1106. int i;
  1107. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1108. if (*items == NULL)
  1109. return -ENOMEM;
  1110. q->max = max;
  1111. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1112. if (q->pool == NULL) {
  1113. kfree(*items);
  1114. return -ENOMEM;
  1115. }
  1116. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1117. GFP_KERNEL, NULL);
  1118. if (q->queue == ERR_PTR(-ENOMEM)) {
  1119. kfree(q->pool);
  1120. kfree(*items);
  1121. return -ENOMEM;
  1122. }
  1123. for (i = 0; i < max; i++) {
  1124. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1125. if (q->pool[i] == NULL) {
  1126. int j;
  1127. for (j = 0; j < i; j++)
  1128. kfree(q->pool[j]);
  1129. kfifo_free(q->queue);
  1130. kfree(q->pool);
  1131. kfree(*items);
  1132. return -ENOMEM;
  1133. }
  1134. memset(q->pool[i], 0, item_size);
  1135. (*items)[i] = q->pool[i];
  1136. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1137. }
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1141. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1142. {
  1143. int i;
  1144. for (i = 0; i < q->max; i++)
  1145. kfree(items[i]);
  1146. kfree(q->pool);
  1147. kfree(items);
  1148. }
  1149. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1150. /*
  1151. * iSCSI Session's hostdata organization:
  1152. *
  1153. * *------------------* <== hostdata_session(host->hostdata)
  1154. * | ptr to class sess|
  1155. * |------------------| <== iscsi_hostdata(host->hostdata)
  1156. * | iscsi_session |
  1157. * *------------------*
  1158. */
  1159. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1160. _sz % sizeof(unsigned long))
  1161. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1162. /**
  1163. * iscsi_session_setup - create iscsi cls session and host and session
  1164. * @scsit: scsi transport template
  1165. * @iscsit: iscsi transport template
  1166. * @cmds_max: scsi host can queue
  1167. * @qdepth: scsi host cmds per lun
  1168. * @cmd_task_size: LLD ctask private data size
  1169. * @mgmt_task_size: LLD mtask private data size
  1170. * @initial_cmdsn: initial CmdSN
  1171. * @hostno: host no allocated
  1172. *
  1173. * This can be used by software iscsi_transports that allocate
  1174. * a session per scsi host.
  1175. **/
  1176. struct iscsi_cls_session *
  1177. iscsi_session_setup(struct iscsi_transport *iscsit,
  1178. struct scsi_transport_template *scsit,
  1179. uint16_t cmds_max, uint16_t qdepth,
  1180. int cmd_task_size, int mgmt_task_size,
  1181. uint32_t initial_cmdsn, uint32_t *hostno)
  1182. {
  1183. struct Scsi_Host *shost;
  1184. struct iscsi_session *session;
  1185. struct iscsi_cls_session *cls_session;
  1186. int cmd_i;
  1187. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1188. if (qdepth != 0)
  1189. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1190. "Queue depth must be between 1 and %d.\n",
  1191. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1192. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1193. }
  1194. if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
  1195. cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
  1196. if (cmds_max != 0)
  1197. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1198. "can_queue must be a power of 2 and between "
  1199. "2 and %d - setting to %d.\n", cmds_max,
  1200. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1201. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1202. }
  1203. shost = scsi_host_alloc(iscsit->host_template,
  1204. hostdata_privsize(sizeof(*session)));
  1205. if (!shost)
  1206. return NULL;
  1207. /* the iscsi layer takes one task for reserve */
  1208. shost->can_queue = cmds_max - 1;
  1209. shost->cmd_per_lun = qdepth;
  1210. shost->max_id = 1;
  1211. shost->max_channel = 0;
  1212. shost->max_lun = iscsit->max_lun;
  1213. shost->max_cmd_len = iscsit->max_cmd_len;
  1214. shost->transportt = scsit;
  1215. shost->transportt->create_work_queue = 1;
  1216. *hostno = shost->host_no;
  1217. session = iscsi_hostdata(shost->hostdata);
  1218. memset(session, 0, sizeof(struct iscsi_session));
  1219. session->host = shost;
  1220. session->state = ISCSI_STATE_FREE;
  1221. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1222. session->cmds_max = cmds_max;
  1223. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1224. session->exp_cmdsn = initial_cmdsn + 1;
  1225. session->max_cmdsn = initial_cmdsn + 1;
  1226. session->max_r2t = 1;
  1227. session->tt = iscsit;
  1228. /* initialize SCSI PDU commands pool */
  1229. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1230. (void***)&session->cmds,
  1231. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1232. goto cmdpool_alloc_fail;
  1233. /* pre-format cmds pool with ITT */
  1234. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1235. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1236. if (cmd_task_size)
  1237. ctask->dd_data = &ctask[1];
  1238. ctask->itt = cmd_i;
  1239. INIT_LIST_HEAD(&ctask->running);
  1240. }
  1241. spin_lock_init(&session->lock);
  1242. /* initialize immediate command pool */
  1243. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1244. (void***)&session->mgmt_cmds,
  1245. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1246. goto mgmtpool_alloc_fail;
  1247. /* pre-format immediate cmds pool with ITT */
  1248. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1249. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1250. if (mgmt_task_size)
  1251. mtask->dd_data = &mtask[1];
  1252. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1253. INIT_LIST_HEAD(&mtask->running);
  1254. }
  1255. if (scsi_add_host(shost, NULL))
  1256. goto add_host_fail;
  1257. if (!try_module_get(iscsit->owner))
  1258. goto cls_session_fail;
  1259. cls_session = iscsi_create_session(shost, iscsit, 0);
  1260. if (!cls_session)
  1261. goto module_put;
  1262. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1263. return cls_session;
  1264. module_put:
  1265. module_put(iscsit->owner);
  1266. cls_session_fail:
  1267. scsi_remove_host(shost);
  1268. add_host_fail:
  1269. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1270. mgmtpool_alloc_fail:
  1271. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1272. cmdpool_alloc_fail:
  1273. scsi_host_put(shost);
  1274. return NULL;
  1275. }
  1276. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1277. /**
  1278. * iscsi_session_teardown - destroy session, host, and cls_session
  1279. * shost: scsi host
  1280. *
  1281. * This can be used by software iscsi_transports that allocate
  1282. * a session per scsi host.
  1283. **/
  1284. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1285. {
  1286. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1287. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1288. struct module *owner = cls_session->transport->owner;
  1289. iscsi_unblock_session(cls_session);
  1290. scsi_remove_host(shost);
  1291. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1292. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1293. kfree(session->password);
  1294. kfree(session->password_in);
  1295. kfree(session->username);
  1296. kfree(session->username_in);
  1297. kfree(session->targetname);
  1298. kfree(session->netdev);
  1299. kfree(session->hwaddress);
  1300. kfree(session->initiatorname);
  1301. iscsi_destroy_session(cls_session);
  1302. scsi_host_put(shost);
  1303. module_put(owner);
  1304. }
  1305. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1306. /**
  1307. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1308. * @cls_session: iscsi_cls_session
  1309. * @conn_idx: cid
  1310. **/
  1311. struct iscsi_cls_conn *
  1312. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1313. {
  1314. struct iscsi_session *session = class_to_transport_session(cls_session);
  1315. struct iscsi_conn *conn;
  1316. struct iscsi_cls_conn *cls_conn;
  1317. char *data;
  1318. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1319. if (!cls_conn)
  1320. return NULL;
  1321. conn = cls_conn->dd_data;
  1322. memset(conn, 0, sizeof(*conn));
  1323. conn->session = session;
  1324. conn->cls_conn = cls_conn;
  1325. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1326. conn->id = conn_idx;
  1327. conn->exp_statsn = 0;
  1328. conn->tmabort_state = TMABORT_INITIAL;
  1329. INIT_LIST_HEAD(&conn->run_list);
  1330. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1331. INIT_LIST_HEAD(&conn->xmitqueue);
  1332. /* initialize general immediate & non-immediate PDU commands queue */
  1333. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1334. GFP_KERNEL, NULL);
  1335. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1336. goto mgmtqueue_alloc_fail;
  1337. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1338. /* allocate login_mtask used for the login/text sequences */
  1339. spin_lock_bh(&session->lock);
  1340. if (!__kfifo_get(session->mgmtpool.queue,
  1341. (void*)&conn->login_mtask,
  1342. sizeof(void*))) {
  1343. spin_unlock_bh(&session->lock);
  1344. goto login_mtask_alloc_fail;
  1345. }
  1346. spin_unlock_bh(&session->lock);
  1347. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1348. if (!data)
  1349. goto login_mtask_data_alloc_fail;
  1350. conn->login_mtask->data = conn->data = data;
  1351. init_timer(&conn->tmabort_timer);
  1352. init_waitqueue_head(&conn->ehwait);
  1353. return cls_conn;
  1354. login_mtask_data_alloc_fail:
  1355. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1356. sizeof(void*));
  1357. login_mtask_alloc_fail:
  1358. kfifo_free(conn->mgmtqueue);
  1359. mgmtqueue_alloc_fail:
  1360. iscsi_destroy_conn(cls_conn);
  1361. return NULL;
  1362. }
  1363. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1364. /**
  1365. * iscsi_conn_teardown - teardown iscsi connection
  1366. * cls_conn: iscsi class connection
  1367. *
  1368. * TODO: we may need to make this into a two step process
  1369. * like scsi-mls remove + put host
  1370. */
  1371. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1372. {
  1373. struct iscsi_conn *conn = cls_conn->dd_data;
  1374. struct iscsi_session *session = conn->session;
  1375. unsigned long flags;
  1376. spin_lock_bh(&session->lock);
  1377. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1378. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1379. if (session->leadconn == conn) {
  1380. /*
  1381. * leading connection? then give up on recovery.
  1382. */
  1383. session->state = ISCSI_STATE_TERMINATE;
  1384. wake_up(&conn->ehwait);
  1385. }
  1386. spin_unlock_bh(&session->lock);
  1387. /*
  1388. * Block until all in-progress commands for this connection
  1389. * time out or fail.
  1390. */
  1391. for (;;) {
  1392. spin_lock_irqsave(session->host->host_lock, flags);
  1393. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1394. spin_unlock_irqrestore(session->host->host_lock, flags);
  1395. break;
  1396. }
  1397. spin_unlock_irqrestore(session->host->host_lock, flags);
  1398. msleep_interruptible(500);
  1399. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1400. "host_failed %d\n", session->host->host_busy,
  1401. session->host->host_failed);
  1402. /*
  1403. * force eh_abort() to unblock
  1404. */
  1405. wake_up(&conn->ehwait);
  1406. }
  1407. /* flush queued up work because we free the connection below */
  1408. scsi_flush_work(session->host);
  1409. spin_lock_bh(&session->lock);
  1410. kfree(conn->data);
  1411. kfree(conn->persistent_address);
  1412. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1413. sizeof(void*));
  1414. if (session->leadconn == conn)
  1415. session->leadconn = NULL;
  1416. spin_unlock_bh(&session->lock);
  1417. kfifo_free(conn->mgmtqueue);
  1418. iscsi_destroy_conn(cls_conn);
  1419. }
  1420. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1421. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1422. {
  1423. struct iscsi_conn *conn = cls_conn->dd_data;
  1424. struct iscsi_session *session = conn->session;
  1425. if (!session) {
  1426. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1427. return -EPERM;
  1428. }
  1429. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1430. session->first_burst > session->max_burst) {
  1431. printk("iscsi: invalid burst lengths: "
  1432. "first_burst %d max_burst %d\n",
  1433. session->first_burst, session->max_burst);
  1434. return -EINVAL;
  1435. }
  1436. spin_lock_bh(&session->lock);
  1437. conn->c_stage = ISCSI_CONN_STARTED;
  1438. session->state = ISCSI_STATE_LOGGED_IN;
  1439. session->queued_cmdsn = session->cmdsn;
  1440. switch(conn->stop_stage) {
  1441. case STOP_CONN_RECOVER:
  1442. /*
  1443. * unblock eh_abort() if it is blocked. re-try all
  1444. * commands after successful recovery
  1445. */
  1446. conn->stop_stage = 0;
  1447. conn->tmabort_state = TMABORT_INITIAL;
  1448. session->age++;
  1449. spin_unlock_bh(&session->lock);
  1450. iscsi_unblock_session(session_to_cls(session));
  1451. wake_up(&conn->ehwait);
  1452. return 0;
  1453. case STOP_CONN_TERM:
  1454. conn->stop_stage = 0;
  1455. break;
  1456. default:
  1457. break;
  1458. }
  1459. spin_unlock_bh(&session->lock);
  1460. return 0;
  1461. }
  1462. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1463. static void
  1464. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1465. {
  1466. struct iscsi_mgmt_task *mtask, *tmp;
  1467. /* handle pending */
  1468. while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1469. if (mtask == conn->login_mtask)
  1470. continue;
  1471. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1472. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1473. sizeof(void*));
  1474. }
  1475. /* handle running */
  1476. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1477. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1478. list_del(&mtask->running);
  1479. if (mtask == conn->login_mtask)
  1480. continue;
  1481. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1482. sizeof(void*));
  1483. }
  1484. conn->mtask = NULL;
  1485. }
  1486. /* Fail commands. Mutex and session lock held and recv side suspended */
  1487. static void fail_all_commands(struct iscsi_conn *conn)
  1488. {
  1489. struct iscsi_cmd_task *ctask, *tmp;
  1490. /* flush pending */
  1491. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1492. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1493. ctask->itt);
  1494. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1495. }
  1496. /* fail all other running */
  1497. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1498. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1499. ctask->sc, ctask->itt);
  1500. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1501. }
  1502. conn->ctask = NULL;
  1503. }
  1504. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1505. struct iscsi_conn *conn, int flag)
  1506. {
  1507. int old_stop_stage;
  1508. spin_lock_bh(&session->lock);
  1509. if (conn->stop_stage == STOP_CONN_TERM) {
  1510. spin_unlock_bh(&session->lock);
  1511. return;
  1512. }
  1513. /*
  1514. * When this is called for the in_login state, we only want to clean
  1515. * up the login task and connection. We do not need to block and set
  1516. * the recovery state again
  1517. */
  1518. if (flag == STOP_CONN_TERM)
  1519. session->state = ISCSI_STATE_TERMINATE;
  1520. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1521. session->state = ISCSI_STATE_IN_RECOVERY;
  1522. old_stop_stage = conn->stop_stage;
  1523. conn->stop_stage = flag;
  1524. conn->c_stage = ISCSI_CONN_STOPPED;
  1525. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1526. spin_unlock_bh(&session->lock);
  1527. scsi_flush_work(session->host);
  1528. write_lock_bh(conn->recv_lock);
  1529. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1530. write_unlock_bh(conn->recv_lock);
  1531. /*
  1532. * for connection level recovery we should not calculate
  1533. * header digest. conn->hdr_size used for optimization
  1534. * in hdr_extract() and will be re-negotiated at
  1535. * set_param() time.
  1536. */
  1537. if (flag == STOP_CONN_RECOVER) {
  1538. conn->hdrdgst_en = 0;
  1539. conn->datadgst_en = 0;
  1540. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1541. old_stop_stage != STOP_CONN_RECOVER) {
  1542. debug_scsi("blocking session\n");
  1543. iscsi_block_session(session_to_cls(session));
  1544. }
  1545. }
  1546. /*
  1547. * flush queues.
  1548. */
  1549. spin_lock_bh(&session->lock);
  1550. fail_all_commands(conn);
  1551. flush_control_queues(session, conn);
  1552. spin_unlock_bh(&session->lock);
  1553. }
  1554. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1555. {
  1556. struct iscsi_conn *conn = cls_conn->dd_data;
  1557. struct iscsi_session *session = conn->session;
  1558. switch (flag) {
  1559. case STOP_CONN_RECOVER:
  1560. case STOP_CONN_TERM:
  1561. iscsi_start_session_recovery(session, conn, flag);
  1562. break;
  1563. default:
  1564. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1565. }
  1566. }
  1567. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1568. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1569. struct iscsi_cls_conn *cls_conn, int is_leading)
  1570. {
  1571. struct iscsi_session *session = class_to_transport_session(cls_session);
  1572. struct iscsi_conn *conn = cls_conn->dd_data;
  1573. spin_lock_bh(&session->lock);
  1574. if (is_leading)
  1575. session->leadconn = conn;
  1576. spin_unlock_bh(&session->lock);
  1577. /*
  1578. * Unblock xmitworker(), Login Phase will pass through.
  1579. */
  1580. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1581. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1582. return 0;
  1583. }
  1584. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1585. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1586. enum iscsi_param param, char *buf, int buflen)
  1587. {
  1588. struct iscsi_conn *conn = cls_conn->dd_data;
  1589. struct iscsi_session *session = conn->session;
  1590. uint32_t value;
  1591. switch(param) {
  1592. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1593. sscanf(buf, "%d", &conn->max_recv_dlength);
  1594. break;
  1595. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1596. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1597. break;
  1598. case ISCSI_PARAM_HDRDGST_EN:
  1599. sscanf(buf, "%d", &conn->hdrdgst_en);
  1600. break;
  1601. case ISCSI_PARAM_DATADGST_EN:
  1602. sscanf(buf, "%d", &conn->datadgst_en);
  1603. break;
  1604. case ISCSI_PARAM_INITIAL_R2T_EN:
  1605. sscanf(buf, "%d", &session->initial_r2t_en);
  1606. break;
  1607. case ISCSI_PARAM_MAX_R2T:
  1608. sscanf(buf, "%d", &session->max_r2t);
  1609. break;
  1610. case ISCSI_PARAM_IMM_DATA_EN:
  1611. sscanf(buf, "%d", &session->imm_data_en);
  1612. break;
  1613. case ISCSI_PARAM_FIRST_BURST:
  1614. sscanf(buf, "%d", &session->first_burst);
  1615. break;
  1616. case ISCSI_PARAM_MAX_BURST:
  1617. sscanf(buf, "%d", &session->max_burst);
  1618. break;
  1619. case ISCSI_PARAM_PDU_INORDER_EN:
  1620. sscanf(buf, "%d", &session->pdu_inorder_en);
  1621. break;
  1622. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1623. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1624. break;
  1625. case ISCSI_PARAM_ERL:
  1626. sscanf(buf, "%d", &session->erl);
  1627. break;
  1628. case ISCSI_PARAM_IFMARKER_EN:
  1629. sscanf(buf, "%d", &value);
  1630. BUG_ON(value);
  1631. break;
  1632. case ISCSI_PARAM_OFMARKER_EN:
  1633. sscanf(buf, "%d", &value);
  1634. BUG_ON(value);
  1635. break;
  1636. case ISCSI_PARAM_EXP_STATSN:
  1637. sscanf(buf, "%u", &conn->exp_statsn);
  1638. break;
  1639. case ISCSI_PARAM_USERNAME:
  1640. kfree(session->username);
  1641. session->username = kstrdup(buf, GFP_KERNEL);
  1642. if (!session->username)
  1643. return -ENOMEM;
  1644. break;
  1645. case ISCSI_PARAM_USERNAME_IN:
  1646. kfree(session->username_in);
  1647. session->username_in = kstrdup(buf, GFP_KERNEL);
  1648. if (!session->username_in)
  1649. return -ENOMEM;
  1650. break;
  1651. case ISCSI_PARAM_PASSWORD:
  1652. kfree(session->password);
  1653. session->password = kstrdup(buf, GFP_KERNEL);
  1654. if (!session->password)
  1655. return -ENOMEM;
  1656. break;
  1657. case ISCSI_PARAM_PASSWORD_IN:
  1658. kfree(session->password_in);
  1659. session->password_in = kstrdup(buf, GFP_KERNEL);
  1660. if (!session->password_in)
  1661. return -ENOMEM;
  1662. break;
  1663. case ISCSI_PARAM_TARGET_NAME:
  1664. /* this should not change between logins */
  1665. if (session->targetname)
  1666. break;
  1667. session->targetname = kstrdup(buf, GFP_KERNEL);
  1668. if (!session->targetname)
  1669. return -ENOMEM;
  1670. break;
  1671. case ISCSI_PARAM_TPGT:
  1672. sscanf(buf, "%d", &session->tpgt);
  1673. break;
  1674. case ISCSI_PARAM_PERSISTENT_PORT:
  1675. sscanf(buf, "%d", &conn->persistent_port);
  1676. break;
  1677. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1678. /*
  1679. * this is the address returned in discovery so it should
  1680. * not change between logins.
  1681. */
  1682. if (conn->persistent_address)
  1683. break;
  1684. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1685. if (!conn->persistent_address)
  1686. return -ENOMEM;
  1687. break;
  1688. default:
  1689. return -ENOSYS;
  1690. }
  1691. return 0;
  1692. }
  1693. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1694. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1695. enum iscsi_param param, char *buf)
  1696. {
  1697. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1698. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1699. int len;
  1700. switch(param) {
  1701. case ISCSI_PARAM_INITIAL_R2T_EN:
  1702. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1703. break;
  1704. case ISCSI_PARAM_MAX_R2T:
  1705. len = sprintf(buf, "%hu\n", session->max_r2t);
  1706. break;
  1707. case ISCSI_PARAM_IMM_DATA_EN:
  1708. len = sprintf(buf, "%d\n", session->imm_data_en);
  1709. break;
  1710. case ISCSI_PARAM_FIRST_BURST:
  1711. len = sprintf(buf, "%u\n", session->first_burst);
  1712. break;
  1713. case ISCSI_PARAM_MAX_BURST:
  1714. len = sprintf(buf, "%u\n", session->max_burst);
  1715. break;
  1716. case ISCSI_PARAM_PDU_INORDER_EN:
  1717. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1718. break;
  1719. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1720. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1721. break;
  1722. case ISCSI_PARAM_ERL:
  1723. len = sprintf(buf, "%d\n", session->erl);
  1724. break;
  1725. case ISCSI_PARAM_TARGET_NAME:
  1726. len = sprintf(buf, "%s\n", session->targetname);
  1727. break;
  1728. case ISCSI_PARAM_TPGT:
  1729. len = sprintf(buf, "%d\n", session->tpgt);
  1730. break;
  1731. case ISCSI_PARAM_USERNAME:
  1732. len = sprintf(buf, "%s\n", session->username);
  1733. break;
  1734. case ISCSI_PARAM_USERNAME_IN:
  1735. len = sprintf(buf, "%s\n", session->username_in);
  1736. break;
  1737. case ISCSI_PARAM_PASSWORD:
  1738. len = sprintf(buf, "%s\n", session->password);
  1739. break;
  1740. case ISCSI_PARAM_PASSWORD_IN:
  1741. len = sprintf(buf, "%s\n", session->password_in);
  1742. break;
  1743. default:
  1744. return -ENOSYS;
  1745. }
  1746. return len;
  1747. }
  1748. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1749. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1750. enum iscsi_param param, char *buf)
  1751. {
  1752. struct iscsi_conn *conn = cls_conn->dd_data;
  1753. int len;
  1754. switch(param) {
  1755. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1756. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1757. break;
  1758. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1759. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1760. break;
  1761. case ISCSI_PARAM_HDRDGST_EN:
  1762. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1763. break;
  1764. case ISCSI_PARAM_DATADGST_EN:
  1765. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1766. break;
  1767. case ISCSI_PARAM_IFMARKER_EN:
  1768. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1769. break;
  1770. case ISCSI_PARAM_OFMARKER_EN:
  1771. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1772. break;
  1773. case ISCSI_PARAM_EXP_STATSN:
  1774. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1775. break;
  1776. case ISCSI_PARAM_PERSISTENT_PORT:
  1777. len = sprintf(buf, "%d\n", conn->persistent_port);
  1778. break;
  1779. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1780. len = sprintf(buf, "%s\n", conn->persistent_address);
  1781. break;
  1782. default:
  1783. return -ENOSYS;
  1784. }
  1785. return len;
  1786. }
  1787. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1788. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1789. char *buf)
  1790. {
  1791. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1792. int len;
  1793. switch (param) {
  1794. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1795. if (!session->netdev)
  1796. len = sprintf(buf, "%s\n", "default");
  1797. else
  1798. len = sprintf(buf, "%s\n", session->netdev);
  1799. break;
  1800. case ISCSI_HOST_PARAM_HWADDRESS:
  1801. if (!session->hwaddress)
  1802. len = sprintf(buf, "%s\n", "default");
  1803. else
  1804. len = sprintf(buf, "%s\n", session->hwaddress);
  1805. break;
  1806. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1807. if (!session->initiatorname)
  1808. len = sprintf(buf, "%s\n", "unknown");
  1809. else
  1810. len = sprintf(buf, "%s\n", session->initiatorname);
  1811. break;
  1812. default:
  1813. return -ENOSYS;
  1814. }
  1815. return len;
  1816. }
  1817. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  1818. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1819. char *buf, int buflen)
  1820. {
  1821. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1822. switch (param) {
  1823. case ISCSI_HOST_PARAM_NETDEV_NAME:
  1824. if (!session->netdev)
  1825. session->netdev = kstrdup(buf, GFP_KERNEL);
  1826. break;
  1827. case ISCSI_HOST_PARAM_HWADDRESS:
  1828. if (!session->hwaddress)
  1829. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  1830. break;
  1831. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  1832. if (!session->initiatorname)
  1833. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  1834. break;
  1835. default:
  1836. return -ENOSYS;
  1837. }
  1838. return 0;
  1839. }
  1840. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  1841. MODULE_AUTHOR("Mike Christie");
  1842. MODULE_DESCRIPTION("iSCSI library functions");
  1843. MODULE_LICENSE("GPL");