libiscsi.c 57 KB

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