libiscsi.c 57 KB

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