libiscsi.c 54 KB

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