libiscsi.c 59 KB

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